hopwise.model.knowledge_aware_recommender.kgat

Reference:

Xiang Wang et al. “KGAT: Knowledge Graph Attention Network for Recommendation.” in SIGKDD 2019.

Reference code:

https://github.com/xiangwang1223/knowledge_graph_attention_network

Classes

Aggregator

GNN Aggregator layer

KGAT

KGAT is a knowledge-based recommendation model. It combines knowledge graph and the user-item interaction

Module Contents

class hopwise.model.knowledge_aware_recommender.kgat.Aggregator(input_dim, output_dim, dropout, aggregator_type)[source]

Bases: torch.nn.Module

GNN Aggregator layer

input_dim
output_dim
dropout
aggregator_type
message_dropout
activation
forward(norm_matrix, ego_embeddings)[source]
class hopwise.model.knowledge_aware_recommender.kgat.KGAT(config, dataset)[source]

Bases: hopwise.model.abstract_recommender.KnowledgeRecommender

KGAT is a knowledge-based recommendation model. It combines knowledge graph and the user-item interaction graph to a new graph called collaborative knowledge graph (CKG). This model learns the representations of users and items by exploiting the structure of CKG. It adopts a GNN-based architecture and define the attention on the CKG.

input_type
ckg
all_hs = None
all_ts = None
all_rs = None
matrix_size
embedding_size
kg_embedding_size
layers
aggregator_type
mess_dropout
reg_weight
A_in
user_embedding
entity_embedding
relation_embedding
trans_w
aggregator_layers
tanh
mf_loss
reg_loss
restore_user_e = None
restore_entity_e = None
other_parameter_name = ['restore_user_e', 'restore_entity_e']
init_graph()[source]

Get the initial attention matrix through the collaborative knowledge graph

Returns:

Sparse tensor of the attention matrix

Return type:

torch.sparse.FloatTensor

_get_ego_embeddings()[source]
forward()[source]
_get_kg_embedding(h, r, pos_t, neg_t)[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

calculate_kg_loss(interaction)[source]

Calculate the training loss for a batch data of KG.

Parameters:

interaction (Interaction) – Interaction class of the batch.

Returns:

Training loss, shape: []

Return type:

torch.Tensor

generate_transE_score(hs, ts, r)[source]

Calculating scores for triples in KG.

Parameters:
  • hs (torch.Tensor) – head entities

  • ts (torch.Tensor) – tail entities

  • r (int) – the relation id between hs and ts

Returns:

the scores of (hs, r, ts)

Return type:

torch.Tensor

update_attentive_A()[source]

Update the attention matrix using the updated embedding matrix

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

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