hopwise.model.knowledge_aware_recommender.kgrec

Reference:

Yuhao Yang et al. “Knowledge Graph Self-Supervised Rationalization for Recommendation” in WWW 2021.

Reference code:

https://github.com/HKUDS/KGRec

Classes

Contrast

Base class for all neural network modules.

AttnHGCN

Heterogeneous Graph Convolutional Network

KGRec

KGRec is a self-supervised knowledge-aware recommender that identifies and focuses on informative knowledge

Module Contents

class hopwise.model.knowledge_aware_recommender.kgrec.Contrast(num_hidden: int, tau: float = 0.7)[source]

Bases: torch.nn.Module

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

tau: float = 0.7
mlp1
mlp2
sim(z1: torch.Tensor, z2: torch.Tensor)[source]
self_sim(z1, z2)[source]
loss(z1: torch.Tensor, z2: torch.Tensor)[source]
forward(z1: torch.Tensor, z2: torch.Tensor)[source]
class hopwise.model.knowledge_aware_recommender.kgrec.AttnHGCN(embedding_size, n_hops, n_users, n_relations, mess_dropout_rate=0.1)[source]

Bases: torch.nn.Module

Heterogeneous Graph Convolutional Network

no_attn_convs
embedding_size
n_hops
n_relations
n_users
mess_dropout_rate = 0.1
relation_embedding
W_Q
n_heads = 2
d_k
mess_dropout
shared_layer_agg(user_emb, entity_emb, edge_index, edge_type, inter_edge, inter_edge_w)[source]
forward(user_emb, entity_emb, edge_index, edge_type, inter_edge, inter_edge_w, item_attn=None)[source]
forward_ui(user_emb, item_emb, inter_edge, inter_edge_w)[source]
forward_kg(entity_emb, edge_index, edge_type)[source]
ui_agg(user_emb, item_emb, inter_edge, inter_edge_w)[source]
kg_agg(entity_emb, edge_index, edge_type)[source]
norm_attn_computer(entity_emb, edge_index, edge_type=None, return_logits=False)[source]
class hopwise.model.knowledge_aware_recommender.kgrec.KGRec(config, dataset)[source]

Bases: hopwise.model.abstract_recommender.KnowledgeRecommender

KGRec is a self-supervised knowledge-aware recommender that identifies and focuses on informative knowledge graph connections through an attentive rationalization mechanism. It combines generative masking reconstruction and contrastive learning tasks to highlight and align meaningful knowledge and interaction signals. By masking and rebuilding high-rationale edges while filtering noisy ones, KGRec learns more interpretable and noise-resistant recommendations.

input_type
embedding_size
reg_weight
context_hops
node_dropout_rate
mess_dropout_rate
mae_coef
mae_msize
cl_coef
cl_tau
cl_drop
samp_func
inter_edge
kg_graph
user_embedding
entity_embedding
mf_loss
reg_loss
restore_user_e = None
restore_entity_e = None
gcn
contrast_fn
node_dropout
get_edges(graph)[source]
forward()[source]
calculate_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

relation_aware_edge_sampling(sampling_rate=0.5)[source]
edge_sampling(edge_index, edge_type, sampling_rate=0.5)[source]
mae_edge_mask_adapt_mixed(edge_index, edge_type, topk_egde_id)[source]
adaptive_kg_drop_cl(edge_index, edge_type, edge_attn_score)[source]
adaptive_ui_drop_cl(item_attn_mean, inter_edge, inter_edge_w)[source]
create_mae_loss(node_pair_emb, masked_edge_emb=None)[source]
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