-
Notifications
You must be signed in to change notification settings - Fork 4
/
rl.py
300 lines (274 loc) · 11.3 KB
/
rl.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
""" RL training utilities"""
import math
import pickle
from time import time
from datetime import timedelta
import os
from os.path import join
from toolz.sandbox.core import unzip
from cytoolz import concat
import numpy as np
import torch
from torch.nn import functional as F
from torch import autograd
from torch.nn.utils import clip_grad_norm_
from metric import compute_rouge_l, compute_rouge_n
from model.ScoreAgent import is_quote
from training import BasicPipeline
from utils import calc_official_rouge
def a2c_validate(agent, abstractor, loader, save_dir, n_epochs, name):
official_eval = True
n_epochs -= 1
agent.eval()
start = time()
print('start running validation...', end='')
avg_reward = 0
i = 0
n_sent = []
sent_idx_l = []
with torch.no_grad():
for art_batch, abs_batch, name_batch in loader:
ext_sents = []
ext_inds = []
for raw_arts, dname in zip(art_batch, name_batch):
# split by ###
raw_arts_l = [[]]
raw_arts_filter = []
for sent in raw_arts:
if sent[0] == '###':
raw_arts_l.append([])
elif is_quote(sent):
continue
else:
raw_arts_l[-1].append(sent)
raw_arts_filter.append(sent)
raw_arts_l = [raw_arts for raw_arts in raw_arts_l if len(raw_arts) > 0]
indices = agent(raw_arts_l, dname=dname, dataset=name)
# indices = agent(raw_arts)
ext_inds += [(len(ext_sents), len(indices) - 1)]
n_sent.append(len(indices) - 1)
ext_sents += [raw_arts_filter[idx.item()]
for idx in indices if idx.item() < len(raw_arts_filter)]
sent_idx_l.append([idx.item() for idx in indices if idx.item() < len(raw_arts_filter)])
# ext or ext+abs
# all_summs = abstractor(ext_sents)
all_summs = ext_sents
for (j, n), abs_sents in zip(ext_inds, abs_batch):
summs = all_summs[j:j + n]
# python ROUGE-1 (not official evaluation)
avg_reward += compute_rouge_n(list(concat(summs)),
list(concat(abs_sents)), n=1)
if official_eval:
if not os.path.exists(join(save_dir, 'dec_all', f'{name}-{str(n_epochs)}', 'dec')):
os.system(f"mkdir -p {join(save_dir, 'dec_all', f'{name}-{str(n_epochs)}', 'dec')}")
fname = join(save_dir, 'dec_all', f'{name}-{str(n_epochs)}', 'dec', f'{i}.dec')
with open(fname, 'w') as o:
o.write(' '.join(word for word in list(concat(summs))))
i += 1
avg_reward /= (i / 100)
print(f'finished in {timedelta(seconds=int(time() - start))}! unofficial ROUGE-1: {avg_reward:.2f}, '
f'avg n_sent: {np.mean(n_sent):.2f}')
metric = {'reward': avg_reward}
if official_eval:
fname = join(save_dir, 'dec_all', f'{name}-{str(n_epochs)}', 'sent_idx_l.pkl')
pickle.dump(sent_idx_l, open(fname, 'wb'))
official_rouge = calc_official_rouge(join(save_dir, 'dec_all', f'{name}-{str(n_epochs)}', 'dec'), name)
metric.update(official_rouge)
metric['reward'] = metric['R-1'] # use official R-1 in saved_model name now
return metric
def a2c_train_step(agent, abstractor, loader, opt, grad_fn,
gamma=0.99, reward_fn=compute_rouge_l,
stop_reward_fn=compute_rouge_n(n=1), stop_coeff=1.0, n_epochs=1):
opt.zero_grad()
indices = []
probs = []
baselines = []
ext_sents = []
n_sent = []
diversity_l = []
art_batch, abs_batch, name_batch = next(loader)
for raw_arts, dname in zip(art_batch, name_batch):
# split by ###
raw_arts_l = [[]] # each ele is a doc (list of sent)
raw_arts_filter = [] # each ele is a sent
for sent in raw_arts:
if sent[0] == '###':
raw_arts_l.append([])
elif is_quote(sent):
continue
else:
raw_arts_l[-1].append(sent)
raw_arts_filter.append(sent)
raw_arts_l = [raw_arts for raw_arts in raw_arts_l if len(raw_arts) > 0]
(inds, ms), bs, diversity = agent(raw_arts_l, dname=dname, dataset='train', n_epochs=n_epochs)
diversity_l.append(diversity)
n_sent.append(len(inds) - 1)
baselines.append(bs)
indices.append(inds)
probs.append(ms)
ext_sents += [raw_arts_filter[idx.item()]
for idx in inds if idx.item() < len(raw_arts_filter)]
# ext or ext+abs
summaries = ext_sents
# with torch.no_grad():
# summaries = abstractor(ext_sents)
i = 0
rewards = []
avg_reward = 0
max_len = 10000
for inds, abss, raw_arts, diversity in zip(indices, abs_batch, art_batch, diversity_l):
# ROUGE-L each sentence against whole ref / one ref sentence abss[j]
# 0 for sentences more than len(abss)
# 10 * ROUGE-1 whole_ext vs. whole_ref for rewarding STOP
rs = ([reward_fn(summaries[i + j], list(concat(abss))) # abss[j]
for j in range(min(len(inds) - 1, len(abss)))]
+ [0 for _ in range(max(0, len(inds) - 1 - len(abss)))]
+ [stop_coeff * stop_reward_fn(
list(concat(summaries[i:i + len(inds) - 1]))[:max_len],
list(concat(abss)))])
assert len(rs) == len(inds)
# avg_reward += np.mean(mmr_scores)
r1 = stop_reward_fn(list(concat(summaries[i:i + len(inds) - 1]))[:100], list(concat(abss)))
avg_reward += r1
# avg_reward += rs[-1] / stop_coeff
i += len(inds) - 1
# compute discounted rewards
R = 0
disc_rs = []
for r in rs[::-1]:
R = r + gamma * R
disc_rs.insert(0, R)
rewards += disc_rs
indices = list(concat(indices))
probs = list(concat(probs))
baselines = list(concat(baselines))
# standardize rewards
reward = torch.Tensor(rewards).to(baselines[0].device)
reward = (reward - reward.mean()) / (
reward.std() + float(np.finfo(np.float32).eps))
baseline = torch.cat(baselines).squeeze()
avg_advantage = 0
losses = []
for action, p, r, b in zip(indices, probs, reward, baseline):
advantage = r - b
avg_advantage += advantage
# losses.append(-p.log_prob(action) * advantage)
losses.append(-p.log_prob(action) * (advantage / len(indices))) # divide by T*B
critic_loss = F.mse_loss(baseline, reward).reshape(1)
# backprop and update
autograd.backward(
[critic_loss] + losses,
[torch.ones(1).to(critic_loss.device)] * (1 + len(losses))
)
grad_log = grad_fn()
opt.step()
log_dict = {}
log_dict.update(grad_log)
log_dict['reward'] = avg_reward / len(art_batch)
log_dict['advantage'] = avg_advantage.item() / len(indices)
log_dict['mse'] = critic_loss.item()
log_dict['avg_n_sent'] = np.mean(n_sent)
assert not math.isnan(log_dict['grad_norm'])
return log_dict
def get_grad_fn(agent, clip_grad, max_grad=1e2):
""" monitor gradient for each sub-component"""
params = [p for p in agent.parameters()]
def f():
grad_log = {}
for n, m in agent.named_children():
tot_grad = 0
for p in m.parameters():
if p.grad is not None:
tot_grad += p.grad.norm(2) ** 2
tot_grad = tot_grad ** (1 / 2)
grad_log['grad_norm' + n] = tot_grad.item()
grad_norm = clip_grad_norm_(
[p for p in params if p.requires_grad], clip_grad)
# grad_norm = grad_norm.item()
if max_grad is not None and grad_norm >= max_grad:
print('WARNING: Exploding Gradients {:.2f}'.format(grad_norm))
grad_norm = max_grad
grad_log['grad_norm'] = grad_norm
return grad_log
return f
class EMA:
def __init__(self, gamma, model, prev_shadow=None):
super(EMA, self).__init__()
self.gamma = gamma
self.model = model
if prev_shadow is not None:
self.shadow = prev_shadow
else:
self.shadow = {}
for name, para in model.named_parameters():
if para.requires_grad:
self.shadow[name] = para.clone()
def update(self):
for name, para in self.model.named_parameters():
if para.requires_grad:
self.shadow[name] = (1.0 - self.gamma) * para + self.gamma * self.shadow[name]
def swap_parameters(self):
# swap the shadow parameters and model parameters.
for name, para in self.model.named_parameters():
if para.requires_grad:
para.data += self.shadow[name].data
self.shadow[name].data = self.shadow[name].data.neg_()
self.shadow[name].data += para.data
para.data -= self.shadow[name].data
def state_dict(self):
return self.shadow
class A2CPipeline(BasicPipeline):
def __init__(self, name,
net, abstractor,
train_batcher, val_batcher, test_batcher,
optim, grad_fn,
reward_fn, gamma,
stop_reward_fn, stop_coeff):
self.name = name
self._net = net
self._train_batcher = train_batcher
self._val_batcher = val_batcher
self._test_batcher = test_batcher
self._opt = optim
self._grad_fn = grad_fn
self._abstractor = abstractor
self._gamma = gamma
self._reward_fn = reward_fn
self._stop_reward_fn = stop_reward_fn
self._stop_coeff = stop_coeff
self._n_epoch = 0 # epoch not very useful?
self.using_ema = None
if self.using_ema is not None:
self.ema = EMA(gamma=.1, model=self._net, prev_shadow=None)
def batches(self):
raise NotImplementedError('A2C does not use batcher')
def train_step(self):
if self.using_ema is not None and self.using_ema:
self.ema.swap_parameters()
self.using_ema = False
self._n_epoch += 1
# forward pass of model
self._net.train()
log_dict = a2c_train_step(
self._net, self._abstractor,
self._train_batcher,
self._opt, self._grad_fn,
self._gamma, self._reward_fn,
self._stop_reward_fn, self._stop_coeff, self._n_epoch
)
if self.using_ema is not None:
self.ema.update()
return log_dict
def validate(self, save_dir, n_epochs, name):
if self.using_ema is not None and not self.using_ema:
self.ema.swap_parameters()
self.using_ema = True
if name == 'val':
return a2c_validate(self._net, self._abstractor, self._val_batcher, save_dir, n_epochs, name)
if name == 'test':
return a2c_validate(self._net, self._abstractor, self._test_batcher, save_dir, n_epochs, name)
def checkpoint(self, *args, **kwargs):
# explicitly use inherited function in case I forgot :)
return super().checkpoint(*args, **kwargs)
def terminate(self):
pass # No extra processs so do nothing