hopwise.model.general_recommender.nncf

Reference:

Ting Bai et al. “A Neural Collaborative Filtering Model with Interaction-based Neighborhood.” in CIKM 2017.

Reference code:

https://github.com/Tbbaby/NNCF-Pytorch

Classes

NNCF

NNCF is an neural network enhanced matrix factorization model which also captures neighborhood information.

Module Contents

class hopwise.model.general_recommender.nncf.NNCF(config, dataset)[source]

Bases: hopwise.model.abstract_recommender.GeneralRecommender

NNCF is an neural network enhanced matrix factorization model which also captures neighborhood information. We implement the NNCF model with three ways to process neighborhood information.

input_type
LABEL
interaction_matrix
ui_embedding_size
neigh_embedding_size
num_conv_kernel
conv_kernel_size
pool_kernel_size
mlp_hidden_size
neigh_num
neigh_info_method
resolution
user_embedding
item_embedding
user_neigh_embedding
item_neigh_embedding
user_conv
item_conv
mlp_layers
out_layer
dropout_layer
loss
_init_weights(module)[source]
Max_ner(lst, max_ner)[source]

Unify embedding length of neighborhood information for efficiency consideration. Truncate the list if the length is larger than max_ner. Otherwise, pad it with 0.

Parameters:
  • lst (list) – The input list contains node’s neighbors.

  • max_ner (int) – The number of neighbors we choose for each node.

Returns:

The list of a node’s community neighbors.

Return type:

list

get_community_member(partition, community_dict, node, kind)[source]

Find other nodes in the same community. e.g. If the node starts with letter “i”, the other nodes start with letter “i” in the same community dict group are its community neighbors.

Parameters:
  • partition (dict) – The input dict that contains the community each node belongs.

  • community_dict (dict) – The input dict that shows the nodes each community contains.

  • node (int) – The id of the input node.

  • kind (char) – The type of the input node.

Returns:

The list of a node’s community neighbors.

Return type:

list

prepare_vector_element(partition, relation, community_dict)[source]

Find the community neighbors of each node, i.e. I(u) and U(i). Then reset the id of nodes.

Parameters:
  • partition (dict) – The input dict that contains the community each node belongs.

  • relation (list) – The input list that contains the relationships of users and items.

  • community_dict (dict) – The input dict that shows the nodes each community contains.

Returns:

The list of nodes’ community neighbors.

Return type:

list

get_neigh_louvain()[source]

Get neighborhood information using louvain algorithm. First, change the id of node, for example, the id of user node “1” will be set to “u_1” in order to use louvain algorithm. Second, use louvain algorithm to seperate nodes into different communities. Finally, find the community neighbors of each node with the same type and reset the id of the nodes.

Returns:

The neighborhood nodes of a batch of user or item, shape: [batch_size, neigh_num]

Return type:

torch.IntTensor

get_neigh_knn()[source]

Get neighborhood information using knn algorithm. Find direct neighbors of each node, if the number of direct neighbors is less than neigh_num, add other similar neighbors using knn algorithm. Otherwise, select random top k direct neighbors, k equals to the number of neighbors.

Returns:

The neighborhood nodes of a batch of user or item, shape: [batch_size, neigh_num]

Return type:

torch.IntTensor

get_neigh_random()[source]

Get neighborhood information using random algorithm. Select random top k direct neighbors, k equals to the number of neighbors.

Returns:

The neighborhood nodes of a batch of user or item, shape: [batch_size, neigh_num]

Return type:

torch.IntTensor

get_neigh_info(user, item)[source]

Get a batch of neighborhood embedding tensor according to input id.

Parameters:
  • user (torch.LongTensor) – The input tensor that contains user’s id, shape: [batch_size, ]

  • item (torch.LongTensor) – The input tensor that contains item’s id, shape: [batch_size, ]

Returns:

The neighborhood embedding tensor of a batch of user, shape: [batch_size, neigh_embedding_size] torch.FloatTensor: The neighborhood embedding tensor of a batch of item, shape: [batch_size, neigh_embedding_size]

Return type:

torch.FloatTensor

forward(user, item)[source]
calculate_loss(interaction)[source]

Calculate the training loss for a batch data.

Parameters:

interaction (Interaction) – Interaction class of the batch.

Returns:

Training loss, shape: []

Return type:

torch.Tensor

predict(interaction)[source]

Predict the scores between users and items.

Parameters:

interaction (Interaction) – Interaction class of the batch.

Returns:

Predicted scores for given users and items, shape: [batch_size]

Return type:

torch.Tensor