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

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

Bases: recbole.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.

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

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

forward(user, item)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

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

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

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_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_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

input_type = 1
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

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

training: bool