beta_rec.models package

beta_rec.models.cmn module

class beta_rec.models.cmn.CollaborativeMemoryNetwork(config, user_embeddings, item_embeddings, item_user_list, device)[source]

Bases: torch.nn.modules.module.Module

CollaborativeMemoryNetwork Class.

forward(input_users, input_items, input_items_negative, input_neighborhoods, input_neighborhood_lengths, input_neighborhoods_negative, input_neighborhood_lengths_negative, evaluation=False)[source]

Train the model.

output_module(input)[source]

Missing Doc.

predict(users, items)[source]

Predict result with the model.

class beta_rec.models.cmn.cmnEngine(config, user_embeddings, item_embeddings, item_user_list)[source]

Bases: beta_rec.models.torch_engine.ModelEngine

CMN Engine.

bpr_loss(pos_score, neg_score)[source]

Calculate BPR loss.

train_an_epoch(train_loader, epoch_id)[source]

Train the model in one epoch.

Parameters:
  • epoch_id (int) – the number of epoch.
  • train_loader (function) – user, pos_items and neg_items generator.
train_single_batch(batch_data)[source]

Train a single batch data.

Train a single batch data.

Parameters:batch_data (list) – batch users, positive items and negative items.
Returns:batch loss.
Return type:loss (float)

beta_rec.models.gmf module

class beta_rec.models.gmf.GMF(config)[source]

Bases: torch.nn.modules.module.Module

GMF Class.

forward(user_indices, item_indices)[source]

Train the model.

init_weight()[source]

Initialize weights.

predict(user_indices, item_indices)[source]

Predict result with the model.

class beta_rec.models.gmf.GMFEngine(config)[source]

Bases: beta_rec.models.torch_engine.ModelEngine

Engine for training & evaluating GMF model.

train_an_epoch(train_loader, epoch_id)[source]

Train the model in one epoch.

Parameters:
  • epoch_id (int) – the number of epoch.
  • train_loader (function) – user, pos_items and neg_items generator.
train_single_batch(users, items, ratings)[source]

Train the model in a single batch.

Parameters:batch_data (list) – batch users, positive items and negative items.
Returns:batch loss.
Return type:loss (float)

beta_rec.models.lightgcn module

class beta_rec.models.lightgcn.LightGCN(config, norm_adj)[source]

Bases: torch.nn.modules.module.Module

Model initialisation, embedding generation and prediction of NGCF.

dropout(x, keep_prob)[source]

Drop out some layers.

forward(norm_adj)[source]

Train GNN on users and item embeddings.

Parameters:norm_adj (torch sparse tensor) – the norm adjacent matrix of the user-item interaction matrix.
Returns:processed user embeddings. i_g_embeddings (tensor): processed item embeddings.
Return type:u_g_embeddings (tensor)
init_emb()[source]

Initialize users and items’ embeddings.

predict(users, items)[source]

Predict result with the model.

Parameters:
  • users (int, or list of int) – user id.
  • items (int, or list of int) – item id.
Returns:

dot product.

Return type:

scores (int)

class beta_rec.models.lightgcn.LightGCNEngine(config)[source]

Bases: beta_rec.models.torch_engine.ModelEngine

LightGCNEngine Class.

loss_comput(usersE, pos_itemsE, neg_itemsE, users, pos_item, neg_item)[source]

Calculate BPR loss.

train_an_epoch(train_loader, epoch_id)[source]

Train the model in one epoch.

Parameters:
  • epoch_id (int) – the number of epoch.
  • train_loader (function) – user, pos_items and neg_items generator.
train_single_batch(batch_data)[source]

Train the model in a single batch.

Parameters:batch_data (list) – batch users, positive items and negative items.
Returns:batch loss.
Return type:loss (float)

beta_rec.models.mf module

class beta_rec.models.mf.MF(config)[source]

Bases: torch.nn.modules.module.Module

A pytorch Module for Matrix Factorization.

forward(batch_data)[source]

Trian the model.

Parameters:batch_data – tuple consists of (users, pos_items, neg_items), which must be LongTensor.
predict(users, items)[source]

Predcit result with the model.

Parameters:
  • users (int, or list of int) – user id(s).
  • items (int, or list of int) – item id(s).
Returns:

predicted scores of these user-item pairs.

Return type:

scores (int, or list of int)

class beta_rec.models.mf.MFEngine(config)[source]

Bases: beta_rec.models.torch_engine.ModelEngine

MFEngine Class.

train_an_epoch(train_loader, epoch_id)[source]

Train a epoch, generate batch_data from data_loader, and call train_single_batch.

Parameters:
  • train_loader (DataLoader) –
  • epoch_id (int) – the number of epoch.
train_single_batch(batch_data)[source]

Train a single batch.

Parameters:batch_data (list) – batch users, positive items and negative items.
Returns:batch loss.
Return type:loss (float)

