forked from facebookresearch/MUSE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
267 lines (228 loc) · 9.59 KB
/
trainer.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
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import os
from logging import getLogger
import scipy
import scipy.linalg
import torch
from torch.autograd import Variable
from torch.nn import functional as F
from .utils import get_optimizer, load_embeddings, normalize_embeddings, export_embeddings
from .utils import clip_parameters
from .dico_builder import build_dictionary
from .evaluation.word_translation import DIC_EVAL_PATH, load_identical_char_dico, load_dictionary
logger = getLogger()
class Trainer(object):
def __init__(self, src_emb, tgt_emb, mapping, discriminator, params):
"""
Initialize trainer script.
"""
self.src_emb = src_emb
self.tgt_emb = tgt_emb
self.src_dico = params.src_dico
self.tgt_dico = getattr(params, 'tgt_dico', None)
self.mapping = mapping
self.discriminator = discriminator
self.params = params
# optimizers
if hasattr(params, 'map_optimizer'):
optim_fn, optim_params = get_optimizer(params.map_optimizer)
self.map_optimizer = optim_fn(mapping.parameters(), **optim_params)
if hasattr(params, 'dis_optimizer'):
optim_fn, optim_params = get_optimizer(params.dis_optimizer)
self.dis_optimizer = optim_fn(discriminator.parameters(), **optim_params)
else:
assert discriminator is None
# best validation score
self.best_valid_metric = -1e12
self.decrease_lr = False
def get_dis_xy(self, volatile):
"""
Get discriminator input batch / output target.
"""
# select random word IDs
bs = self.params.batch_size
mf = self.params.dis_most_frequent
assert mf <= min(len(self.src_dico), len(self.tgt_dico))
src_ids = torch.LongTensor(bs).random_(len(self.src_dico) if mf == 0 else mf)
tgt_ids = torch.LongTensor(bs).random_(len(self.tgt_dico) if mf == 0 else mf)
if self.params.cuda:
src_ids = src_ids.cuda()
tgt_ids = tgt_ids.cuda()
# get word embeddings
src_emb = self.src_emb(Variable(src_ids, volatile=True))
tgt_emb = self.tgt_emb(Variable(tgt_ids, volatile=True))
src_emb = self.mapping(Variable(src_emb.data, volatile=volatile))
tgt_emb = Variable(tgt_emb.data, volatile=volatile)
# input / target
x = torch.cat([src_emb, tgt_emb], 0)
y = torch.FloatTensor(2 * bs).zero_()
y[:bs] = 1 - self.params.dis_smooth
y[bs:] = self.params.dis_smooth
y = Variable(y.cuda() if self.params.cuda else y)
return x, y
def dis_step(self, stats):
"""
Train the discriminator.
"""
self.discriminator.train()
# loss
x, y = self.get_dis_xy(volatile=True)
preds = self.discriminator(Variable(x.data))
loss = F.binary_cross_entropy(preds, y)
stats['DIS_COSTS'].append(loss.data[0])
# check NaN
if (loss != loss).data.any():
logger.error("NaN detected (discriminator)")
exit()
# optim
self.dis_optimizer.zero_grad()
loss.backward()
self.dis_optimizer.step()
clip_parameters(self.discriminator, self.params.dis_clip_weights)
def mapping_step(self, stats):
"""
Fooling discriminator training step.
"""
if self.params.dis_lambda == 0:
return 0
self.discriminator.eval()
# loss
x, y = self.get_dis_xy(volatile=False)
preds = self.discriminator(x)
loss = F.binary_cross_entropy(preds, 1 - y)
loss = self.params.dis_lambda * loss
# check NaN
if (loss != loss).data.any():
logger.error("NaN detected (fool discriminator)")
exit()
# optim
self.map_optimizer.zero_grad()
loss.backward()
self.map_optimizer.step()
self.orthogonalize()
return 2 * self.params.batch_size
def load_training_dico(self, dico_train):
"""
Load training dictionary.
"""
word2id1 = self.src_dico.word2id
word2id2 = self.tgt_dico.word2id
# identical character strings
if dico_train == "identical_char":
self.dico = load_identical_char_dico(word2id1, word2id2)
# use one of the provided dictionary
elif dico_train == "default":
# filename = '%s-%s.0-5000.txt' % (self.params.src_lang, self.params.tgt_lang)
filename = '%s-%s.txt' % (self.params.src_lang, self.params.tgt_lang)
self.dico = load_dictionary(
os.path.join(DIC_EVAL_PATH, filename),
word2id1, word2id2
)
# dictionary provided by the user
else:
self.dico = load_dictionary(dico_train, word2id1, word2id2)
# cuda
if self.params.cuda:
self.dico = self.dico.cuda()
def build_dictionary(self):
"""
Build a dictionary from aligned embeddings.
"""
src_emb = self.mapping(self.src_emb.weight).data
tgt_emb = self.tgt_emb.weight.data
src_emb = src_emb / src_emb.norm(2, 1, keepdim=True).expand_as(src_emb)
tgt_emb = tgt_emb / tgt_emb.norm(2, 1, keepdim=True).expand_as(tgt_emb)
self.dico = build_dictionary(src_emb, tgt_emb, self.params)
def procrustes(self):
"""
Find the best orthogonal matrix mapping using the Orthogonal Procrustes problem
https://en.wikipedia.org/wiki/Orthogonal_Procrustes_problem
"""
A = self.src_emb.weight.data[self.dico[:, 0]]
B = self.tgt_emb.weight.data[self.dico[:, 1]]
W = self.mapping.weight.data
M = B.transpose(0, 1).mm(A).cpu().numpy()
U, S, V_t = scipy.linalg.svd(M, full_matrices=True)
W.copy_(torch.from_numpy(U.dot(V_t)).type_as(W))
def orthogonalize(self):
"""
Orthogonalize the mapping.
"""
if self.params.map_beta > 0:
W = self.mapping.weight.data
beta = self.params.map_beta
W.copy_((1 + beta) * W - beta * W.mm(W.transpose(0, 1).mm(W)))
def update_lr(self, to_log, metric):
"""
Update learning rate when using SGD.
"""
if 'sgd' not in self.params.map_optimizer:
return
old_lr = self.map_optimizer.param_groups[0]['lr']
new_lr = max(self.params.min_lr, old_lr * self.params.lr_decay)
if new_lr < old_lr:
logger.info("Decreasing learning rate: %.8f -> %.8f" % (old_lr, new_lr))
self.map_optimizer.param_groups[0]['lr'] = new_lr
if self.params.lr_shrink < 1 and to_log[metric] >= -1e7:
if to_log[metric] < self.best_valid_metric:
logger.info("Validation metric is smaller than the best: %.5f vs %.5f"
% (to_log[metric], self.best_valid_metric))
# decrease the learning rate, only if this is the
# second time the validation metric decreases
if self.decrease_lr:
old_lr = self.map_optimizer.param_groups[0]['lr']
self.map_optimizer.param_groups[0]['lr'] *= self.params.lr_shrink
logger.info("Shrinking the learning rate: %.5f -> %.5f"
% (old_lr, self.map_optimizer.param_groups[0]['lr']))
self.decrease_lr = True
def save_best(self, to_log, metric):
"""
Save the best model for the given validation metric.
"""
# best mapping for the given validation criterion
if to_log[metric] > self.best_valid_metric:
# new best mapping
self.best_valid_metric = to_log[metric]
logger.info('* Best value for "%s": %.5f' % (metric, to_log[metric]))
# save the mapping
W = self.mapping.weight.data.cpu().numpy()
path = os.path.join(self.params.exp_path, 'best_mapping.pth')
logger.info('* Saving the mapping to %s ...' % path)
torch.save(W, path)
def reload_best(self):
"""
Reload the best mapping.
"""
path = os.path.join(self.params.exp_path, 'best_mapping.pth')
logger.info('* Reloading the best model from %s ...' % path)
# reload the model
assert os.path.isfile(path)
to_reload = torch.from_numpy(torch.load(path))
W = self.mapping.weight.data
assert to_reload.size() == W.size()
W.copy_(to_reload.type_as(W))
def export(self):
"""
Export embeddings.
"""
params = self.params
# load all embeddings
logger.info("Reloading all embeddings for mapping ...")
params.src_dico, src_emb = load_embeddings(params, source=True, full_vocab=True)
params.tgt_dico, tgt_emb = load_embeddings(params, source=False, full_vocab=True)
# apply same normalization as during training
normalize_embeddings(src_emb, params.normalize_embeddings, mean=params.src_mean)
normalize_embeddings(tgt_emb, params.normalize_embeddings, mean=params.tgt_mean)
# map source embeddings to the target space
bs = 4096
logger.info("Map source embeddings to the target space ...")
for i, k in enumerate(range(0, len(src_emb), bs)):
x = Variable(src_emb[k:k + bs], volatile=True)
src_emb[k:k + bs] = self.mapping(x.cuda() if params.cuda else x).data.cpu()
# write embeddings to the disk
export_embeddings(src_emb, tgt_emb, params)