forked from alexysxeightn/MADE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_network.py
182 lines (129 loc) · 5.86 KB
/
my_network.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
import torch
import torch.nn as nn
import torch.optim as optim
import torchtext
from torchtext.datasets import TranslationDataset, Multi30k
from torchtext.data import Field, BucketIterator
import random
import math
import time
class Encoder(nn.Module):
def __init__(self, input_dim, emb_dim, hid_dim, n_layers, dropout):
super().__init__()
self.input_dim = input_dim
self.emb_dim = emb_dim
self.hid_dim = hid_dim
self.n_layers = n_layers
# self.dropout = dropout
self.embedding = nn.Embedding(
num_embeddings=input_dim,
embedding_dim=emb_dim
)
# <YOUR CODE HERE>
self.rnn = nn.LSTM(
input_size=emb_dim,
hidden_size=hid_dim,
num_layers=n_layers,
dropout=dropout
)
# <YOUR CODE HERE>
self.dropout = nn.Dropout(p=dropout)# <YOUR CODE HERE>
def forward(self, src):
#src = [src sent len, batch size]
# Compute an embedding from the src data and apply dropout to it
embedded = self.embedding(src)# <YOUR CODE HERE>
embedded = self.dropout(embedded)
output, (hidden, cell) = self.rnn(embedded)
#embedded = [src sent len, batch size, emb dim]
# Compute the RNN output values of the encoder RNN.
# outputs, hidden and cell should be initialized here. Refer to nn.LSTM docs ;)
# <YOUR CODE HERE>
#outputs = [src sent len, batch size, hid dim * n directions]
#hidden = [n layers * n directions, batch size, hid dim]
#cell = [n layers * n directions, batch size, hid dim]
#outputs are always from the top hidden layer
return hidden, cell
class Decoder(nn.Module):
def __init__(self, output_dim, emb_dim, hid_dim, n_layers, dropout):
super().__init__()
self.emb_dim = emb_dim
self.hid_dim = hid_dim
self.output_dim = output_dim
self.n_layers = n_layers
self.dropout = dropout
self.embedding = nn.Embedding(
num_embeddings=output_dim,
embedding_dim=emb_dim
)
# <YOUR CODE HERE>
self.rnn = nn.LSTM(
input_size=emb_dim,
hidden_size=hid_dim,
num_layers=n_layers,
dropout=dropout
)
# <YOUR CODE HERE>
self.out = nn.Linear(
in_features=hid_dim,
out_features=output_dim
)
# <YOUR CODE HERE>
self.dropout = nn.Dropout(p=dropout)# <YOUR CODE HERE>
def forward(self, input, hidden, cell):
#input = [batch size]
#hidden = [n layers * n directions, batch size, hid dim]
#cell = [n layers * n directions, batch size, hid dim]
#n directions in the decoder will both always be 1, therefore:
#hidden = [n layers, batch size, hid dim]
#context = [n layers, batch size, hid dim]
input = input.unsqueeze(0)
#input = [1, batch size]
# Compute an embedding from the input data and apply dropout to it
embedded = self.dropout(self.embedding(input))# <YOUR CODE HERE>
#embedded = [1, batch size, emb dim]
# Compute the RNN output values of the encoder RNN.
# outputs, hidden and cell should be initialized here. Refer to nn.LSTM docs ;)
# <YOUR CODE HERE>
#output = [sent len, batch size, hid dim * n directions]
#hidden = [n layers * n directions, batch size, hid dim]
#cell = [n layers * n directions, batch size, hid dim]
#sent len and n directions will always be 1 in the decoder, therefore:
#output = [1, batch size, hid dim]
#hidden = [n layers, batch size, hid dim]
#cell = [n layers, batch size, hid dim]
output, (hidden, cell) = self.rnn(embedded, (hidden, cell))
prediction = self.out(output.squeeze(0))
#prediction = [batch size, output dim]
return prediction, hidden, cell
class Seq2Seq(nn.Module):
def __init__(self, encoder, decoder, device):
super().__init__()
self.encoder = encoder
self.decoder = decoder
self.device = device
assert encoder.hid_dim == decoder.hid_dim, \
"Hidden dimensions of encoder and decoder must be equal!"
assert encoder.n_layers == decoder.n_layers, \
"Encoder and decoder must have equal number of layers!"
def forward(self, src, trg, teacher_forcing_ratio = 0.5):
#src = [src sent len, batch size]
#trg = [trg sent len, batch size]
#teacher_forcing_ratio is probability to use teacher forcing
#e.g. if teacher_forcing_ratio is 0.75 we use ground-truth inputs 75% of the time
# Again, now batch is the first dimention instead of zero
batch_size = trg.shape[1]
max_len = trg.shape[0]
trg_vocab_size = self.decoder.output_dim
#tensor to store decoder outputs
outputs = torch.zeros(max_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)
#first input to the decoder is the <sos> tokens
input = trg[0,:]
for t in range(1, max_len):
output, hidden, cell = self.decoder(input, hidden, cell)
outputs[t] = output
teacher_force = random.random() < teacher_forcing_ratio
top1 = output.max(1)[1]
input = (trg[t] if teacher_force else top1)
return outputs