beta_rec.models.mlp module

class beta_rec.models.mlp.MLP(config)[source]

Bases: torch.nn.modules.module.Module

MLP Class.

forward(user_indices, item_indices)[source]

Train the model.

init_weight()[source]

Initialize weight in the model.

predict(user_indices, item_indices)[source]

Predict result with the model.

class beta_rec.models.mlp.MLPEngine(config)[source]

Bases: beta_rec.models.torch_engine.ModelEngine

Engine for training & evaluating GMF model.

train_an_epoch(train_loader, epoch_id)[source]

Train the model in one epoch.

Parameters:
  • epoch_id (int) – the number of epoch.
  • train_loader (function) – user, pos_items and neg_items generator.
train_single_batch(users, items, ratings)[source]

Train the model in a single batch.

Parameters:batch_data (list) – batch users, positive items and negative items.
Returns:batch loss.
Return type:loss (float)

beta_rec.models.narm module

class beta_rec.models.narm.NARM(config)[source]

Bases: torch.nn.modules.module.Module

Neural Attentive Session Based Recommendation Model Class.

Parameters:
  • n_items (int) – the number of items.
  • hidden_size (int) – the hidden size of gru.
  • embedding_dim (int) – the dimension of item embedding.
  • batch_size (int) –
  • n_layers (int) – the number of gru layers.
forward(seq, lengths)[source]

Train the model.

init_hidden(batch_size)[source]

Initialize hidden layers.

class beta_rec.models.narm.NARMEngine(config)[source]

Bases: beta_rec.models.torch_engine.ModelEngine

Engine for training & evaluating NARM model.

static get_recommendation_confidence_list(recommendation)[source]

Missing Doc.

static get_recommendation_list(recommendation)[source]

Missing Doc.

predict(user_profile, batch=1)[source]

Predict the next item given user profile.

Parameters:
  • user_profile (List) – Contains the item IDs of the events.
  • batch (int) – Prediction batch size.
Returns:

Prediction scores for selected items for every event of the batch.

Return type:

preds (List)

recommend(user_profile, user_id=None)[source]

Make a recommendation.

Parameters:
  • user_profile (List) – Contains the item IDs of the events.
  • user_id (None) – users’ id for personalised recommenation.
Returns:

item and score pairs.

Return type:

List

train_an_epoch(train_loader, epoch)[source]

Train the model in one epoch.

Parameters:
  • epoch_id (int) – the number of epoch.
  • train_loader (function) – user, pos_items and neg_items generator.

beta_rec.models.ncf module

class beta_rec.models.ncf.NeuMF(config)[source]

Bases: torch.nn.modules.module.Module

NeuMF Class.

forward(user_indices, item_indices)[source]

Train the model.

init_weight()[source]

Initialize weight in the model.

predict(user_indices, item_indices)[source]

Predict the result with the model.

class beta_rec.models.ncf.NeuMFEngine(config)[source]

Bases: beta_rec.models.torch_engine.ModelEngine

Engine for training & evaluating GMF model.

init_weights()[source]

Initialize weights in the model.

load_pretrain_weights()[source]

Load weights from trained MLP model & GMF model.

train_an_epoch(train_loader, epoch_id)[source]

Train the model in one epoch.

Parameters:
  • epoch_id (int) – the number of epoch.
  • train_loader (function) – user, pos_items and neg_items generator.
train_single_batch(users, items, ratings)[source]

Train the model in a single batch.

Parameters:batch_data (list) – batch users, positive items and negative items.
Returns:batch loss.
Return type:loss (float)

beta_rec.models.ngcf module

class beta_rec.models.ngcf.NGCF(config, norm_adj)[source]

Bases: torch.nn.modules.module.Module

Model initialisation, embedding generation and prediction of NGCF.

forward(norm_adj)[source]

Perform GNN function on users and item embeddings.

Parameters:norm_adj (torch sparse tensor) – the norm adjacent matrix of the user-item interaction matrix.
Returns:processed user embeddings. i_g_embeddings (tensor): processed item embeddings.
Return type:u_g_embeddings (tensor)
init_emb()[source]

Initialize users and itmes’ embeddings.

predict(users, items)[source]

Predict result with the model.

Parameters:
  • users (int, or list of int) – user id.
  • items (int, or list of int) – item id.
Returns:

dot product.

Return type:

scores (int)

class beta_rec.models.ngcf.NGCFEngine(config)[source]

Bases: beta_rec.models.torch_engine.ModelEngine

NGCFEngine Class.

bpr_loss(users, pos_items, neg_items)[source]

Bayesian Personalised Ranking (BPR) pairwise loss function.

Note that the sizes of pos_scores and neg_scores should be equal.

Parameters:
  • pos_scores (tensor) – Tensor containing predictions for known positive items.
  • neg_scores (tensor) – Tensor containing predictions for sampled negative items.
Returns:

