crslab.model.recommendation.sasrec package

Submodules

class crslab.model.recommendation.sasrec.modules.Embeddings(item_size, hidden_size, max_seq_length, hidden_dropout_prob)[source]

Bases: torch.nn.modules.module.Module

Construct the embeddings from item, position, attribute.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(input_ids)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class crslab.model.recommendation.sasrec.modules.Encoder(num_hidden_layers, hidden_size, num_attention_heads, hidden_dropout_prob, hidden_act, attention_probs_dropout_prob)[source]

Bases: torch.nn.modules.module.Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(hidden_states, attention_mask, output_all_encoded_layers=True)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class crslab.model.recommendation.sasrec.modules.Intermediate(hidden_size, hidden_act, hidden_dropout_prob)[source]

Bases: torch.nn.modules.module.Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(input_tensor)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class crslab.model.recommendation.sasrec.modules.Layer(hidden_size, num_attention_heads, hidden_dropout_prob, hidden_act, attention_probs_dropout_prob)[source]

Bases: torch.nn.modules.module.Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(hidden_states, attention_mask)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class crslab.model.recommendation.sasrec.modules.LayerNorm(hidden_size, eps=1e-12)[source]

Bases: torch.nn.modules.module.Module

Construct a layernorm module in the TF style (epsilon inside the square root).

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class crslab.model.recommendation.sasrec.modules.SASRec(hidden_dropout_prob, device, initializer_range, hidden_size, max_seq_length, item_size, num_attention_heads, attention_probs_dropout_prob, hidden_act, num_hidden_layers)[source]

Bases: torch.nn.modules.module.Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

build_model()[source]
compute_loss(y_pred, y, subset='test')[source]
cross_entropy(seq_out, pos_ids, neg_ids)[source]
forward(input_ids, attention_mask=None, output_all_encoded_layers=True)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

init_model()[source]
init_sas_weights(module)[source]

Initialize the weights.

load_model(path)[source]
save_model(file_name)[source]
class crslab.model.recommendation.sasrec.modules.SelfAttention(hidden_size, num_attention_heads, hidden_dropout_prob, attention_probs_dropout_prob)[source]

Bases: torch.nn.modules.module.Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(input_tensor, attention_mask)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

transpose_for_scores(x)[source]
Parameters

x – (bs, seq_len, all_head_size)

Returns

x.permute(0, 2, 1, 3), (bs, num_heads, seq_len, head_size)

crslab.model.recommendation.sasrec.modules.gelu(x)[source]

Implementation of the gelu activation function.

For information: OpenAI GPT’s gelu is slightly different (and gives slightly different results): 0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))) Also see https://arxiv.org/abs/1606.08415

crslab.model.recommendation.sasrec.modules.swish(x)[source]

SASREC

References

Kang, Wang-Cheng, and Julian McAuley. “Self-attentive sequential recommendation.” in ICDM 2018.

class crslab.model.recommendation.sasrec.sasrec.SASRECModel(opt, device, vocab, side_data)[source]

Bases: crslab.model.base.BaseModel

hidden_dropout_prob

A float indicating the dropout rate to dropout hidden state in SASRec.

initializer_range

A float indicating the range of parameters initiation in SASRec.

hidden_size

A integer indicating the size of hidden state in SASRec.

max_seq_length

A integer indicating the max interaction history length.

item_size

A integer indicating the number of items.

num_attention_heads

A integer indicating the head number in SASRec.

attention_probs_dropout_prob

A float indicating the dropout rate in attention layers.

hidden_act

A string indicating the activation function type in SASRec.

num_hidden_layers

A integer indicating the number of hidden layers in SASRec.

Parameters
  • opt (dict) – A dictionary record the hyper parameters.

  • device (torch.device) – A variable indicating which device to place the data and model.

  • vocab (dict) – A dictionary record the vocabulary information.

  • side_data (dict) – A dictionary record the side data.

build_model()[source]

build model

forward(batch, mode)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Module contents