KGNNLS

Reference:

Hongwei Wang et al. “Knowledge-aware Graph Neural Networks with Label Smoothness Regularization for Recommender Systems.” in KDD 2019.

Reference code:

https://github.com/hwwang55/KGNN-LS

class recbole.model.knowledge_aware_recommender.kgnnls.KGNNLS(config, dataset)[source]

Bases: recbole.model.abstract_recommender.KnowledgeRecommender

KGNN-LS is a knowledge-based recommendation model. KGNN-LS transforms the knowledge graph into a user-specific weighted graph and then apply a graph neural network to compute personalized item embeddings. To provide better inductive bias, KGNN-LS relies on label smoothness assumption, which posits that adjacent items in the knowledge graph are likely to have similar user relevance labels/scores. Label smoothness provides regularization over the edge weights and it is equivalent to a label propagation scheme on a graph.

aggregate(user_embeddings, entities, relations)[source]

For each item, aggregate the entity representation and its neighborhood representation into a single vector.

Parameters
  • user_embeddings (torch.FloatTensor) – The embeddings of users, shape: [batch_size, embedding_size]

  • entities (list) – entities is a list of i-iter (i = 0, 1, …, n_iter) neighbors for the batch of items. dimensions of entities: {[batch_size, 1], [batch_size, n_neighbor], [batch_size, n_neighbor^2], …, [batch_size, n_neighbor^n_iter]}

  • relations (list) – relations is a list of i-iter (i = 0, 1, …, n_iter) corresponding relations for entities. relations have the same shape as entities.

Returns

The embeddings of items, shape: [batch_size, embedding_size]

Return type

item_embeddings(torch.FloatTensor)

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

calculate_ls_loss(user, item, target)[source]

Calculate label smoothness loss.

Parameters
  • user (torch.FloatTensor) – the index of users, shape: [batch_size*2],

  • item (torch.FloatTensor) – the index of items, shape: [batch_size*2],

  • target (torch.FloatTensor) – the label of user-item, shape: [batch_size*2],

Returns

label smoothness loss

Return type

ls_loss

construct_adj(kg_graph)[source]

Get neighbors and corresponding relations for each entity in the KG.

Parameters

kg_graph (scipy.sparse.coo_matrix) – an undirected graph

Returns

  • adj_entity (torch.LongTensor): each line stores the sampled neighbor entities for a given entity, shape: [n_entities, neighbor_sample_size]

  • adj_relation (torch.LongTensor): each line stores the corresponding sampled neighbor relations, shape: [n_entities, neighbor_sample_size]

Return type

tuple

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.

full_sort_predict(interaction)[source]

full sort prediction function. Given users, calculate the scores between users and all candidate items.

Parameters

interaction (Interaction) – Interaction class of the batch.

Returns

Predicted scores for given users and all candidate items, shape: [n_batch_users * n_candidate_items]

Return type

torch.Tensor

get_interaction_table(user_id, item_id, y)[source]

Get interaction_table that is used for fetching user-item interaction label in LS regularization.

Parameters
  • user_id (torch.Tensor) – the user id in user-item interactions, shape: [n_interactions, 1]

  • item_id (torch.Tensor) – the item id in user-item interactions, shape: [n_interactions, 1]

  • y (torch.Tensor) – the label in user-item interactions, shape: [n_interactions, 1]

Returns

  • interaction_table(dict): key: user_id * 10^offset + item_id; value: y_{user_id, item_id}

  • offset(int): The offset that is used for calculating the key(index) in interaction_table

Return type

tuple

get_neighbors(items)[source]

Get neighbors and corresponding relations for each entity in items from adj_entity and adj_relation.

Parameters

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

Returns

  • entities(list): Entities is a list of i-iter (i = 0, 1, …, n_iter) neighbors for the batch of items. dimensions of entities: {[batch_size, 1], [batch_size, n_neighbor], [batch_size, n_neighbor^2], …, [batch_size, n_neighbor^n_iter]}

  • relations(list): Relations is a list of i-iter (i = 0, 1, …, n_iter) corresponding relations for entities. Relations have the same shape as entities.

Return type

tuple

input_type = 2
label_smoothness_predict(user_embeddings, user, entities, relations)[source]

Predict the label of items by label smoothness.

Parameters
  • user_embeddings (torch.FloatTensor) – The embeddings of users, shape: [batch_size*2, embedding_size],

  • user (torch.FloatTensor) – the index of users, shape: [batch_size*2]

  • entities (list) – entities is a list of i-iter (i = 0, 1, …, n_iter) neighbors for the batch of items. dimensions of entities: {[batch_size*2, 1], [batch_size*2, n_neighbor], [batch_size*2, n_neighbor^2], …, [batch_size*2, n_neighbor^n_iter]}

  • relations (list) – relations is a list of i-iter (i = 0, 1, …, n_iter) corresponding relations for entities. relations have the same shape as entities.

Returns

The predicted label of items, shape: [batch_size*2]

Return type

predicted_labels(torch.FloatTensor)

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

sample_neg_interaction(pos_interaction_table, offset)[source]

Sample neg_interaction to construct train data.

Parameters
  • pos_interaction_table (dict) – the interaction_table that only contains pos_interaction.

  • offset (int) – The offset that is used for calculating the key(index) in interaction_table

Returns

key: user_id * 10^offset + item_id; value: y_{user_id, item_id}

Return type

interaction_table(dict)