hopwise.model.abstract_recommender

Classes

AbstractRecommender

Base class for all models

GeneralRecommender

This is a abstract general recommender. All the general model should implement this class.

AutoEncoderMixin

This is a common part of auto-encoders. All the auto-encoder models should inherit this class,

SequentialRecommender

This is a abstract sequential recommender. All the sequential model should implement This class.

KnowledgeRecommender

This is a abstract knowledge-based recommender. All the knowledge-based model should implement this class.

ExplainableRecommender

This is a abstract explainable-based recommender. All the explainable-based model should implement this class.

PathLanguageModelingRecommender

This is an abstract path-language-modeling recommender.

ContextRecommender

This is a abstract context-aware recommender. All the context-aware model should implement this class.

Module Contents

class hopwise.model.abstract_recommender.AbstractRecommender(_skip_nn_module_init=False)[source]

Bases: torch.nn.Module

Base class for all models

logger
abstractmethod 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

abstractmethod 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

abstractmethod 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

abstractmethod full_sort_predict_kg(interaction)[source]

Full sort prediction KG function. Given heads, calculate the scores between heads and all candidate tails.

Parameters:

interaction (Interaction) – Interaction class of the batch.

Returns:

Predicted scores for given heads and all candidate tails, shape: [n_batch_heads * n_candidate_tails]

Return type:

torch.Tensor

other_parameter()[source]
load_other_parameter(para)[source]
__str__()[source]

Model prints with number of trainable parameters

class hopwise.model.abstract_recommender.GeneralRecommender(config, dataset)[source]

Bases: AbstractRecommender

This is a abstract general recommender. All the general model should implement this class. The base general recommender class provide the basic dataset and parameters information.

type

Casts all parameters and buffers to dst_type.

Note

This method modifies the module in-place.

Parameters:

dst_type (type or string) – the desired type

Returns:

self

Return type:

Module

USER_ID
ITEM_ID
NEG_ITEM_ID
n_users
n_items
device
class hopwise.model.abstract_recommender.AutoEncoderMixin[source]

This is a common part of auto-encoders. All the auto-encoder models should inherit this class, including CDAE, MacridVAE, MultiDAE, MultiVAE, RaCT and RecVAE. The base AutoEncoderMixin class provides basic dataset information and rating matrix function.

build_histroy_items(dataset)[source]
get_rating_matrix(user)[source]

Get a batch of user’s feature with the user’s id and history interaction matrix.

Parameters:

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

Returns:

The user’s feature of a batch of user, shape: [batch_size, n_items]

Return type:

torch.FloatTensor

class hopwise.model.abstract_recommender.SequentialRecommender(config, dataset)[source]

Bases: AbstractRecommender

This is a abstract sequential recommender. All the sequential model should implement This class.

type

Casts all parameters and buffers to dst_type.

Note

This method modifies the module in-place.

Parameters:

dst_type (type or string) – the desired type

Returns:

self

Return type:

Module

USER_ID
ITEM_ID
ITEM_SEQ
ITEM_SEQ_LEN
POS_ITEM_ID
NEG_ITEM_ID
max_seq_length
n_items
device
gather_indexes(output, gather_index)[source]

Gathers the vectors at the specific positions over a minibatch

get_attention_mask(item_seq, bidirectional=False)[source]

Generate left-to-right uni-directional or bidirectional attention mask for multi-head attention.

class hopwise.model.abstract_recommender.KnowledgeRecommender(config, dataset, _skip_nn_module_init=False)[source]

Bases: AbstractRecommender

This is a abstract knowledge-based recommender. All the knowledge-based model should implement this class. The base knowledge-based recommender class provide the basic dataset and parameters information.

type

Casts all parameters and buffers to dst_type.

Note

This method modifies the module in-place.

Parameters:

dst_type (type or string) – the desired type

Returns:

self

Return type:

Module

USER_ID
ITEM_ID
NEG_ITEM_ID
ENTITY_ID
RELATION_ID
HEAD_ENTITY_ID
TAIL_ENTITY_ID
NEG_TAIL_ENTITY_ID
n_users
n_items
n_entities
n_relations
class hopwise.model.abstract_recommender.ExplainableRecommender[source]

This is a abstract explainable-based recommender. All the explainable-based model should implement this class. This class use templates to make the explanation more interpretable.

