-
Notifications
You must be signed in to change notification settings - Fork 161
/
nmt_autoencoder.py
209 lines (177 loc) · 6.75 KB
/
nmt_autoencoder.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
#!/u/subramas/miniconda2/bin/python
"""Main script to run things"""
import sys
sys.path.append('/u/subramas/Research/nmt-pytorch/')
from data_utils import read_nmt_data, get_autoencode_minibatch, read_config, hyperparam_string
from model import Seq2Seq, Seq2SeqAutoencoder
from evaluate import evaluate_model, evaluate_autoencode_model
import math
import numpy as np
import logging
import argparse
import os
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.autograd import Variable
parser = argparse.ArgumentParser()
parser.add_argument(
"--config",
help="path to json config",
required=True
)
args = parser.parse_args()
config_file_path = args.config
config = read_config(config_file_path)
experiment_name = hyperparam_string(config)
save_dir = config['data']['save_dir']
load_dir = config['data']['load_dir']
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
filename='log/%s' % (experiment_name),
filemode='w'
)
# define a new Handler to log to console as well
console = logging.StreamHandler()
# optional, set the logging level
console.setLevel(logging.INFO)
# set a format which is the same for console use
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
print 'Reading data ...'
src, _ = read_nmt_data(
src=config['data']['src'],
trg=None
)
src_test, _ = read_nmt_data(
src=config['data']['test_src'],
trg=None
)
batch_size = config['data']['batch_size']
max_length = config['data']['max_src_length']
src_vocab_size = len(src['word2id'])
logging.info('Model Parameters : ')
logging.info('Task : %s ' % (config['data']['task']))
logging.info('Model : %s ' % (config['model']['seq2seq']))
logging.info('Source Language : %s ' % (config['model']['src_lang']))
logging.info('Target Language : %s ' % (config['model']['src_lang']))
logging.info('Source Word Embedding Dim : %s' % (config['model']['dim_word_src']))
logging.info('Target Word Embedding Dim : %s' % (config['model']['dim_word_trg']))
logging.info('Source RNN Hidden Dim : %s' % (config['model']['dim']))
logging.info('Target RNN Hidden Dim : %s' % (config['model']['dim']))
logging.info('Source RNN Depth : %d ' % (config['model']['n_layers_src']))
logging.info('Target RNN Depth : %d ' % (config['model']['n_layers_trg']))
logging.info('Source RNN Bidirectional : %s' % (config['model']['bidirectional']))
logging.info('Batch Size : %d ' % (config['data']['batch_size']))
logging.info('Optimizer : %s ' % (config['training']['optimizer']))
logging.info('Learning Rate : %f ' % (config['training']['lrate']))
logging.info('Found %d words in src ' % (src_vocab_size))
weight_mask = torch.ones(src_vocab_size).cuda()
weight_mask[src['word2id']['<pad>']] = 0
loss_criterion = nn.CrossEntropyLoss(weight=weight_mask).cuda()
model = Seq2SeqAutoencoder(
src_emb_dim=config['model']['dim_word_src'],
trg_emb_dim=config['model']['dim_word_trg'],
src_vocab_size=src_vocab_size,
src_hidden_dim=config['model']['dim'],
trg_hidden_dim=config['model']['dim'],
batch_size=batch_size,
bidirectional=config['model']['bidirectional'],
pad_token_src=src['word2id']['<pad>'],
nlayers=config['model']['n_layers_src'],
nlayers_trg=config['model']['n_layers_trg'],
dropout=0.,
).cuda()
if load_dir:
model.load_state_dict(torch.load(
open(load_dir)
))
def clip_gradient(model, clip):
"""Compute a gradient clipping coefficient based on gradient norm."""
totalnorm = 0
for p in model.parameters():
modulenorm = p.grad.data.norm()
totalnorm += modulenorm ** 2
totalnorm = math.sqrt(totalnorm)
return min(1, clip / (totalnorm + 1e-6))
if config['training']['optimizer'] == 'adam':
lr = config['training']['lrate']
optimizer = optim.Adam(model.parameters(), lr=lr)
elif config['training']['optimizer'] == 'adadelta':
optimizer = optim.Adadelta(model.parameters())
elif config['training']['optimizer'] == 'sgd':
lr = config['training']['lrate']
optimizer = optim.SGD(model.parameters(), lr=lr)
else:
raise NotImplementedError("Learning method not recommend for task")
torch.save(
model.state_dict(),
open(os.path.join(
save_dir,
experiment_name + 'epoch_0.model'), 'wb'
)
)
bleu = evaluate_autoencode_model(
model, src, src_test, config, verbose=False,
metric='bleu',
)
logging.info('Epoch : %d : BLEU : %.5f ' % (0, bleu))
for i in xrange(1000):
losses = []
for j in xrange(0, len(src['data']), batch_size):
input_lines_src, output_lines_src, lens_src, mask_src = get_autoencode_minibatch(
src['data'], src['word2id'], j,
batch_size, max_length, add_start=True, add_end=True
)
decoder_logit = model(input_lines_src)
optimizer.zero_grad()
loss = loss_criterion(
decoder_logit.contiguous().view(-1, src_vocab_size),
output_lines_src.view(-1)
)
losses.append(loss.data[0])
loss.backward()
optimizer.step()
if j % config['management']['monitor_loss'] == 0:
logging.info('Epoch : %d Minibatch : %d Loss : %.5f' % (
i, j, np.mean(losses))
)
losses = []
if (
config['management']['print_samples'] and
j % config['management']['print_samples'] == 0
):
word_probs = model.decode(
decoder_logit
).data.cpu().numpy().argmax(axis=-1)
output_lines_trg = input_lines_src.data.cpu().numpy()
for sentence_pred, sentence_real in zip(
word_probs[:5], output_lines_trg[:5]
):
sentence_pred = [src['id2word'][x] for x in sentence_pred]
sentence_real = [src['id2word'][x] for x in sentence_real]
if '</s>' in sentence_real:
index = sentence_real.index('</s>')
sentence_real = sentence_real[:index]
sentence_pred = sentence_pred[:index]
logging.info(' '.join(sentence_pred))
logging.info('-----------------------------------------------')
logging.info(' '.join(sentence_real))
logging.info('===============================================')
bleu = evaluate_autoencode_model(
model, src, src_test, config, verbose=False,
metric='bleu',
)
logging.info('Epoch : %d : BLEU : %.5f ' % (i, bleu))
torch.save(
model.state_dict(),
open(os.path.join(
save_dir,
experiment_name + '__epoch_%d' % (i) + '.model'), 'wb'
)
)