-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment.py
244 lines (211 loc) · 10.5 KB
/
experiment.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
from common import (load_state,
load_histories,
create_exp_dir)
from models.models import RecurrentCopyModel, MemRNN
from models.NMTModels import (RNNDecoder, RNNEncoder, Seq2Seq,
BidirectionalDecoder, BidirectionalEncoder, Attention, AttnSeq2Seq,
TransformerEncoder, TransformerDecoder, TransformerSeq2Seq)
from models.SAB import SAB_LSTM
from models.expRNN.orthogonal import OrthogonalRNN
from models.expRNN.initialization import henaff_init_
from models.expRNN.trivializations import expm
from models.expRNN.parametrization import get_parameters
from natsort import natsorted
import os
import torch
import torch.nn as nn
class Experiment(object):
def __init__(self, config):
# for each item in config that is a list, select current item.
save_dir = config.save_dir
if not os.path.exists(save_dir):
create_exp_dir(save_dir)
self.save_dir = save_dir
self.experiment_path = self.save_dir
state_file_list = natsorted([fn for fn in os.listdir(save_dir)
if fn.endswith('.pt')])
model = self._get_model(config)
if config.model not in ['ORNN']:
optimizer = self._get_optimizer(config, model.parameters())
else:
non_orth_parameters, log_orth_parameters = get_parameters(model)
normal_opt = self._get_optimizer(config, non_orth_parameters)
orth_opt = self._get_optimizer(config,log_orth_parameters,orth=True)
optimizer = (normal_opt, orth_opt)
scheduler = self._get_scheduler(config, optimizer)
# if path exists and some model has been saved in it, Load experiment
if os.path.exists(save_dir) and len(state_file_list):
# RESUME old experiment
print('Resuming experiment from: {}'.format(save_dir))
model, optimizer, scheduler, epoch = load_state(save_dir,
model,
optimizer,
scheduler,
best=False)
self.epoch = epoch
(self.train_losses, self.train_accs,
self.val_losses, self.val_accs, self.val_hist) = load_histories(save_dir)
# start new experiment
else:
(self.train_losses, self.train_accs, self.val_losses,
self.val_accs, self.val_hist) = ([] for i in range(5))
self.args = config
self.model = model
if config.device is not None:
self.model = self.model.to(config.device)
self.optimizer = optimizer
self.scheduler = scheduler
if config.cuda:
self.model = self.model.cuda()
if self.optimizer is not None:
for state in self.optimizer.state.values():
for k, v in state.items():
if isinstance(v, torch.Tensor):
state[k] = v.cuda()
# Adapted from https://github.com/veugene/DemiGORU
def _get_optimizer(self, config, params, orth=False):
name = config.opt
kwargs = {'params' : [p for p in params if p.requires_grad]}
if not orth:
if config.lr is not None:
kwargs.update({'lr': config.lr})
else:
if config.lr_orth is not None:
kwargs.update({'lr': config.lr_orth})
# automatically set to 1/10th of normal optimizer
elif config.lr is not None:
kwargs.update({'lr': config.lr/10})
if name.lower() == 'rmsprop':
if config.alpha is not None:
kwargs.update({'alpha': config.alpha})
optimizer = torch.optim.RMSprop(**kwargs)
elif name.lower() == 'adam':
kwargs.update({'betas': (config.beta0, config.beta1)})
optimizer = torch.optim.Adam(**kwargs)
elif name.lower() == 'sgd':
# add lr if none, required for sgd
if kwargs.get('lr') is None:
kwargs.update({'lr': 1e-4})
optimizer = torch.optim.SGD(**kwargs)
else:
raise ValueError("Optimizer {} not supported.".format(name))
return optimizer
def _get_model(self, config):
model_name = config.model
if model_name == 'RNN':
base = nn.RNNCell(input_size=config.input_size,
hidden_size=config.nhid,
nonlinearity=config.nonlin)
elif model_name == 'ORNN':
base = OrthogonalRNN(input_size=config.input_size,
hidden_size=config.nhid,
initializer_skew=henaff_init_,
mode='static',
param=expm)
elif model_name == 'LSTM':
base = nn.LSTMCell(input_size=config.input_size,
hidden_size=config.nhid)
elif model_name == 'MemRNN':
base = MemRNN(input_size=config.input_size,
hidden_size=config.nhid,
nonlinearity=config.nonlin,
device=config.device)
elif model_name == 'SAB':
model = SAB_LSTM(input_size=config.input_size,
hidden_size=config.nhid,
num_layers=config.nlayers,
num_classes=config.n_labels+1,
truncate_length=config.trunc,
attn_every_k=config.attk,
top_k=config.topk,
device=config.device)
return model
elif model_name == 'Trans':
base = Transformer(d_model=config.input_size,
nhead=config.nhead,
num_encoder_layers=config.nenc,
num_decoder_layers=config.ndec,
dim_feedforward=config.nhid,
dropout=0)
else:
raise ValueError('Model {} not supported.'.format(model_name))
if config.task == 'copy' or config.task == 'denoise':
if model_name == 'Trans':
model = base
else:
model = RecurrentCopyModel(base,
config.nhid,
config.onehot,
config.n_labels,
device=config.device)
else:
raise ValueError('Task {} not supported.'.format(config.task))
return model
def _get_scheduler(self, config, optimizer):
if vars(config).get('sch_kwargs', None) is None:
return None
kwargs = config.sch_kwargs
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer,
**kwargs)
return scheduler
class NMTExperiment(Experiment):
def _get_model(self, config):
model_name = config.model
if model_name == 'RNN':
encoder = RNNEncoder(inp_size=config.inp_size,
emb_size=config.demb,
hid_size=config.nhid,
n_layers=config.nenc,
dropout=config.dropout)
decoder = RNNDecoder(out_size=config.out_size,
emb_size=config.demb,
hid_size=config.nhid,
n_layers=config.ndec,
dropout=config.dropout)
model = Seq2Seq(encoder=encoder, decoder=decoder)
elif model_name == 'MemRNN':
encoder = BidirectionalEncoder(inp_size=config.inp_size,
emb_size=config.demb,
enc_hid_size=config.nhid,
dec_hid_size=config.nhid,
dropout=config.dropout,
device=config.device)
attention = Attention(enc_hid_size=config.nhid,
dec_hid_size=config.nhid,
device=config.device)
decoder = BidirectionalDecoder(out_size=config.out_size,
emb_size=config.demb,
enc_hid_size=config.nhid,
dec_hid_size=config.nhid,
dropout=config.dropout,
attention=attention,
device=config.device)
model = AttnSeq2Seq(encoder=encoder,
decoder=decoder)
model = model.to(config.device)
elif model_name == 'Trans':
encoder = TransformerEncoder(inp_size=config.inp_size,
hid_size=config.nhid,
n_layers=config.nenc,
n_heads=config.nhenc,
pf_dim=512,
dropout=config.dropout,
max_length=100,
device=config.device)
decoder = TransformerDecoder(output_dim=config.out_size,
hid_size=config.nhid,
n_layers=config.ndec,
n_heads=config.nhdec,
pf_dim=512,
dropout=config.dropout,
max_length=100,
device=config.device)
model = TransformerSeq2Seq(encoder=encoder,
decoder=decoder,
src_pad_idx=config.SRCPADIDX,
trg_pad_idx=config.TRGPADIDX,
device=config.device)
model = model.to(config.device)
else:
raise ValueError('Model {} not supported.'.format(model_name))
return model