-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstmDecoder.py
262 lines (162 loc) · 7.96 KB
/
lstmDecoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import torch
import torch.nn as nn
import torch.optim as optim
#from torchtext.legacy.datasets import Multi30k
#from torchtext.legacy.data import Field, BucketIterator
#import spacy
import numpy as np
import random
import math
import time
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class Encoder(nn.Module):
def __init__(self, input_dim, hid_dim, n_layers):
super().__init__()
self.rnn = nn.LSTM(input_dim, hid_dim, num_layers=n_layers, bidirectional=True, batch_first=True)#, dropout = dropout
def forward(self, x):
#The input to the encoder are the landmarks
#x = [batch size, sequence_len , 68*3]
#embedded = self.embedding(x.to(torch.int))
out, (hid, cell) = self.rnn(x)
#out = [batch size, src len, hid dim * n directions]
#hid = [n directions, batch size, hid dim]
#cell = [n directions, batch size, hid dim]
#print("ENCODER: hid.shape: ", hid.shape)
return hid, cell
class Decoder(nn.Module):
def __init__(self, hid_dim, n_layers, output_dim):
super().__init__()
self.output_dim = output_dim
self.hid_dim = hid_dim
#self.embedding = nn.Embedding(output_dim, emb_dim)
#put 1 instead of embed dim
self.rnn = nn.LSTM(input_size=1, hidden_size=hid_dim, bidirectional=True, batch_first=True, num_layers=n_layers)
self.fc_out = nn.Linear(2*hid_dim, output_dim)
def forward(self, input, hidden, cell):
#input = [batch size]
#hidden = [n directions, batch size, hid dim]
#input = [batch size, 1]
#embedded = self.embedding(input)
out, (hid, cell) = self.rnn(input.to(torch.float32), (hidden, cell))#input.to(torch.float32)
#out = [seq len, batch size, hid dim * n directions]
#hid = [n layers * n directions, batch size, hid dim]
pred = self.fc_out(out)#.squeeze(0))
#pred = [batch size, output dim]
return pred, hid, cell
class Seq2Seq(nn.Module):
def __init__(self, encoder, decoder, device):
super().__init__()
self.encoder = encoder
self.decoder = decoder
self.device = device
def forward(self, src, trg):
#src = [src len, batch size]
#trg = [trg len, batch size]
batch_size = trg.shape[0]
trg_len = src.shape[1]#FIXME Forse qui non ho passato i landmark con la dimensione del batch in testa
trg_vocab_size = self.decoder.output_dim
#tensor to store decoder outputs
outputs = torch.zeros(trg_len, batch_size, trg_vocab_size).to(self.device)
#last hidden state of the encoder is used as the initial hidden state of the decoder
hidden, cell = self.encoder(src)
#hidden = hidden.unsqueeze(1)
#first input to the decoder is the # token
input = torch.full((batch_size , 1 , 1), 1, dtype=torch.long).to(self.device)
for t in range(0, trg_len):
#insert input token embedding, previous hidden state
#receive output tensor (predictions) and new hidden
output, hidden, cell = self.decoder(input, hidden, cell)
#output = output.unsqueeze(0)#ADDED TO BE CHECKED
#place predictions in a tensor holding predictions for each token
outputs[t] = output[:, 0]
#get the highest predicted token from our predictions
top1 = output.argmax(-1, keepdim=True)
input = top1
return outputs.permute(1,0,2)
class only_Decoder_linear_emb(nn.Module):
def __init__(self, input_dim, hid_dim, n_layers, output_dim):
super().__init__()
self.output_dim = output_dim
self.hid_dim = hid_dim
self.fc_in = nn.Linear(input_dim, 256)
self.rnn = nn.LSTM(256, hid_dim, num_layers=n_layers, bidirectional=True, batch_first=True)#, dropout = dropout
self.fc_out = nn.Linear(2*hid_dim, output_dim)
def forward(self, input, len_):
#input = [batch size]
#hidden = [n directions*num_layers, batch size, hid dim]
#input = [batch size, 1]
x = self.fc_in(input.to(torch.float32))
output, _ = self.rnn(x)
#output = [seq len, batch size, hid dim * n directions]
#hidden = [n layers * n directions, batch size, hid dim]
prediction, (hidden, cell) = self.fc_out(output)
#prediction = self.tan(prediction)
#prediction = [batch size, output dim]
return prediction, hidden, cell
class only_Decoder(nn.Module):
def __init__(self, input_dim, hid_dim, n_layers, output_dim):
super().__init__()
self.output_dim = output_dim
self.hid_dim = hid_dim
self.rnn = nn.LSTM(input_dim, hid_dim, num_layers=n_layers, bidirectional=True, batch_first=True)#, dropout = dropout
self.fc_out = nn.Linear(2*hid_dim, output_dim)
#self.tan = nn.Tanh()
def forward(self, input, len_):
#input = [batch size]
#hidden = [n directions*num_layers, batch size, hid dim]
#input = [batch size, 1]
#packed_seq = nn.utils.rnn.pack_padded_sequence(input.permute(1,0,2), len_.to('cpu'), enforce_sorted=False)
#output, _ = self.rnn(packed_seq.to(torch.float32))
output, (hidden, cell) = self.rnn(input.to(torch.float32))
#outputs, _ = nn.utils.rnn.pad_packed_sequence(output)
#output = [seq len, batch size, hid dim * n directions]
#hidden = [n layers * n directions, batch size, hid dim]
prediction = self.fc_out(output)
#prediction = self.tan(prediction)
#prediction = [batch size, output dim]
return prediction, hidden, cell#, hidden
"""
def forward(self, x):
Arguments:
x: Tensor, shape ``[batch_size, seq_len, embedding_dim]``
x = x + self.pe[:x.size(1)]
return self.dropout(x)"""
class only_Decoder_MLP_emb(nn.Module):
def __init__(self, input_dim, hid_dim, n_layers, output_dim, path_emb):
super().__init__()
self.output_dim = output_dim
self.hid_dim = hid_dim
self.emb = MLP_emb(input_dim)#.to(device)
if path_emb != None:
#self.emb.load_state_dict(torch.load("./models/model_embexp6_trvalloss070.pt"))
self.emb.load_state_dict(torch.load(path_emb))
#+substitute input_dim with 768
self.rnn = nn.LSTM(768, hid_dim, num_layers=n_layers, bidirectional=True, batch_first=True)#, dropout = dropout
self.fc_out = nn.Linear(2*hid_dim, output_dim)
def forward(self, input, len_):
#input = [batch size]
#hidden = [n directions*num_layers, batch size, hid dim]
#input = [batch size, 1]
#Frozen because we expect a pretrained embedding
with torch.no_grad():
input = self.emb(input.to(torch.float32))
output, (hidden, cell) = self.rnn(input)#MODIFIED
#output = [seq len, batch size, hid dim * n directions]
#hidden = [n layers * n directions, batch size, hid dim]
prediction = self.fc_out(output)
#prediction = [batch size, output dim]
return prediction, hidden, cell#
class MLP_emb(nn.Module):
def __init__(self, input_dim):
super().__init__()
self.linear0 = nn.Linear(input_dim, 768)
self.linear1 = nn.Linear(768, 512)
self.linear2 = nn.Linear(512, 768)
self.actv = nn.ReLU()
def forward(self, x):
x = self.linear0(x)
x = self.actv(x)
x = self.linear1(x)
x = self.actv(x)
x = self.linear2(x)
return x