Download annotations/annotation_20/DEALRec/code/prune/models.py from mcy98/RECODE-H: direct link, hf CLI and curl.
- Browser
- Download file 3.45 kB
-
https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_20/DEALRec/code/prune/models.py
- Command line
-
hf download hf://datasets/mcy98/RECODE-H/annotations/annotation_20/DEALRec/code/prune/models.py
-
curl -L -o models.py https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_20/DEALRec/code/prune/models.py
3.45 kB
| import numpy as np | |
| import torch | |
| import torch.nn as nn | |
| import ipdb | |
| from modules import SASEncoder, LayerNorm | |
| torch.set_printoptions(profile="full") | |
| class SASRecModel(nn.Module): | |
| """ | |
| Class implementing SASRec model | |
| """ | |
| def __init__(self, args): | |
| super().__init__() | |
| self.item_embeddings = nn.Embedding(args.item_size, args.hidden_size, padding_idx=0) | |
| self.position_embeddings = nn.Embedding(args.max_seq_length, args.hidden_size) | |
| self.item_encoder = SASEncoder(args) | |
| self.LayerNorm = LayerNorm(args.hidden_size, eps=1e-12) | |
| self.dropout = nn.Dropout(args.hidden_dropout_prob) | |
| self.args = args | |
| self.criterion = nn.BCELoss(reduction='none') | |
| self.apply(self.__init_weights) | |
| def _add_position_embedding(self, | |
| sequence: torch.Tensor) -> torch.Tensor: | |
| seq_length = sequence.size(1) | |
| position_ids = torch.arange(seq_length, dtype=torch.long, device=sequence.device) | |
| position_ids = position_ids.unsqueeze(0).expand_as(sequence) | |
| item_embeddings = self.item_embeddings(sequence) | |
| position_embeddings = self.position_embeddings(position_ids) | |
| sequence_emb = item_embeddings + position_embeddings | |
| sequence_emb = self.LayerNorm(sequence_emb) | |
| sequence_emb = self.dropout(sequence_emb) | |
| return sequence_emb | |
| def _get_embedding_and_mask(self, input_ids): | |
| sequence_emb = self._add_position_embedding(input_ids) | |
| attention_mask = (input_ids > 0).long() | |
| extended_attention_mask = attention_mask.unsqueeze(1).unsqueeze(2) # torch.int64 | |
| max_len = attention_mask.size(-1) | |
| attn_shape = (1, max_len, max_len) | |
| subsequent_mask = torch.triu(torch.ones(attn_shape), diagonal=1) # torch.uint8 | |
| subsequent_mask = (subsequent_mask == 0).unsqueeze(1) | |
| subsequent_mask = subsequent_mask.long() | |
| if self.args.cuda_condition: | |
| subsequent_mask = subsequent_mask.cuda() | |
| extended_attention_mask = extended_attention_mask * subsequent_mask | |
| extended_attention_mask = extended_attention_mask.to(dtype=next(self.parameters()).dtype) # fp16 compatibility | |
| extended_attention_mask = (1.0 - extended_attention_mask) * -10000.0 | |
| return sequence_emb, extended_attention_mask | |
| def __init_weights(self, module): | |
| """ | |
| Method to initialize the weights. | |
| """ | |
| if isinstance(module, (nn.Linear, nn.Embedding)): | |
| # Slightly different from the TF version which uses truncated_normal for initialization | |
| # cf https://github.com/pytorch/pytorch/pull/5617 | |
| module.weight.data.normal_(mean=0.0, std=self.args.initializer_range) | |
| elif isinstance(module, LayerNorm): | |
| module.bias.data.zero_() | |
| module.weight.data.fill_(1.0) | |
| if isinstance(module, nn.Linear) and module.bias is not None: | |
| module.bias.data.zero_() | |
| def finetune(self, input_ids): | |
| """ | |
| Method to fine-tune the pre-trained model | |
| :param input_ids: | |
| :return: | |
| """ | |
| sequence_emb, extended_attention_mask = self._get_embedding_and_mask(input_ids) | |
| sequence_output = self.item_encoder(sequence_emb, | |
| extended_attention_mask, | |
| output_all_encoded_layers=False) | |
| return sequence_output |