-
Notifications
You must be signed in to change notification settings - Fork 10
/
train.py
executable file
·217 lines (178 loc) · 9.55 KB
/
train.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
import os
import logging
import argparse
import numpy as np
from tqdm import tqdm
from collections import OrderedDict
import torch
import torch.nn as nn
from seq2seq import models, utils
from seq2seq.data.dictionary import Dictionary
from seq2seq.data.dataset import Seq2SeqDataset, BatchSampler
from seq2seq.models import ARCH_MODEL_REGISTRY, ARCH_CONFIG_REGISTRY
def get_args():
""" Defines training-specific hyper-parameters. """
parser = argparse.ArgumentParser('Sequence to Sequence Model')
# Add data arguments
parser.add_argument('--data', default='europarl_prepared', help='path to data directory')
parser.add_argument('--source-lang', default='de', help='source language')
parser.add_argument('--target-lang', default='en', help='target language')
parser.add_argument('--max-tokens', default=None, type=int, help='maximum number of tokens in a batch')
parser.add_argument('--batch-size', default=10, type=int, help='maximum number of sentences in a batch')
parser.add_argument('--train-on-tiny', action='store_true', help='train model on a tiny dataset')
# Add model arguments
parser.add_argument('--arch', default='lstm', choices=ARCH_MODEL_REGISTRY.keys(), help='model architecture')
# Add optimization arguments
parser.add_argument('--max-epoch', default=100, type=int, help='force stop training at specified epoch')
parser.add_argument('--clip-norm', default=4.0, type=float, help='clip threshold of gradients')
parser.add_argument('--lr', default=0.0003, type=float, help='learning rate')
parser.add_argument('--patience', default=10, type=int,
help='number of epochs without improvement on validation set before early stopping')
# Add checkpoint arguments
parser.add_argument('--log-file', default=None, help='path to save logs')
parser.add_argument('--save-dir', default='checkpoints', help='path to save checkpoints')
parser.add_argument('--restore-file', default='checkpoint_last.pt', help='filename to load checkpoint')
parser.add_argument('--save-interval', type=int, default=1, help='save a checkpoint every N epochs')
parser.add_argument('--no-save', action='store_true', help='don\'t save models or checkpoints')
parser.add_argument('--epoch-checkpoints', action='store_true', help='store all epoch checkpoints')
# Parse twice as model arguments are not known the first time
args, _ = parser.parse_known_args()
model_parser = parser.add_argument_group(argument_default=argparse.SUPPRESS)
ARCH_MODEL_REGISTRY[args.arch].add_args(model_parser)
args = parser.parse_args()
ARCH_CONFIG_REGISTRY[args.arch](args)
return args
def main(args):
""" Main training function. Trains the translation model over the course of several epochs, including dynamic
learning rate adjustment and gradient clipping. """
logging.info('Commencing training!')
torch.manual_seed(42)
np.random.seed(42)
utils.init_logging(args)
# Load dictionaries
src_dict = Dictionary.load(os.path.join(args.data, 'dict.{:s}'.format(args.source_lang)))
logging.info('Loaded a source dictionary ({:s}) with {:d} words'.format(args.source_lang, len(src_dict)))
tgt_dict = Dictionary.load(os.path.join(args.data, 'dict.{:s}'.format(args.target_lang)))
logging.info('Loaded a target dictionary ({:s}) with {:d} words'.format(args.target_lang, len(tgt_dict)))
# Load datasets
def load_data(split):
return Seq2SeqDataset(
src_file=os.path.join(args.data, '{:s}.{:s}'.format(split, args.source_lang)),
tgt_file=os.path.join(args.data, '{:s}.{:s}'.format(split, args.target_lang)),
src_dict=src_dict, tgt_dict=tgt_dict)
train_dataset = load_data(split='train') if not args.train_on_tiny else load_data(split='tiny_train')
valid_dataset = load_data(split='valid')
# Build model and optimization criterion
model = models.build_model(args, src_dict, tgt_dict)
logging.info('Built a model with {:d} parameters'.format(sum(p.numel() for p in model.parameters())))
criterion = nn.CrossEntropyLoss(ignore_index=src_dict.pad_idx, reduction='sum')
# Instantiate optimizer and learning rate scheduler
optimizer = torch.optim.Adam(model.parameters(), args.lr)
# Load last checkpoint if one exists
state_dict = utils.load_checkpoint(args, model, optimizer) # lr_scheduler
last_epoch = state_dict['last_epoch'] if state_dict is not None else -1
# Track validation performance for early stopping
bad_epochs = 0
best_validate = float('inf')
for epoch in range(last_epoch + 1, args.max_epoch):
train_loader = \
torch.utils.data.DataLoader(train_dataset, num_workers=1, collate_fn=train_dataset.collater,
batch_sampler=BatchSampler(train_dataset, args.max_tokens, args.batch_size, 1,
0, shuffle=True, seed=42))
model.train()
stats = OrderedDict()
stats['loss'] = 0
stats['lr'] = 0
stats['num_tokens'] = 0
stats['batch_size'] = 0
stats['grad_norm'] = 0
stats['clip'] = 0
# Display progress
progress_bar = tqdm(train_loader, desc='| Epoch {:03d}'.format(epoch), leave=False, disable=False)
# Iterate over the training set
for i, sample in enumerate(progress_bar):
if len(sample) == 0:
continue
model.train()
'''
___QUESTION-1-DESCRIBE-F-START___
Describe what the following lines of code do.
'''
output, _ = model(sample['src_tokens'], sample['src_lengths'], sample['tgt_inputs'])
loss = \
criterion(output.view(-1, output.size(-1)), sample['tgt_tokens'].view(-1)) / len(sample['src_lengths'])
loss.backward()
grad_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip_norm)
optimizer.step()
optimizer.zero_grad()
'''___QUESTION-1-DESCRIBE-F-END___'''
# Update statistics for progress bar
total_loss, num_tokens, batch_size = loss.item(), sample['num_tokens'], len(sample['src_tokens'])
stats['loss'] += total_loss * len(sample['src_lengths']) / sample['num_tokens']
stats['lr'] += optimizer.param_groups[0]['lr']
stats['num_tokens'] += num_tokens / len(sample['src_tokens'])
stats['batch_size'] += batch_size
stats['grad_norm'] += grad_norm
stats['clip'] += 1 if grad_norm > args.clip_norm else 0
progress_bar.set_postfix({key: '{:.4g}'.format(value / (i + 1)) for key, value in stats.items()},
refresh=True)
logging.info('Epoch {:03d}: {}'.format(epoch, ' | '.join(key + ' {:.4g}'.format(
value / len(progress_bar)) for key, value in stats.items())))
# Calculate validation loss
valid_perplexity = validate(args, model, criterion, valid_dataset, epoch)
model.train()
# Save checkpoints
if epoch % args.save_interval == 0:
utils.save_checkpoint(args, model, optimizer, epoch, valid_perplexity) # lr_scheduler
# Check whether to terminate training
if valid_perplexity < best_validate:
best_validate = valid_perplexity
bad_epochs = 0
else:
bad_epochs += 1
if bad_epochs >= args.patience:
logging.info('No validation set improvements observed for {:d} epochs. Early stop!'.format(args.patience))
break
def validate(args, model, criterion, valid_dataset, epoch):
""" Validates model performance on a held-out development set. """
valid_loader = \
torch.utils.data.DataLoader(valid_dataset, num_workers=1, collate_fn=valid_dataset.collater,
batch_sampler=BatchSampler(valid_dataset, args.max_tokens, args.batch_size, 1, 0,
shuffle=False, seed=42))
model.eval()
stats = OrderedDict()
stats['valid_loss'] = 0
stats['num_tokens'] = 0
stats['batch_size'] = 0
# Iterate over the validation set
for i, sample in enumerate(valid_loader):
if len(sample) == 0:
continue
with torch.no_grad():
# Compute loss
output, attn_scores = model(sample['src_tokens'], sample['src_lengths'], sample['tgt_inputs'])
loss = criterion(output.view(-1, output.size(-1)), sample['tgt_tokens'].view(-1))
# Update tracked statistics
stats['valid_loss'] += loss.item()
stats['num_tokens'] += sample['num_tokens']
stats['batch_size'] += len(sample['src_tokens'])
# Calculate validation perplexity
stats['valid_loss'] = stats['valid_loss'] / stats['num_tokens']
perplexity = np.exp(stats['valid_loss'])
stats['num_tokens'] = stats['num_tokens'] / stats['batch_size']
logging.info(
'Epoch {:03d}: {}'.format(epoch, ' | '.join(key + ' {:.3g}'.format(value) for key, value in stats.items())) +
' | valid_perplexity {:.3g}'.format(perplexity))
return perplexity
if __name__ == '__main__':
args = get_args()
args.device_id = 0
# Set up logging to file
logging.basicConfig(filename=args.log_file, filemode='a', level=logging.INFO,
format='%(levelname)s: %(message)s')
if args.log_file is not None:
# Logging to console
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logging.getLogger('').addHandler(console)
main(args)