Deep Learning RNN sequence model

Take down the note when I came accross the bugs during doing the lab 7.3.

The key words:
pack padded sequence, pad packed sequence, the output of lstm model.

The code is listed below:

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
class ImprovedRNN(nn.Module):
def __init__(self, input_dim, embedding_dim, hidden_dim, output_dim):
super().__init__()

self.embedding = nn.Embedding(input_dim, embedding_dim)
# YOUR CODE HERE
self.lstm=nn.LSTM(embedding_dim,hidden_dim,batch_first=True)
# raise NotImplementedError()
self.fc = nn.Linear(hidden_dim, output_dim)

def forward(self, text_len):
text, lengths = text_len
embedded = self.embedding(text)
# print(embedded.data.size()) # (sentence length, batch_size,hidden_dim)
# print(lengths) # the tensor(size 64), contains the length of sizes
embedded = nn.utils.rnn.pack_padded_sequence(embedded, lengths)
# print(embedded.data.size()) # the packed_sequence record the series data and the tensor recording each length

# YOUR CODE HERE
# print(embedded[0].size())
_,(last_state,_)=self.lstm(embedded)
# lstm_out_pad,length_sentence=nn.utils.rnn.pad_packed_sequence(lstm_out)
print(last_state.size())
# lstm_final_out=lstm_out_pad[length_sentence-1] # just use the final timestep output
# lstm_out_pad [0] is the data and [1] records the length of each sentence
# print("...",lstm_out.data.size(),"\n ssss",type(lstm_out.data))
# print("\n haha...",lstm_out_pad)
# print(lstm_out_pad[0][-1][63])
# length_63=lstm_out_pad[-1][63] # Use [-1] can't get real last one
# print("I'm the length of first sentence:",length_63)
# print("i'm the data:",lstm_out_pad[0][length_63-1],lstm_out_pad[0][length_63 - 1][63])
# print("im the length list:",lstm_out_pad[-1])
# print(lstm_final_out.size())
out=self.fc(lstm_final_out)
# print("emm heng?")
return out
# raise NotImplementedError()

INPUT_DIM = len(TEXT.vocab) # 25002
EMBEDDING_DIM = 50
HIDDEN_DIM = 100
OUTPUT_DIM = 1

imodel = ImprovedRNN(INPUT_DIM, EMBEDDING_DIM, HIDDEN_DIM, OUTPUT_DIM)

# TODO: Train and evaluate the model
# YOUR CODE HERE
optimizer = optim.Adam(model.parameters(), lr=0.01)

torchbearer_trial = Trial(imodel, optimizer, criterion, metrics=['acc', 'loss']).to(device)
torchbearer_trial.with_generators(train_generator=MyIter(train_iterator), val_generator=MyIter(valid_iterator), test_generator=MyIter(test_iterator))
# torchbearer_trial.with_train_generator(MyIter(train_iterator))
torchbearer_trial.run(epochs=5)
torchbearer_trial.predict()