-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
272 lines (230 loc) · 9.34 KB
/
generator.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
263
264
265
266
267
268
269
270
271
272
class EncoderRNN(nn.Module):
def __init__(self, feature_size, hidden_size, output_length, layer = 1):
super(EncoderRNN, self).__init__()
'''
init parameters:
feature_size: the feature number of the embedding layer
hidden_size: the hidden size of GRU(usually equal to feature size)
output_length: the number of total dictionary
layer: layer number of RNN
'''
self.hidden_size = hidden_size
self.embedding = nn.Embedding(output_length, feature_size, padding_idx = PAD_token)
self.gru = nn.GRU(feature_size, hidden_size, num_layers = layer)
self.gru.flatten_parameters()
self.layer = layer
def forward(self, input, hidden):
'''
input:
input: one batch of word(changed by dictionary)
type: pytorch variable LongTensor
size: (1, batch_size)
hidden: hidden state of the RNN
type: pytorch variable FloatTensor
size: (layer_number, batch_size, hidden_size)
output:
output: output of RNN unit
type: pytorch variable FloatTensor
size: (layer_number, batch_size, feature_size)
hidden: hidden state of RNN unit
type: pytorch variable FloatTensor
size: (layer_number, batch_size, feature_size)
'''
embedded = self.embedding(input.view(1, -1))
output = embedded
output, hidden = self.gru(output, hidden)
return output, hidden
def initHidden(self, batch_size):
result = Variable(torch.zeros(self.layer, batch_size, self.hidden_size))
if use_cuda:
return result.cuda()
else:
return result
MAX_LENGTH = 150
class DecoderRNN(nn.Module):
def __init__(self, feature_size, hidden_size, output_length, layer = 1):
'''
init parameters:
feature_size: the feature number of the embedding layer
hidden_size: the hidden size of GRU(usually equal to feature size)
output_length: the number of total dictionary
layer: layer number of RNN
'''
super(DecoderRNN, self).__init__()
self.hidden_size = hidden_size
self.output_length = output_length
self.feature_size = feature_size
self.embedding = nn.Embedding(output_length, feature_size, padding_idx = PAD_token)
self.gru = nn.GRU(feature_size, hidden_size, num_layers = layer)
self.gru.flatten_parameters()
self.out = nn.Linear(feature_size, output_length)
self.softmax = nn.LogSoftmax(dim=2)
self.layer = layer
def forward(self, input, hidden, rubbish):
'''
input:
input: one batch of word(changed by dictionary)
type: pytorch variable LongTensor
size: (1, batch_size)
hidden: hidden state of the RNN
type: pytorch variable FloatTensor
size: (layer_number, batch_size, hidden_size)
output:
output: output of RNN unit
type: pytorch variable FloatTensor
size: (layer_number, batch_size, feature_size)
hidden: hidden state of RNN unit
type: pytorch variable FloatTensor
size: (layer_number, batch_size, feature_size)
'''
output = self.embedding(input.view(1, -1))
output = F.relu(output)
output, hidden = self.gru(output, hidden)
output = self.softmax(self.out(output))
return output, hidden, None
def initHidden(self, batch_size):
result = Variable(torch.zeros(self.layer, batch_size, self.hidden_size))
if use_cuda:
return result.cuda()
else:
return result
class EvaluateDecoder(nn.Module):
def __init__(self, feature_size, hidden_size, output_length):
super(EvaluateDecoder, self).__init__()
'''
feature_size: input of GRU layer dimension
hidden_size: hidden size of GRU
output_length:the length of wrod
e.g.
EvaluateCritic(256, 256, lang_txt.n_words)
'''
self.hidden_size = hidden_size
self.output_length = output_length
self.feature_size = feature_size
self.embedding = nn.Embedding(output_length, feature_size, padding_idx = PAD_token)
self.gru = nn.GRU(feature_size, hidden_size)
self.gru.flatten_parameters()
self.out = nn.Linear(hidden_size, output_length)
self.sigmoid = nn.Sigmoid()
def forward(self, input, hidden, rubbish):
'''
input: a batch of a word index.
should be LongTensor, shape = (1, batch_size)
'''
output = self.embedding(input.view(1, -1))
output = F.relu(output)
output, hidden = self.gru(output, hidden)
output = self.sigmoid(self.out(output))
return output, hidden, None
def initHidden(self, batch_size):
'''
return the hidden of the first GRU cell
batch_size:int
'''
result = Variable(torch.zeros(1, batch_size, self.hidden_size))
if use_cuda:
return result.cuda()
else:
return result
class EvaluateEncoder(nn.Module):
def __init__(self, feature_size, hidden_size, output_length):
super(EvaluateEncoder, self).__init__()
'''
feature_size: input of GRU layer dimension
hidden_size: hidden size of GRU
output_length:the length of wrod
e.g.
EvaluateCritic(256, 256, lang_txt.n_words)
'''
self.hidden_size = hidden_size
self.output_length = output_length
self.feature_size = feature_size
self.embedding = nn.Embedding(output_length, feature_size, padding_idx = PAD_token)
self.gru = nn.GRU(feature_size, hidden_size)
self.gru.flatten_parameters()
def forward(self, input, hidden):
'''
input: a batch of a word index.
should be LongTensor, shape = (1, batch_size)
hidden: hidden of GRU
'''
output = self.embedding(input.view(1, -1))
output, hidden = self.gru(output, hidden)
return output, hidden
def initHidden(self, batch_size):
'''
return the hidden of the first GRU cell
batch_size:int
'''
result = Variable(torch.zeros(1, batch_size, self.hidden_size))
if use_cuda:
return result.cuda()
else:
return result
class EvaluateR(nn.Module):
'''
the evaluation function
use one layer of nn
'''
def __init__(self, hidden_size, layer_num = 1):
super(EvaluateR, self).__init__()
self.hidden_size = hidden_size
self.layer_num = layer_num
self.linear = nn.Linear(self.hidden_size, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, input):
'''
input:the first layer of hidden state
type: pytorch FloatTensor variable
shape: (batch_size, feature_number)
output: the evauate score of each word
type: pytorch FloatTensor variable(from 0 to 1)
shape: (batch_size, 1)
'''
#input1 = input.view(1, -1)
input = input.view(-1, self.hidden_size)
output = self.sigmoid(self.linear(input))
return output
class disEncoderRNN(nn.Module):
def __init__(self, feature_size, hidden_size, output_length):
super(disEncoderRNN, self).__init__()
self.hidden_size = hidden_size
self.embedding = nn.Embedding(output_length, feature_size)
self.gru = nn.GRU(feature_size, hidden_size)
#self.gru.flatten_parameters()
def forward(self, input, hidden):
embedded = self.embedding(input.view(1, -1))
output = embedded
output, hidden = self.gru(output, hidden)
return output, hidden
def initHidden(self, batch_size):
result = Variable(torch.zeros(1, batch_size, self.hidden_size))
if use_cuda:
return result.cuda(0)
else:
return result
class disDecoderRNN(nn.Module):
def __init__(self, feature_size, hidden_size, output_length, dropout = 0.2):
super(disDecoderRNN, self).__init__()
self.hidden_size = hidden_size
self.output_length = output_length
self.feature_size = feature_size
self.embedding = nn.Embedding(output_length, feature_size)
self.gru = nn.GRU(feature_size, hidden_size)
self.gru.flatten_parameters()
self.dropout_linear = nn.Dropout(p = dropout)
self.out = nn.Linear(hidden_size, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, input, hidden, rubbish):
output = self.embedding(input.view(1, -1))
output = F.relu(output)
output, hidden = self.gru(output, hidden)
output = self.dropout_linear(output)
output = self.sigmoid(self.out(output))
return output, hidden, None
def initHidden(self, batch_size):
result = Variable(torch.zeros(1, batch_size, self.hidden_size))
if use_cuda:
return result.cuda(0)
else:
return result