-
Notifications
You must be signed in to change notification settings - Fork 7
/
update_functions.py
300 lines (228 loc) · 10.5 KB
/
update_functions.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import abc
import itertools
import torch
from utils import to_device
class UpdateFunction(object):
def __init__(self, model, lr=0.001, weight_decay=0.0000001, grad_clipping=0, training=True, **kwargs):
self.model = model
self.grad_clipping = grad_clipping
self.lr = lr
self.weight_decay = weight_decay
self.training = training
def get_parameters(self, *modules):
parameters = itertools.chain.from_iterable(m.parameters() for m in modules)
return parameters
@abc.abstractmethod
def step(self, engine, batch):
pass
def __call__(self, engine, batch):
if self.training:
self.model.train()
else:
self.model.eval()
batch = to_device(batch)
losses = self.step(engine, batch)
return losses
class Seq2SeqUpdateFunction(UpdateFunction):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.params = self.get_parameters(self.model)
self.optimizer = torch.optim.Adam(self.params, lr=self.lr, weight_decay=self.weight_decay, amsgrad=True)
def step(self, engine, batch):
self.optimizer.zero_grad()
output_dict = self.model(batch)
loss = output_dict["loss"]
if self.training:
loss.backward()
if self.grad_clipping != 0:
torch.nn.utils.clip_grad_norm_(self.params, self.grad_clipping)
self.optimizer.step()
output_dict = {
'loss': loss,
}
return output_dict
class Seq2SeqMeaningStyleUpdateFunction(UpdateFunction):
def __init__(self, D_num_iterations, D_loss_multiplier, P_loss_multiplier, P_bow_loss_multiplier,
use_discriminator, use_predictor, use_predictor_bow, use_motivator, use_gauss,
*args, **kwargs):
super().__init__(*args, **kwargs)
self.D_num_iterations = D_num_iterations
self.D_loss_multiplier = D_loss_multiplier
self.P_loss_multiplier = P_loss_multiplier
self.P_bow_loss_multiplier = P_bow_loss_multiplier
self.use_discriminator = use_discriminator
self.use_predictor = use_predictor
self.use_predictor_bow = use_predictor_bow
self.use_motivator = use_motivator
self.use_gauss = use_gauss
self.params_encoder_decoder = self.get_parameters(
# encoder
self.model.embedding, self.model.encoder,
self.model.hidden_meaning, self.model.hidden_style,
# decoder
self.model.decoder_cell, self.model.output_projection,
self.model.meaning_style_hidden
)
params_D = []
if use_discriminator:
params_D.append(self.model.D_meaning)
if use_motivator:
params_D.append(self.model.D_style)
if use_predictor:
params_D.append(self.model.P_style)
if use_motivator:
params_D.append(self.model.P_meaning)
if use_predictor_bow:
params_D.append(self.model.P_bow_style)
if use_motivator:
params_D.append(self.model.P_bow_meaning)
if use_gauss:
params_D.append(self.model.D_hidden)
self.params_D = self.get_parameters(*params_D)
self.optimizer_encoder_decoder = torch.optim.Adam(
self.params_encoder_decoder, lr=self.lr, weight_decay=self.weight_decay, amsgrad=True
)
self.optimizer_D = torch.optim.Adam(
self.params_D, lr=self.lr, weight_decay=self.weight_decay, amsgrad=True
)
def step(self, engine, batch):
losses_output_dict = {}
# discriminator
state = self.model.encode(batch)
output_dict_D = self.model.discriminate(state, batch)
if self.use_discriminator:
loss_D_meaning = output_dict_D['loss_D_meaning']
losses_output_dict['loss_D_meaning'] = float(loss_D_meaning.item())
if self.use_motivator:
loss_D_style = output_dict_D['loss_D_style']
losses_output_dict['loss_D_style'] = float(loss_D_style.item())
else:
loss_D_style = 0
else:
loss_D_meaning = 0
loss_D_style = 0
if 'meaning_embedding' in batch and self.use_predictor:
loss_P_style = output_dict_D['loss_P_style']
losses_output_dict['loss_P_style'] = float(loss_P_style.item())
if self.use_motivator:
loss_P_meaning = output_dict_D['loss_P_meaning']
losses_output_dict['loss_P_meaning'] = float(loss_P_meaning.item())
else:
loss_P_meaning = 0
else:
loss_P_style = 0
loss_P_meaning = 0
if 'meaning_bow' in batch and self.use_predictor_bow:
loss_P_bow_style = output_dict_D['loss_P_bow_style']
losses_output_dict['loss_P_bow_style'] = float(loss_P_bow_style.item())
if self.use_motivator:
loss_P_bow_meaning = output_dict_D['loss_P_bow_meaning']
losses_output_dict['loss_P_bow_meaning'] = float(loss_P_bow_meaning.item())
else:
loss_P_bow_meaning = 0
else:
loss_P_bow_style = 0
loss_P_bow_meaning = 0
if self.use_gauss:
output_dict_D = self.model.combine_meaning_style(output_dict_D)
G_real = torch.randn_like(output_dict_D['decoder_hidden'])
G_fake = output_dict_D['decoder_hidden']
G_labels = torch.cat([torch.ones_like(batch['style']), torch.zeros_like(batch['style'])], dim=0)
G_inputs = torch.cat([G_real, G_fake], dim=0)
G_logits = self.model.D_hidden(G_inputs)
loss_D_hidden = self.model._D_loss(G_logits, G_labels)
losses_output_dict['loss_D_hidden'] = float(loss_D_hidden.item())
else:
loss_D_hidden = 0
loss_D_total = loss_D_meaning + loss_D_style \
+ loss_P_meaning + loss_P_style \
+ loss_P_bow_meaning + loss_P_bow_style \
+ loss_D_hidden
if self.training:
loss_D_total.backward()
if self.grad_clipping != 0:
torch.nn.utils.clip_grad_norm_(self.params_D, self.grad_clipping)
self.optimizer_D.step()
self.model.zero_grad()
# encoder-decoder
if not self.training or engine.state.iteration % self.D_num_iterations == 0:
output_dict = self.model(batch)
output_dict = self.model.discriminate(output_dict, batch, adversarial=True)
loss = output_dict['loss']
losses_output_dict['loss'] = float(loss.item())
loss_D_adv_meaning = output_dict['loss_D_adv_meaning']
losses_output_dict['loss_D_adv_meaning'] = float(loss_D_adv_meaning.item())
if self.use_motivator:
loss_D_adv_style = output_dict['loss_D_adv_style']
losses_output_dict['loss_D_adv_style'] = float(loss_D_adv_style.item())
else:
loss_D_adv_style = 0
if 'meaning_embedding' in batch and self.use_predictor:
loss_P_adv_style = output_dict['loss_P_adv_style']
losses_output_dict['loss_P_adv_style'] = float(loss_P_adv_style.item())
if self.use_motivator:
loss_P_adv_meaning = output_dict['loss_P_adv_meaning']
losses_output_dict['loss_P_adv_meaning'] = float(loss_P_adv_meaning.item())
else:
loss_P_adv_meaning = 0
else:
loss_P_adv_style = 0
loss_P_adv_meaning = 0
if 'meaning_bow' in batch and self.use_predictor_bow:
loss_P_bow_adv_style = output_dict['loss_P_bow_adv_style']
losses_output_dict['loss_P_bow_adv_style'] = float(loss_P_bow_adv_style.item())
if self.use_motivator:
loss_P_bow_adv_meaning = output_dict['loss_P_bow_adv_meaning']
losses_output_dict['loss_P_bow_adv_meaning'] = float(loss_P_bow_adv_meaning.item())
else:
loss_P_bow_adv_meaning = 0
else:
loss_P_bow_adv_style = 0
loss_P_bow_adv_meaning = 0
if self.use_gauss:
G_logits = self.model.D_hidden(output_dict['decoder_hidden'])
loss_D_adv_hidden = self.model._D_adv_loss(G_logits)
losses_output_dict['loss_D_adv_hidden'] = float(loss_D_adv_hidden.item())
else:
loss_D_adv_hidden = 0
loss_total = loss
if loss_D_meaning <= 0.35:
loss_total += self.D_loss_multiplier * loss_D_adv_meaning
if loss_D_style <= 0.35:
loss_total += self.D_loss_multiplier * loss_D_adv_style
if loss_P_style < 2.5e-3:
loss_total += self.P_loss_multiplier * loss_P_adv_style
if loss_P_meaning < 2.5e-3:
loss_total += self.P_loss_multiplier * loss_P_adv_meaning
if loss_P_bow_meaning <= 0.35:
loss_total += self.P_bow_loss_multiplier * loss_P_bow_adv_meaning
if loss_P_bow_style <= 0.35:
loss_total += self.P_bow_loss_multiplier * loss_P_bow_adv_style
if loss_D_hidden <= 0.35:
loss_total += self.D_loss_multiplier * loss_D_adv_hidden
losses_output_dict['loss_total'] = float(loss_total.item())
if self.training:
loss_total.backward()
if self.grad_clipping != 0:
torch.nn.utils.clip_grad_norm_(self.params_encoder_decoder, self.grad_clipping)
self.optimizer_encoder_decoder.step()
self.model.zero_grad()
return losses_output_dict
class StyleClassifierUpdateFunction(UpdateFunction):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.params = self.get_parameters(self.model)
self.optimizer = torch.optim.Adam(self.params, lr=self.lr, weight_decay=self.weight_decay, amsgrad=True)
def step(self, engine, batch):
self.optimizer.zero_grad()
output_dict = self.model(batch)
loss = output_dict["loss"]
if self.training:
loss.backward()
if self.grad_clipping != 0:
torch.nn.utils.clip_grad_norm_(self.params, self.grad_clipping)
self.optimizer.step()
output_dict = {
'loss': loss,
}
return output_dict