|
| 1 | +from typing import Union |
| 2 | + |
| 3 | +import torch |
| 4 | +import torch.nn as nn |
| 5 | + |
| 6 | + |
| 7 | +class embedding_cat_variables(nn.Module): |
| 8 | + # at the moment cat_past and cat_fut together |
| 9 | + def __init__(self, seq_len: int, lag: int, d_model: int, emb_dims: list, device): |
| 10 | + """Class for embedding categorical variables, adding 3 positional variables during forward |
| 11 | +
|
| 12 | + Parameters |
| 13 | + ---------- |
| 14 | + seq_len: int |
| 15 | + length of the sequence (sum of past and future steps) |
| 16 | + lag: (int): |
| 17 | + number of future step to be predicted |
| 18 | + hiden_size: int |
| 19 | + dimension of all variables after they are embedded |
| 20 | + emb_dims: list |
| 21 | + size of the dictionary for embedding. One dimension for each categorical variable |
| 22 | + device : torch.device |
| 23 | + """ # noqa: E501 |
| 24 | + super().__init__() |
| 25 | + self.seq_len = seq_len |
| 26 | + self.lag = lag |
| 27 | + self.device = device |
| 28 | + self.cat_embeds = emb_dims + [seq_len, lag + 1, 2] # |
| 29 | + self.cat_n_embd = nn.ModuleList( |
| 30 | + [nn.Embedding(emb_dim, d_model) for emb_dim in self.cat_embeds] |
| 31 | + ) |
| 32 | + |
| 33 | + def forward( |
| 34 | + self, x: Union[torch.Tensor, int], device: torch.device |
| 35 | + ) -> torch.Tensor: |
| 36 | + """All components of x are concatenated with 3 new variables for data augmentation, in the order: |
| 37 | +
|
| 38 | + - pos_seq: assign at each step its time-position |
| 39 | + - pos_fut: assign at each step its future position. 0 if it is a past step |
| 40 | + - is_fut: explicit for each step if it is a future(1) or past one(0) |
| 41 | +
|
| 42 | + Parameters |
| 43 | + ---------- |
| 44 | + x: torch.Tensor |
| 45 | + `[bs, seq_len, num_vars]` |
| 46 | +
|
| 47 | + Returns |
| 48 | + ------ |
| 49 | + torch.Tensor: |
| 50 | + `[bs, seq_len, num_vars+3, n_embd]` |
| 51 | + """ # noqa: E501 |
| 52 | + if isinstance(x, int): |
| 53 | + no_emb = True |
| 54 | + B = x |
| 55 | + else: |
| 56 | + no_emb = False |
| 57 | + B, _, _ = x.shape |
| 58 | + |
| 59 | + pos_seq = self.get_pos_seq(bs=B).to(device) |
| 60 | + pos_fut = self.get_pos_fut(bs=B).to(device) |
| 61 | + is_fut = self.get_is_fut(bs=B).to(device) |
| 62 | + |
| 63 | + if no_emb: |
| 64 | + cat_vars = torch.cat((pos_seq, pos_fut, is_fut), dim=2) |
| 65 | + else: |
| 66 | + cat_vars = torch.cat((x, pos_seq, pos_fut, is_fut), dim=2) |
| 67 | + cat_vars = cat_vars.long() |
| 68 | + cat_n_embd = self.get_cat_n_embd(cat_vars) |
| 69 | + return cat_n_embd |
| 70 | + |
| 71 | + def get_pos_seq(self, bs): |
| 72 | + pos_seq = torch.arange(0, self.seq_len) |
| 73 | + pos_seq = pos_seq.repeat(bs, 1).unsqueeze(2).to(self.device) |
| 74 | + return pos_seq |
| 75 | + |
| 76 | + def get_pos_fut(self, bs): |
| 77 | + pos_fut = torch.cat( |
| 78 | + ( |
| 79 | + torch.zeros((self.seq_len - self.lag), dtype=torch.long), |
| 80 | + torch.arange(1, self.lag + 1), |
| 81 | + ) |
| 82 | + ) |
| 83 | + pos_fut = pos_fut.repeat(bs, 1).unsqueeze(2).to(self.device) |
| 84 | + return pos_fut |
| 85 | + |
| 86 | + def get_is_fut(self, bs): |
| 87 | + is_fut = torch.cat( |
| 88 | + ( |
| 89 | + torch.zeros((self.seq_len - self.lag), dtype=torch.long), |
| 90 | + torch.ones((self.lag), dtype=torch.long), |
| 91 | + ) |
| 92 | + ) |
| 93 | + is_fut = is_fut.repeat(bs, 1).unsqueeze(2).to(self.device) |
| 94 | + return is_fut |
| 95 | + |
| 96 | + def get_cat_n_embd(self, cat_vars): |
| 97 | + cat_n_embd = torch.Tensor().to(cat_vars.device) |
| 98 | + for index, layer in enumerate(self.cat_n_embd): |
| 99 | + emb = layer(cat_vars[:, :, index]) |
| 100 | + cat_n_embd = torch.cat((cat_n_embd, emb.unsqueeze(2)), dim=2) |
| 101 | + return cat_n_embd |
0 commit comments