KGCN¶
- Reference:
Hongwei Wang et al. “Knowledge graph convolution networks for recommender systems.” in WWW 2019.
- Reference code:
-
class
recbole.model.knowledge_aware_recommender.kgcn.
KGCN
(config, dataset)[source]¶ Bases:
recbole.model.abstract_recommender.KnowledgeRecommender
KGCN is a knowledge-based recommendation model that captures inter-item relatedness effectively by mining their associated attributes on the KG. To automatically discover both high-order structure information and semantic information of the KG, we treat KG as an undirected graph and sample from the neighbors for each entity in the KG as their receptive field, then combine neighborhood information with bias when calculating the representation of a given entity.
-
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
-
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_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¶
-
mix_neighbor_vectors
(neighbor_vectors, neighbor_relations, user_embeddings)[source]¶ Mix neighbor vectors on user-specific graph.
- Parameters
neighbor_vectors (torch.FloatTensor) – The embeddings of neighbor entities(items), shape: [batch_size, -1, neighbor_sample_size, embedding_size]
neighbor_relations (torch.FloatTensor) – The embeddings of neighbor relations, shape: [batch_size, -1, neighbor_sample_size, embedding_size]
user_embeddings (torch.FloatTensor) – The embeddings of users, shape: [batch_size, embedding_size]
- Returns
The neighbors aggregated embeddings, shape: [batch_size, -1, embedding_size]
- Return type
neighbors_aggregated(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
-