loss.

train_an_epoch(train_loader, epoch_id)[source]

Train the model in one epoch.

Parameters:
  • epoch_id (int) – the number of epoch.
  • train_loader (function) – user, pos_items and neg_items generator.
train_single_batch(batch_data)[source]

Train the model in a single batch.

Parameters:batch_data (list) – batch users, positive items and negative items.
Returns:batch loss.
Return type:loss (float)

beta_rec.models.pairwise_ngcf module

beta_rec.models.torch_engine module

class beta_rec.models.torch_engine.ModelEngine(config)[source]

Bases: object

Meta Engine for training & evaluating NCF model.

Note: Subclass should implement self.model!

bce_loss(scores, ratings)[source]

Binary Cross-Entropy (BCE) pointwise loss, also known as log loss or logistic loss.

Parameters:
  • scores (tensor) – Tensor containing predictions for both positive and negative items.
  • ratings (tensor) – Tensor containing ratings for both positive and negative items.
Returns:

loss.

bpr_loss(pos_scores, neg_scores)[source]

Bayesian Personalised Ranking (BPR) pairwise loss function.

Note that the sizes of pos_scores and neg_scores should be equal.

Parameters:
  • pos_scores (tensor) – Tensor containing predictions for known positive items.
  • neg_scores (tensor) – Tensor containing predictions for sampled negative items.
Returns:

loss.

resume_checkpoint(model_dir, model=None)[source]

Resume model with checkpoint.

save_checkpoint(model_dir)[source]

Save checkpoint.

set_device()[source]

Set device.

set_optimizer()[source]

Set optimizer in the model.

train_an_epoch(train_loader, epoch_id)[source]

Train the model in one epoch.

train_single_batch(batch_data, ratings=None)[source]

Train the model in a single batch.

beta_rec.models.triple2vec module

class beta_rec.models.triple2vec.Triple2vec(config)[source]

Bases: torch.nn.modules.module.Module

Triple2vec Class.

forward(batch_data)[source]

Train the model.

init_emb()[source]

Initialize embeddings.

predict(users, items)[source]

Predict result with the model.

class beta_rec.models.triple2vec.Triple2vecEngine(config)[source]

Bases: beta_rec.models.torch_engine.ModelEngine

Engine for training Triple model.

train_an_epoch(train_loader, epoch_id)[source]

Train the model in one epoch.

train_single_batch(batch_data, ratings=None)[source]

Train the model in a single batch.

beta_rec.models.vbcar module

class beta_rec.models.vbcar.VBCAR(config)[source]

Bases: torch.nn.modules.module.Module

VBCAR Class.

forward(batch_data)[source]

Train the model.

init_feature(user_fea, item_fea)[source]

Initialize features.

init_layers()[source]

Initialize layers in the model.

item_encode(index)[source]

Encode item.

kl_div(dis1, dis2=None, neg=False)[source]

Missing Doc.

predict(users, items)[source]

Predict result with the model.

reparameterize(gaussian)[source]

Re-parameterize the model.

user_encode(index)[source]

Encode user.

class beta_rec.models.vbcar.VBCAREngine(config)[source]

Bases: beta_rec.models.torch_engine.ModelEngine

Engine for training & evaluating GMF model.

train_an_epoch(train_loader, epoch_id)[source]

Train the model in one epoch.

train_single_batch(batch_data, ratings=None)[source]

Train the model in a single batch.

beta_rec.models.vlml module

class beta_rec.models.vlml.VariableLengthMemoryLayer(hops, emb_dim, device)[source]

Bases: torch.nn.modules.module.Module

VariableLengthMemoryLayer Class.

apply_attention_memory(memory, output_memory, query, memory_mask=None, maxlen=None)[source]

Apply attention memory.

:param : param memory: [batch size, max length, embedding size], typically Matrix M. :param : param output_memory: [batch size, max length, embedding size], typically Matrix C. :param : param query: [batch size, embed size], typically u. :param : param memory_mask: [batch size] dim Tensor, the length of each sequence if variable length. :param : param maxlen: int/Tensor, the maximum sequence padding length; if None it infers based on the max of

memory_mask.
:param : returns: AttentionOutput

output: [batch size, embedding size]. weight: [batch size, max length], the attention weights applied to

the output representation.
forward(query, memory, output_memory, seq_length, maxlen=32)[source]

Train the model.

mask_mod(inputs, mask_length, maxlen=None)[source]

Use a memory mask.

Apply a memory mask such that the values we mask result in being the minimum possible value we can represent with a float32.

Parameters:
  • inputs – [batch size, length], dtype=tf.float32.
  • memory_mask – [batch_size] shape Tensor of ints indicating the length of inputs.
  • maxlen – Sets the maximum length of the sequence; if None, inferred from inputs.
Returns:

[batch size, length] dim Tensor with the mask applied.

Module contents

Models Module.