forked from Stonesjtu/Pytorch-NCE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
245 lines (215 loc) · 8.76 KB
/
main.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
#!/usr/bin/env python
import sys
import argparse
import time
from datetime import datetime
import math
import torch
import torch.optim as optim
import data
from model import RNNModel
from nce import NCELoss
from cross_entropy import CELoss
from utils import process_data, build_unigram_noise
def setup_parser():
parser = argparse.ArgumentParser(
description='PyTorch PennTreeBank RNN/LSTM Language Model')
parser.add_argument('--data', type=str, default='./data/penn',
help='location of the data corpus')
parser.add_argument('--dict', type=str, default=None,
help='location of the vocabulary file, without which will use vocab of training corpus')
parser.add_argument('--emsize', type=int, default=200,
help='size of word embeddings')
parser.add_argument('--nhid', type=int, default=200,
help='number of hidden units per layer')
parser.add_argument('--nlayers', type=int, default=2,
help='number of layers')
parser.add_argument('--lr', type=float, default=1.0,
help='initial learning rate')
parser.add_argument('--weight-decay', type=float, default=1e-5,
help='initial weight decay')
parser.add_argument('--lr-decay', type=float, default=2,
help='learning rate decay when no progress is observed on validation set')
parser.add_argument('--clip', type=float, default=0.25,
help='gradient clipping')
parser.add_argument('--epochs', type=int, default=40,
help='upper epoch limit')
parser.add_argument('--batch-size', type=int, default=20, metavar='N',
help='batch size')
parser.add_argument('--dropout', type=float, default=0.2,
help='dropout applied to layers (0 = no dropout)')
parser.add_argument('--seed', type=int, default=1111,
help='random seed')
parser.add_argument('--cuda', action='store_true',
help='use CUDA')
parser.add_argument('--log-interval', type=int, default=200, metavar='N',
help='report interval')
parser.add_argument('--save', type=str, default='model.pt',
help='path to save the final model')
parser.add_argument('--nce', action='store_true',
help='use NCE as loss function')
parser.add_argument('--noise-ratio', type=int, default=10,
help='set the noise ratio of NCE sampling')
parser.add_argument('--norm-term', type=int, default=9,
help='set the log normalization term of NCE sampling')
parser.add_argument('--train', action='store_true',
help='set train mode, otherwise only evaluation is performed')
parser.add_argument('--tb-name', type=str, default=None,
help='the name which would be used in tensorboard record')
parser.add_argument('--prof', action='store_true',
help='Enable profiling mode, will execute only one batch data')
return parser
parser = setup_parser()
args = parser.parse_args()
print(args)
# Initialize tensor-board summary writer
if args.tb_name:
from tensorboard import SummaryWriter
exp_name = '{} {}'.format(
datetime.now().strftime('%B%d %H:%M:%S'),
args.tb_name,
)
writer = SummaryWriter('runs/{}'.format(
exp_name,
))
# Set the random seed manually for reproducibility.
torch.manual_seed(args.seed)
if torch.cuda.is_available():
if not args.cuda:
print("WARNING: You have a CUDA device, so you should probably run with --cuda")
else:
torch.cuda.manual_seed(args.seed)
#################################################################
# Load data
#################################################################
corpus = data.Corpus(
path=args.data,
dict_path=args.dict,
batch_size=args.batch_size,
shuffle=True,
pin_memory=args.cuda,
)
eval_batch_size = args.batch_size
################################################################## Build the criterion and model
#################################################################
ntokens = len(corpus.train.dataset.dictionary)
print('Vocabulary size is {}'.format(ntokens))
# noise for soise sampling in NCE
noise = build_unigram_noise(
torch.FloatTensor(corpus.train.dataset.dictionary.idx2count)
)
if args.nce:
criterion = NCELoss(
ntokens=ntokens,
nhidden=args.nhid,
noise=noise,
noise_ratio=args.noise_ratio,
norm_term=args.norm_term,
normed_eval=True, # evaluate PPL using normalized prob
)
else:
criterion = CELoss(
ntokens=ntokens,
nhidden=args.nhid,
)
model = RNNModel(
ntokens, args.emsize, args.nhid, args.nlayers,
criterion=criterion, dropout=args.dropout,
)
if args.cuda:
model.cuda()
print(model)
#################################################################
# Training code
#################################################################
def train(model, data_source, lr=1.0, weight_decay=1e-5, momentum=0.9):
params = model.parameters()
optimizer = optim.SGD(
params=params,
lr=lr,
momentum=momentum,
weight_decay=weight_decay
)
# Turn on training mode which enables dropout.
model.train()
total_loss = 0
for num_batch, data_batch in enumerate(corpus.train):
optimizer.zero_grad()
data, target, length = process_data(data_batch, cuda=args.cuda)
loss = model(data, target, length)
loss.backward()
# `clip_grad_norm` helps prevent the exploding gradient problem in RNNs / LSTMs.
torch.nn.utils.clip_grad_norm(params, args.clip)
optimizer.step()
total_loss += loss.data[0]
if num_batch % args.log_interval == 0 and num_batch > 0:
if args.prof:
break
cur_loss = total_loss / args.log_interval
print('| epoch {:3d} | {:5d}/{:5d} batches'
' | lr {:02.2f} | '
'loss {:5.2f} | ppl {:8.2f}'.format(
epoch, num_batch, len(corpus.train), lr,
cur_loss, math.exp(cur_loss)))
total_loss = 0
print('-' * 87)
def evaluate(model, data_source, cuda=args.cuda):
# Turn on evaluation mode which disables dropout.
model.eval()
eval_loss = 0
total_length = 0
data_source.batch_size = eval_batch_size
for data_batch in data_source:
data, target, length = process_data(data_batch, cuda=cuda, eval=True)
loss = model(data, target, length)
cur_length = length.sum()
eval_loss += loss.data[0] * cur_length
total_length += cur_length
return math.exp(eval_loss/total_length)
if __name__ == '__main__':
lr = args.lr
best_val_ppl = None
if args.train:
# At any point you can hit Ctrl + C to break out of training early.
try:
# Loop over epochs.
for epoch in range(1, args.epochs + 1):
epoch_start_time = time.time()
train(model, corpus.train, lr=lr, weight_decay=args.weight_decay)
if args.prof:
break
val_ppl = evaluate(model, corpus.valid)
if args.tb_name:
writer.add_scalar('valid_PPL', val_ppl, epoch)
print('-' * 89)
print('| end of epoch {:3d} | time: {:5.2f}s |'
'valid ppl {:8.2f}'.format(epoch,
(time.time() - epoch_start_time),
val_ppl))
print('-' * 89)
with open(args.save+'.epoch_{}'.format(epoch), 'wb') as f:
torch.save(model, f)
# Save the model if the validation loss is the best we've seen so far.
if not best_val_ppl or val_ppl < best_val_ppl:
with open(args.save, 'wb') as f:
torch.save(model, f)
best_val_ppl = val_ppl
else:
# Anneal the learning rate if no improvement has been seen in the
# validation dataset.
lr /= args.lr_decay
except KeyboardInterrupt:
print('-' * 89)
print('Exiting from training early')
else:
# Load the best saved model.
with open(args.save, 'rb') as f:
model = torch.load(f)
# Run on test data.
test_ppl = evaluate(model, corpus.test)
print('=' * 89)
print('| End of training | test ppl {:8.2f}'.format(test_ppl))
print('=' * 89)
sys.stdout.flush()
if args.tb_name:
writer.close()