forked from YangXuanyue/pytorch-e2e-coref
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_trainer.py
More file actions
executable file
·174 lines (138 loc) · 6.31 KB
/
Copy pathdecoder_trainer.py
File metadata and controls
executable file
·174 lines (138 loc) · 6.31 KB
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
import configs
from modules import SelfAttendedDecoder, LangModel, Decoder
from model_utils import *
import os
from log import log
import time
import data_utils
import re
from itertools import chain
from allennlp.training.optimizers import DenseSparseAdam
class DecoderTrainer:
def __init__(self):
self.decoder = SelfAttendedDecoder(vocab=data_utils.vocab, device_id=configs.decoder_device_id)
# self.decoder.apply(init_weights)
self.xe_loss = nn.CrossEntropyLoss(ignore_index=data_utils.vocab.padding_id)
self.optimizer = DenseSparseAdam(self.decoder.parameters(), lr=configs.adam_lr) # , lr=configs.lr)
# self.optimizer = optim.ASGD(self.model.parameters(), lr=configs.lr, weight_decay=configs.l2_weight_decay)
self.lr_scheduler = lr_scheduler.ReduceLROnPlateau(
self.optimizer, 'min',
patience=configs.lr_scheduler_patience,
factor=configs.lr_scheduler_factor, verbose=True
)
self.epoch_idx = 0
self.min_ppl = 1000.
self.ckpt_path = 'decoder.pretrained.params'
def train(self):
# self.load_ckpt()
start_epoch_idx = self.epoch_idx
for epoch_idx in range(start_epoch_idx, configs.epoch_num):
self.epoch_idx = epoch_idx
log(f'starting epoch {epoch_idx}')
log('training')
self.decoder.train()
avg_epoch_loss = 0.
batch_num = 0
next_logging_pct = .5
start_time = time.time()
for pct, (report_batch, report_len_batch) in data_utils.gen_report_batches('train'):
batch_num += 1
self.optimizer.zero_grad()
max_report_len, batch_size = report_batch.shape
# [max_report_len - 1, batch_size, vocab_size]
word_logits_seq_batch = self.decoder.run_lang_model(
# [max_report_len, batch_size], [batch_size]
report_batch, report_len_batch
)
loss = self.xe_loss(
# [(max_report_len - 1) * batch_size, vocab_size]
word_logits_seq_batch.view(-1, data_utils.vocab.size),
# [(max_report_len - 1) * batch_size]
report_batch[1:, :].contiguous().view(-1).to(torch.device(configs.decoder_device_id))
)
# print(torch.argmax(output_batch[:50, :5, :], dim=-1))
loss.backward()
self.optimizer.step()
avg_epoch_loss += loss.item()
if pct >= next_logging_pct:
log(
f'{int(pct)}%, avg_train_loss: {avg_epoch_loss / batch_num}, '
f'time: {time.time() - start_time}'
)
next_logging_pct += 10.
avg_epoch_loss /= batch_num
log(
f'avg_train_loss: {avg_epoch_loss}\n'
f'avg_train_time: {time.time() - start_time}'
)
with torch.no_grad():
log('validating')
self.decoder.eval()
batch_num = 0
avg_epoch_ppl = 0
next_logging_pct = 10.
start_time = time.time()
for pct, (report_batch, report_len_batch) in data_utils.gen_report_batches('test'):
batch_num += 1
max_report_len, batch_size = report_batch.shape
# [max_report_len - 1, batch_size, vocab_size]
word_logits_seq_batch = self.decoder.run_lang_model(
# [max_report_len, batch_size], [batch_size]
report_batch, report_len_batch
)
loss = self.xe_loss(
# [(max_report_len - 1) * batch_size, vocab_size]
word_logits_seq_batch.view(-1, data_utils.vocab.size),
# [(max_report_len - 1) * batch_size]
report_batch[1:, :].contiguous().view(-1).to(torch.device(configs.decoder_device_id))
)
# print(torch.argmax(output_batch[:50, :5, :], dim=-1))
avg_epoch_ppl += math.exp(loss.item())
if pct >= next_logging_pct:
log(
f'{int(pct)}%, avg_dev_ppl: {avg_epoch_ppl / batch_num}, '
f'time: {time.time() - start_time}'
)
next_logging_pct += 10.
avg_epoch_ppl /= batch_num
self.lr_scheduler.step(avg_epoch_ppl)
log(
f'avg_dev_time: {time.time() - start_time}\n'
f'avg_dev_ppl: {avg_epoch_ppl}'
)
if avg_epoch_ppl < self.min_ppl:
self.min_ppl = avg_epoch_ppl
self.save_ckpt()
def get_ckpt(self):
return {
'epoch_idx': self.epoch_idx,
'min_ppl': self.min_ppl,
'decoder': self.decoder.state_dict(),
'optimizer': self.optimizer.state_dict(),
'lr_scheduler': self.lr_scheduler.state_dict()
}
def set_ckpt(self, ckpt_dict):
self.epoch_idx = ckpt_dict['epoch_idx'] + 1
self.min_ppl = ckpt_dict['min_ppl']
if configs.uses_gumbel_softmax:
ckpt_dict['decoder']['embedder.weight'] = \
ckpt_dict['decoder']['embedder.weight'].to(torch.device(configs.decoder_device_id))
else:
ckpt_dict['decoder']['embedder.weight'] = \
ckpt_dict['decoder']['embedder.weight'].cpu()
self.decoder.load_state_dict(ckpt_dict['decoder'])
if configs.uses_new_optimizer:
self.optimizer.load_state_dict(ckpt_dict['optimizer'])
self.lr_scheduler.load_state_dict(ckpt_dict['lr_scheduler'])
del ckpt_dict
torch.cuda.empty_cache()
ckpt = property(get_ckpt, set_ckpt)
def save_ckpt(self):
torch.save(self.ckpt, f=self.ckpt_path)
print(f'saved checkpoint to {self.ckpt_path}')
def load_ckpt(self):
self.ckpt = torch.load(self.ckpt_path)
print(f'loaded checkpoint from {self.ckpt_path}')
if __name__ == '__main__':
decoder_trainer = DecoderTrainer()
decoder_trainer.train()