Download annotations/annotation_34/ELCRec/src/models.py from mcy98/RECODE-H: direct link, hf CLI and curl.
- Browser
- Download file 4.56 kB
-
https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_34/ELCRec/src/models.py
- Command line
-
hf download hf://datasets/mcy98/RECODE-H/annotations/annotation_34/ELCRec/src/models.py
-
curl -L -o models.py https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_34/ELCRec/src/models.py
4.56 kB
| import torch | |
| import faiss | |
| import torch.nn as nn | |
| from modules import Encoder, LayerNorm | |
| class KMeans(object): | |
| def __init__(self, num_cluster, seed, hidden_size, gpu_id=0, device="cpu"): | |
| """ | |
| Args: | |
| k: number of clusters | |
| """ | |
| self.seed = seed | |
| self.num_cluster = num_cluster | |
| self.max_points_per_centroid = 4096 | |
| self.min_points_per_centroid = 0 | |
| self.gpu_id = 0 | |
| self.device = device | |
| self.first_batch = True | |
| self.hidden_size = hidden_size | |
| self.clus, self.index = self.__init_cluster(self.hidden_size) | |
| self.centroids = [] | |
| def __init_cluster( | |
| self, hidden_size, verbose=False, niter=20, nredo=5, max_points_per_centroid=4096, min_points_per_centroid=0 | |
| ): | |
| print(" cluster train iterations:", niter) | |
| clus = faiss.Clustering(hidden_size, self.num_cluster) | |
| clus.verbose = verbose | |
| clus.niter = niter | |
| clus.nredo = nredo | |
| clus.seed = self.seed | |
| clus.max_points_per_centroid = max_points_per_centroid | |
| clus.min_points_per_centroid = min_points_per_centroid | |
| index = faiss.IndexFlatL2(hidden_size) | |
| return clus, index | |
| def train(self, x): | |
| if x.shape[0] > self.num_cluster: | |
| self.clus.train(x, self.index) | |
| centroids = faiss.vector_to_array(self.clus.centroids).reshape(self.num_cluster, self.hidden_size) | |
| centroids = torch.tensor(centroids, requires_grad=True).to(self.device) | |
| self.centroids = nn.functional.normalize(centroids, p=2, dim=1) | |
| def query(self, x): | |
| D, I = self.index.search(x, 1) | |
| seq2cluster = [int(n[0]) for n in I] | |
| seq2cluster = torch.LongTensor(seq2cluster).to(self.device) | |
| return seq2cluster, self.centroids[seq2cluster] | |
| class SASRecModel(nn.Module): | |
| def __init__(self, args): | |
| super(SASRecModel, self).__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 = Encoder(args) | |
| self.LayerNorm = LayerNorm(args.hidden_size, eps=1e-12) | |
| self.dropout = nn.Dropout(args.hidden_dropout_prob) | |
| self.args = args | |
| self.cluster_centers = nn.Parameter(torch.Tensor(int(args.num_intent_clusters), int(args.hidden_size))) | |
| self.criterion = nn.BCELoss(reduction="none") | |
| self.apply(self.init_weights) | |
| def add_position_embedding(self, sequence): | |
| 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 forward(self, 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 | |
| sequence_emb = self.add_position_embedding(input_ids) | |
| item_encoded_layers = self.item_encoder(sequence_emb, extended_attention_mask, output_all_encoded_layers=True) | |
| sequence_output = item_encoded_layers[-1] | |
| return sequence_output | |
| def init_weights(self, module): | |
| if isinstance(module, (nn.Linear, nn.Embedding)): | |
| 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_() | |