abstractmethod explain(interaction)[source]

Explain the prediction function.

Given users, calculate the scores and paths between users and all candidate items, then return the templates filled with path data.

Parameters:

interaction (Interaction) – The interaction batch.

Returns:

Predicted scores for given users and all candidate items,

with shape [n_batch_users * n_candidate_items].

pandas.DataFrame: Explanation of the prediction, containing paths and corresponding templates,

with shape [n_paths * [uid, pid, score, template1, template2, …, #templates]].

Return type:

torch.Tensor

abstractmethod decode_path(path)[source]

Decode the path into a string. Path decoding is specific to each model.

Parameters:

path (list) – The path data.

Returns:

The decoded path string.

Return type:

str

class hopwise.model.abstract_recommender.PathLanguageModelingRecommender(config, dataset, _skip_nn_module_init=True)[source]

Bases: KnowledgeRecommender

This is an abstract path-language-modeling recommender. All the path-language-modeling model should implement this class. The base path-language-modeling recommender class inherits the knowledge-aware recommender class to learn from knowledge graph paths defined by a chain of entity-relation triplets.

type

Casts all parameters and buffers to dst_type.

Note

This method modifies the module in-place.

Parameters:

dst_type (type or string) – the desired type

Returns:

self

Return type:

Module

input_type
class hopwise.model.abstract_recommender.ContextRecommender(config, dataset)[source]

Bases: AbstractRecommender

This is a abstract context-aware recommender. All the context-aware model should implement this class. The base context-aware recommender class provide the basic embedding function of feature fields which also contains a first-order part of feature fields.

type

Casts all parameters and buffers to dst_type.

Note

This method modifies the module in-place.

Parameters:

dst_type (type or string) – the desired type

Returns:

self

Return type:

Module

input_type
field_names
LABEL
embedding_size
device
double_tower
numerical_features
token_field_names = []
token_field_dims = []
float_field_names = []
float_field_dims = []
token_seq_field_names = []
token_seq_field_dims = []
float_seq_field_names = []
float_seq_field_dims = []
num_feature_field = 0
first_order_linear
embed_float_fields(float_fields)[source]

Embed the float feature columns

Parameters:

float_fields (torch.FloatTensor) – The input dense tensor. shape of [batch_size, num_float_field]

Returns:

The result embedding tensor of float columns.

Return type:

torch.FloatTensor

embed_float_seq_fields(float_seq_fields, mode='mean')[source]

Embed the float feature columns

Parameters:
  • float_seq_fields (torch.LongTensor) – The input tensor. shape of [batch_size, seq_len]

  • mode (str) – How to aggregate the embedding of feature in this field. default=mean

Returns:

The result embedding tensor of token sequence columns.

Return type:

torch.FloatTensor

embed_token_fields(token_fields)[source]

Embed the token feature columns

Parameters:

token_fields (torch.LongTensor) – The input tensor. shape of [batch_size, num_token_field]

Returns:

The result embedding tensor of token columns.

Return type:

torch.FloatTensor

embed_token_seq_fields(token_seq_fields, mode='mean')[source]

Embed the token feature columns

Parameters:
  • token_seq_fields (torch.LongTensor) – The input tensor. shape of [batch_size, seq_len]

  • mode (str) – How to aggregate the embedding of feature in this field. default=mean

Returns:

The result embedding tensor of token sequence columns.

Return type:

torch.FloatTensor

double_tower_embed_input_fields(interaction)[source]

Embed the whole feature columns in a double tower way.

Parameters:

interaction (Interaction) – The input data collection.

Returns:

The embedding tensor of token sequence columns in the first part. torch.FloatTensor: The embedding tensor of float sequence columns in the first part. torch.FloatTensor: The embedding tensor of token sequence columns in the second part. torch.FloatTensor: The embedding tensor of float sequence columns in the second part.

Return type:

torch.FloatTensor

concat_embed_input_fields(interaction)[source]
embed_input_fields(interaction)[source]

Embed the whole feature columns.

Parameters:

interaction (Interaction) – The input data collection.

Returns:

The embedding tensor of token sequence columns. torch.FloatTensor: The embedding tensor of float sequence columns.

Return type:

torch.FloatTensor