-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
225 lines (189 loc) · 8 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
218
219
220
221
222
223
224
225
# Can be run with python -m pie.scripts.train
import time
import os
from datetime import datetime
import logging
# import pie
from pie.models.base_model import BaseModel
from pie.settings import settings_from_file
from pie.trainer import Trainer
import pie.initialization as initialization
from pie.data import Dataset, Reader, MultiLabelEncoder
from pie.models import SimpleModel, get_pretrained_embeddings
import pie.optimize as optimize
import pie
# set seeds
import random
import numpy
import torch
def get_targets(settings):
return [task['name'] for task in settings.tasks if task.get('target')]
def get_fname_infix(settings):
# fname
fname = os.path.join(settings.modelpath, settings.modelname)
timestamp = datetime.now().strftime("%Y_%m_%d-%H_%M_%S")
infix = '+'.join(get_targets(settings)) + '-' + timestamp
return fname, infix
def run(settings):
now = datetime.now()
# set seed
seed = now.hour * 10000 + now.minute * 100 + now.second
print("Using seed:", seed)
random.seed(seed)
numpy.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
if settings.verbose:
logging.basicConfig(level=logging.INFO)
# datasets
reader = Reader(settings, settings.input_path)
tasks = reader.check_tasks(expected=None)
if settings.verbose:
print("::: Available tasks :::")
print()
for task in tasks:
print("- {}".format(task))
print()
# load existing model
model = None
label_encoder = None
if settings.existing_model:
print(f"::: Loading existing model from {settings.existing_model} :::")
model = BaseModel.load(settings.existing_model)
label_encoder = model.label_encoder
if label_encoder is None:
# label encoder
label_encoder = MultiLabelEncoder.from_settings(settings, tasks=tasks)
if settings.verbose:
print("::: Fitting data :::")
print()
label_encoder.fit_reader(reader)
if settings.verbose:
print()
print("::: Vocabulary :::")
print()
types = '{}/{}={:.2f}'.format(*label_encoder.word.get_type_stats())
tokens = '{}/{}={:.2f}'.format(*label_encoder.word.get_token_stats())
print("- {:<15} types={:<10} tokens={:<10}".format("word", types, tokens))
types = '{}/{}={:.2f}'.format(*label_encoder.char.get_type_stats())
tokens = '{}/{}={:.2f}'.format(*label_encoder.char.get_token_stats())
print("- {:<15} types={:<10} tokens={:<10}".format("char", types, tokens))
print()
print("::: Tasks :::")
print()
for task, le in label_encoder.tasks.items():
print("- {:<15} target={:<6} level={:<6} vocab={:<6}"
.format(task, le.target, le.level, len(le)))
print()
trainset = Dataset(settings, reader, label_encoder)
devset = None
if settings.dev_path:
devset = Dataset(settings, Reader(settings, settings.dev_path), label_encoder)
else:
logging.warning("No devset: cannot monitor/optimize training")
if not settings.existing_model:
# model
model = SimpleModel(
label_encoder, settings.tasks,
settings.wemb_dim, settings.cemb_dim, settings.hidden_size,
settings.num_layers, cell=settings.cell,
# dropout
dropout=settings.dropout, word_dropout=settings.word_dropout,
# word embeddings
merge_type=settings.merge_type, cemb_type=settings.cemb_type,
cemb_layers=settings.cemb_layers, custom_cemb_cell=settings.custom_cemb_cell,
# lm joint loss
include_lm=settings.include_lm, lm_shared_softmax=settings.lm_shared_softmax,
# decoder
scorer=settings.scorer, linear_layers=settings.linear_layers)
# pretrain(/load pretrained) embeddings
if model.wemb is not None:
if settings.pretrain_embeddings:
print("Pretraining word embeddings")
wemb_reader = Reader(
settings, settings.input_path, settings.dev_path, settings.test_path)
weight = get_pretrained_embeddings(
wemb_reader, label_encoder, vector_size=settings.wemb_dim,
window=5, negative=5, min_count=1)
model.wemb.weight.data = torch.tensor(weight, dtype=torch.float32)
elif settings.load_pretrained_embeddings:
print("Loading pretrained embeddings")
if not os.path.isfile(settings.load_pretrained_embeddings):
print("Couldn't find pretrained eembeddings in: {}".format(
settings.load_pretrained_embeddings))
initialization.init_pretrained_embeddings(
settings.load_pretrained_embeddings, label_encoder.word, model.wemb)
# load pretrained weights
if settings.load_pretrained_encoder:
model.init_from_encoder(pie.Encoder.load(settings.load_pretrained_encoder))
# freeze embeddings
if settings.freeze_embeddings:
model.wemb.weight.requires_grad = False
### At this point the model has been initialized and is ready to be trained ###
model.to(settings.device)
print("::: Model :::")
print()
print(model)
print()
print("::: Model parameters :::")
print()
trainable = sum(p.nelement() for p in model.parameters() if p.requires_grad)
total = sum(p.nelement() for p in model.parameters())
print("{}/{} trainable/total".format(trainable, total))
print()
# training
print("Starting training")
running_time = time.time()
trainer = Trainer(settings, model, trainset, reader.get_nsents())
loss = None
try:
model.train()
loss = trainer.train_epochs(settings.epochs, devset=devset)
except KeyboardInterrupt:
print("Stopping training")
running_time = time.time() - running_time
if settings.test_path:
model.eval()
print("Evaluating model on test set")
testset = Dataset(settings, Reader(settings, settings.test_path), label_encoder)
for task in model.evaluate(testset, trainset).values():
task.print_summary()
# save model
fpath, infix = get_fname_infix(settings)
if not settings.run_test and settings.modelpath != "":
fpath = model.save(fpath, infix=infix, settings=settings)
print("Saved best model to: [{}]".format(fpath))
if False: # might have to add something like settings.create_csv
model.eval()
if devset is not None and not settings.run_test:
scorers = model.evaluate(devset, trainset)
scores = []
for task in sorted(scorers):
scorer = scorers[task]
result = scorer.get_scores()
for acc in result:
scores.append('{}-{}:{:.6f}'.format(
acc, task, result[acc]['accuracy']))
scores.append('{}-{}-support:{}'.format(
acc, task, result[acc]['support']))
path = '{}.results.{}.csv'.format(
settings.modelname, '-'.join(get_targets(settings)))
with open(path, 'a') as f:
line = [infix, str(seed), str(running_time)]
line += scores
f.write('{}\n'.format('\t'.join(line)))
print("Bye!")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('config_path', nargs='?', default='config.json')
parser.add_argument('--opt_path', help='Path to optimization file (see opt.json)')
parser.add_argument('--n_iter', type=int, default=20)
args = parser.parse_args()
settings = settings_from_file(args.config_path)
if args.opt_path:
opt = optimize.read_opt(args.opt_path)
optimize.run_optimize(run, settings, opt, args.n_iter)
else:
run(settings)