-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathrl.py
185 lines (167 loc) · 6.15 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
""" 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
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 in loader:
ext_sents = []
ext_inds = []
for raw_arts in art_batch:
indices = agent(raw_arts)
ext_inds += [(len(ext_sents), len(indices)-1)]
ext_sents += [raw_arts[idx.item()]
for idx in indices if idx.item() < len(raw_arts)]
all_summs = abstractor(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)
i += 1
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 = []
art_batch, abs_batch = next(loader)
for raw_arts in art_batch:
(inds, ms), bs = agent(raw_arts)
baselines.append(bs)
indices.append(inds)
probs.append(ms)
ext_sents += [raw_arts[idx.item()]
for idx in inds if idx.item() < len(raw_arts)]
with torch.no_grad():
summaries = abstractor(ext_sents)
i = 0
rewards = []
avg_reward = 0
for inds, abss in zip(indices, abs_batch):
rs = ([reward_fn(summaries[i+j], 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])),
list(concat(abss)))])
assert len(rs) == len(inds)
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/len(indices))) # divide by T*B
critic_loss = F.mse_loss(baseline, reward)
# 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()
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 A2CPipeline(BasicPipeline):
def __init__(self, name,
net, abstractor,
train_batcher, val_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._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?
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)
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