hopwise.model.sequential_recommender.bert4rec

Reference:

Fei Sun et al. “BERT4Rec: Sequential Recommendation with Bidirectional Encoder Representations from Transformer.” In CIKM 2019.

Reference code:

The authors’ tensorflow implementation https://github.com/FeiSun/BERT4Rec

Classes

BERT4Rec

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

Module Contents

class hopwise.model.sequential_recommender.bert4rec.BERT4Rec(config, dataset)

Bases: hopwise.model.abstract_recommender.SequentialRecommender

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

n_layers
n_heads
hidden_size
inner_size
hidden_dropout_prob
attn_dropout_prob
hidden_act
layer_norm_eps
mask_ratio
MASK_ITEM_SEQ
POS_ITEMS
NEG_ITEMS
MASK_INDEX
loss_type
initializer_range
mask_token
mask_item_length
item_embedding
position_embedding
trm_encoder
LayerNorm
dropout
output_ffn
output_gelu
output_ln
output_bias
_init_weights(module)

Initialize the weights

reconstruct_test_data(item_seq, item_seq_len)

Add mask token at the last position according to the lengths of item_seq

forward(item_seq)
multi_hot_embed(masked_index, max_length)

For memory, we only need calculate loss for masked position. Generate a multi-hot vector to indicate the masked position for masked sequence, and then is used for gathering the masked position hidden representation.

Examples

sequence: [1 2 3 4 5]

masked_sequence: [1 mask 3 mask 5]

masked_index: [1, 3]

max_length: 5

multi_hot_embed: [[0 1 0 0 0], [0 0 0 1 0]]

calculate_loss(interaction)

Calculate the training loss for a batch data.

Parameters:

interaction (Interaction) – Interaction class of the batch.

Returns:

Training loss, shape: []

Return type:

torch.Tensor

predict(interaction)

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)

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