-
Notifications
You must be signed in to change notification settings - Fork 4
/
rl.py
executable file
·295 lines (270 loc) · 9.82 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
""" RL training utilities"""
import math
from time import time
from datetime import timedelta
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 training import BasicPipeline
label = [0, 0, 0, 0, 0, 0]
label_mask = [1, 1, 1, 1, 1, 1]
def rl_edu_to_sentence(edus, ind):
ind = list(filter(lambda x: x < len(edus), ind))
new_ind = []
for i in ind:
try:
i = i.item()
except:
pass
new_ind.append(i)
ind = new_ind
ind = list(sorted(ind))
#print(ind)
ret = ''
for i in ind:
ret += edus[i] + ' '
rets = ret[:-1].split()
masks = [1] * len(rets)
return rets, masks
def a2c_validate(agent, abstractor, loader):
agent.eval()
start = time()
print('start running validation...', end='')
avg_reward = 0
i = 0
with torch.no_grad():
for art_batch, abs_batch, sent_batch in loader:
print(i)
ext_sents = []
ext_inds = []
masks = []
dirty = []
for raw_arts, sent_labels in zip(art_batch, sent_batch):
indices = agent(raw_arts, sent_labels)
ext_inds += [(len(ext_sents), len(indices) - 1)]
assert indices[-1][-1].item() == len(raw_arts) + 1
tmp_stop = indices[-1][-1].item()
tmp_truncate = tmp_stop - 1
str_arts = list(map(lambda x: ' '.join(x), raw_arts))
for idx in indices:
t, m = rl_edu_to_sentence(str_arts, idx)
if t == []:
assert len(idx) == 1
id = idx[0].item()
if id == tmp_truncate:
dirty.append(len(ext_sents))
ext_sents.append(label)
masks.append(label_mask)
else:
if idx[-1].item() != tmp_stop:
ext_sents.append(t)
masks.append(m)
all_summs = abstractor(ext_sents, masks)
for d in dirty:
all_summs[d] = []
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)
i += 1
if i % 100 == 1:
print(avg_reward/i, i)
'''
with open('./compare/rl/' + str(i - 1) + '.dec', 'w') as f:
for s in summs:
s = ' '.join(s)
f.write(s + '\n')
'''
#if i > 1000:
# break
avg_reward /= (i/100)
print('finished in {}! avg reward: {:.2f}'.format(
timedelta(seconds=int(time()-start)), avg_reward))
return {'reward': avg_reward}
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):
opt.zero_grad()
indices = []
probs = []
baselines = []
ext_sents = []
masks = []
art_batch, abs_batch, sent_label_batch = next(loader)
leng = []
avg_leng = []
dirty = []
time1 = time()
for raw_arts, sent_labels in zip(art_batch, sent_label_batch):
(inds, ms), bs = agent(raw_arts, sent_labels)
assert inds[-1][-1].item() == len(raw_arts) + 1
baselines.append(bs)
indices.append(inds)
probs.append(ms)
try:
avg_leng.append(sum(list(map(lambda x: len(x) - 1, inds[:-1])))/(len(inds) - 1))
except:
pass
leng.append(len(inds) - 1)
tmp_stop = inds[-1][-1].item()
tmp_truncate = tmp_stop - 1
str_arts = list(map(lambda x: ' '.join(x), raw_arts))
for idx in inds:
t, m = rl_edu_to_sentence(str_arts, idx)
assert len(t) == len(m)
if t == []:
assert len(idx) == 1
id = idx[0].item()
if id == tmp_truncate:
dirty.append(len(ext_sents))
ext_sents.append(label)
masks.append(label_mask)
else:
if idx[-1].item() != tmp_stop:
ext_sents.append(t)
masks.append(m)
print('长度:', leng)
leng = list(map(lambda x: x[0] - len(x[1]), zip(leng, abs_batch)))
avg_dis = sum(leng)/len(leng)
print('平均差距:', avg_dis)
print('平均edu数量:', sum(avg_leng)/len(avg_leng))
time2 = time()
with torch.no_grad():
summaries = abstractor(ext_sents, masks)
for d in dirty:
summaries[d] = []
time3 = time()
i = 0
rewards = []
avg_reward = 0
for inds, abss in zip(indices, abs_batch):
rs_abs = []
rs_len = []
abs_num = min(len(inds) - 1, len(abss))
for j in range(abs_num):
rs_abs.append(reward_fn(summaries[i+j], abss[j]))
rs_len.append(len(inds[j]))
rs_zero = []
for j in range(max(0, len(inds)-1-len(abss))):
rs_zero += [0] * len(inds[j + abs_num])
rs_zero += [0] * (len(inds[-1]) - 1)
rs_final = stop_coeff*stop_reward_fn(list(concat(summaries[i:i+len(inds)-1])),
list(concat(abss)))
avg_reward += rs_final/stop_coeff
i += len(inds)-1
disc_rs = [rs_final]
R = rs_final
for _ in rs_zero:
R = R * gamma
disc_rs.append(R)
for r, leng in zip(rs_abs, rs_len):
R = r + R * gamma
disc_rs += [R] * leng
disc_rs = list(reversed(disc_rs))
assert len(disc_rs) == sum(list(map(lambda x: len(x), inds)))
rewards += disc_rs
indices = list(concat(list(concat(indices))))
probs = list(concat(probs))
baselines = list(concat(baselines))
# standardize rewards
reward = torch.Tensor(rewards).to(baselines[0].device)
assert len(reward) == len(probs) and len(baselines) == len(probs) and len(baselines) == len(indices)
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/len(indices))) # divide by T*B
critic_loss = F.mse_loss(baseline, reward)
time4 = time()
# 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)
#print(avg_reward)
log_dict['advantage'] = avg_advantage.item()/len(indices)
log_dict['mse'] = critic_loss.item()
assert not math.isnan(log_dict['grad_norm'])
time5 = time()
print(time2-time1, time3-time2, time4-time3,time5-time4)
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
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 A2CPipeline(BasicPipeline):
def __init__(self, name,
net, abstractor,
train_batcher, val_batcher,
optim, grad_fn,
reward_fn, gamma,
stop_reward_fn, stop_coeff, model_path=None):
self.name = name
self._net = net
self._train_batcher = train_batcher
self._val_batcher = val_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?
if model_path is not None:
self.load(model_path)
self.param_debug()
def batches(self):
raise NotImplementedError('A2C does not use batcher')
def train_step(self):
# 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
)
return log_dict
def validate(self):
return a2c_validate(self._net, self._abstractor, self._val_batcher)
#return 0
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