-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbc_par.py
305 lines (235 loc) · 11.7 KB
/
bc_par.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
301
302
303
304
305
import copy
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Normal, TransformedDistribution, constraints
from torch.distributions.transforms import Transform
class TanhTransform(Transform):
r"""
Transform via the mapping :math:`y = \tanh(x)`.
It is equivalent to
```
ComposeTransform([AffineTransform(0., 2.), SigmoidTransform(), AffineTransform(-1., 2.)])
```
However this might not be numerically stable, thus it is recommended to use `TanhTransform`
instead.
Note that one should use `cache_size=1` when it comes to `NaN/Inf` values.
"""
domain = constraints.real
codomain = constraints.interval(-1.0, 1.0)
bijective = True
sign = +1
@staticmethod
def atanh(x):
return 0.5 * (x.log1p() - (-x).log1p())
def __eq__(self, other):
return isinstance(other, TanhTransform)
def _call(self, x):
return x.tanh()
def _inverse(self, y):
# We do not clamp to the boundary here as it may degrade the performance of certain algorithms.
# one should use `cache_size=1` instead
return self.atanh(y)
def log_abs_det_jacobian(self, x, y):
# We use a formula that is more numerically stable, see details in the following link
# https://github.com/tensorflow/probability/blob/master/tensorflow_probability/python/bijectors/tanh.py#L69-L80
return 2. * (math.log(2.) - x - F.softplus(-2. * x))
class MLPNetwork(nn.Module):
def __init__(self, input_dim, output_dim, hidden_size=256):
super(MLPNetwork, self).__init__()
self.network = nn.Sequential(
nn.Linear(input_dim, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, output_dim),
)
def forward(self, x):
return self.network(x)
class Policy(nn.Module):
def __init__(self, state_dim, action_dim, max_action, hidden_size=256):
super(Policy, self).__init__()
self.action_dim = action_dim
self.max_action = max_action
self.network = MLPNetwork(state_dim, action_dim * 2, hidden_size)
def forward(self, x, get_logprob=False):
mu_logstd = self.network(x)
mu, logstd = mu_logstd.chunk(2, dim=1)
logstd = torch.clamp(logstd, -20, 2)
std = logstd.exp()
dist = Normal(mu, std)
transforms = [TanhTransform(cache_size=1)]
dist = TransformedDistribution(dist, transforms)
action = dist.rsample()
if get_logprob:
logprob = dist.log_prob(action).sum(axis=-1, keepdim=True)
else:
logprob = None
mean = torch.tanh(mu)
return action * self.max_action, logprob, mean * self.max_action
def AvgL1Norm(x, eps=1e-8):
return x/x.abs().mean(-1,keepdim=True).clamp(min=eps)
class Encoder(nn.Module):
def __init__(self, state_dim, action_dim, zs_dim=256, hdim=256, activ=F.elu):
super(Encoder, self).__init__()
self.activ = activ
# state encoder
self.zs1 = nn.Linear(state_dim, hdim)
self.zs2 = nn.Linear(hdim, hdim)
self.zs3 = nn.Linear(hdim, zs_dim)
# state-action encoder
self.zsa1 = nn.Linear(zs_dim + action_dim, hdim)
self.zsa2 = nn.Linear(hdim, hdim)
self.zsa3 = nn.Linear(hdim, zs_dim)
def zs(self, state):
zs = self.activ(self.zs1(state))
zs = self.activ(self.zs2(zs))
zs = AvgL1Norm(self.zs3(zs))
return zs
def zsa(self, zs, action):
zsa = self.activ(self.zsa1(torch.cat([zs, action], 1)))
zsa = self.activ(self.zsa2(zsa))
zsa = self.zsa3(zsa)
return zsa
class DoubleQFunc(nn.Module):
def __init__(self, state_dim, action_dim, hidden_size=256):
super(DoubleQFunc, self).__init__()
self.network1 = MLPNetwork(state_dim + action_dim, 1, hidden_size)
self.network2 = MLPNetwork(state_dim + action_dim, 1, hidden_size)
def forward(self, state, action):
x = torch.cat((state, action), dim=1)
return self.network1(x), self.network2(x)
class BCPAR(object):
def __init__(self,
config,
device,
target_entropy=None,
):
self.config= config
self.device = device
self.discount = config['gamma']
self.tau = config['tau']
self.target_entropy = target_entropy if target_entropy else -config['action_dim']
self.update_interval = config['update_interval']
self.total_it = 0
# aka critic
self.q_funcs = DoubleQFunc(config['state_dim'], config['action_dim'], hidden_size=config['hidden_sizes']).to(self.device)
self.target_q_funcs = copy.deepcopy(self.q_funcs)
self.target_q_funcs.eval()
for p in self.target_q_funcs.parameters():
p.requires_grad = False
# aka actor
self.policy = Policy(config['state_dim'], config['action_dim'], config['max_action'], hidden_size=config['hidden_sizes']).to(self.device)
# aka encoder
self.encoder = Encoder(config['state_dim'], config['action_dim']).to(self.device)
self.encoder_target = copy.deepcopy(self.encoder)
self.encoder_target.eval()
# aka temperature
if config['temperature_opt']:
self.log_alpha = torch.zeros(1, requires_grad=True, device=self.device)
else:
self.log_alpha = torch.log(torch.FloatTensor([0.2])).to(self.device)
self.q_optimizer = torch.optim.Adam(self.q_funcs.parameters(), lr=config['critic_lr'])
self.policy_optimizer = torch.optim.Adam(self.policy.parameters(), lr=config['actor_lr'])
self.temp_optimizer = torch.optim.Adam([self.log_alpha], lr=config['actor_lr'])
self.encoder_optimizer = torch.optim.Adam(self.encoder.parameters(), lr=config['actor_lr'])
def select_action(self, state, test=True):
with torch.no_grad():
action, _, mean = self.policy(torch.Tensor(state).view(1,-1).to(self.device))
if test:
return mean.squeeze().cpu().numpy()
else:
return action.squeeze().cpu().numpy()
def update_target(self):
"""moving average update of target networks"""
with torch.no_grad():
for target_q_param, q_param in zip(self.target_q_funcs.parameters(), self.q_funcs.parameters()):
target_q_param.data.copy_(self.tau * q_param.data + (1.0 - self.tau) * target_q_param.data)
# update encoder
for target_q_param, q_param in zip(self.encoder_target.parameters(), self.encoder.parameters()):
target_q_param.data.copy_(self.tau * q_param.data + (1.0 - self.tau) * target_q_param.data)
def update_q_functions(self, state_batch, action_batch, reward_batch, nextstate_batch, not_done_batch, writer=None):
with torch.no_grad():
nextaction_batch, logprobs_batch, _ = self.policy(nextstate_batch, get_logprob=True)
q_t1, q_t2 = self.target_q_funcs(nextstate_batch, nextaction_batch)
# take min to mitigate positive bias in q-function training
q_target = torch.min(q_t1, q_t2)
if self.config['entropy_backup']:
value_target = reward_batch + not_done_batch * self.discount * (q_target - self.alpha * logprobs_batch)
else:
value_target = reward_batch + not_done_batch * self.discount * q_target
q_1, q_2 = self.q_funcs(state_batch, action_batch)
if writer is not None and self.total_it % 5000 == 0:
writer.add_scalar('train/q1', q_1.mean(), self.total_it)
writer.add_scalar('train/logprob', logprobs_batch.mean(), self.total_it)
loss = F.mse_loss(q_1, value_target) + F.mse_loss(q_2, value_target)
return loss
def update_policy_and_temp(self, state_batch, src_state_batch, src_action_batch):
action_batch, logprobs_batch, _ = self.policy(state_batch, get_logprob=True)
q_b1, q_b2 = self.q_funcs(state_batch, action_batch)
qval_batch = torch.min(q_b1, q_b2)
p_w = self.config['weight'] / qval_batch.abs().mean().detach()
pred_src_act, _, _ = self.policy(src_state_batch, get_logprob=True)
policy_loss = p_w * (self.alpha * logprobs_batch - qval_batch).mean() + F.mse_loss(pred_src_act, src_action_batch)
temp_loss = -self.alpha * (logprobs_batch.detach() + self.target_entropy).mean()
return policy_loss, temp_loss
def update_encoder(self, state_batch, action_batch, nextstate_batch, writer=None):
with torch.no_grad():
next_zs = self.encoder.zs(nextstate_batch)
zs = self.encoder.zs(state_batch)
pred_zs = self.encoder.zsa(zs, action_batch)
encoder_loss = F.mse_loss(pred_zs, next_zs)
self.encoder_optimizer.zero_grad()
encoder_loss.backward()
self.encoder_optimizer.step()
if writer is not None and self.total_it % 5000 == 0:
writer.add_scalar('train/encoder loss', encoder_loss, global_step=self.total_it)
def train(self, src_replay_buffer, tar_replay_buffer, batch_size=128, writer=None):
self.total_it += 1
# update the encoder and the agent only given some certain amount of data
if src_replay_buffer.size < 500 or tar_replay_buffer.size < 500:
return
src_state, src_action, src_next_state, src_reward, src_not_done = src_replay_buffer.sample(batch_size)
tar_state, tar_action, tar_next_state, tar_reward, tar_not_done = tar_replay_buffer.sample(batch_size)
# update encoder
if self.total_it % 200 == 0:
tar_s, tar_a, tar_ns, _, _ = tar_replay_buffer.sample(batch_size // 2)
self.update_encoder(tar_s, tar_a, tar_ns, writer)
# derive representation deviation
with torch.no_grad():
next_src_zs = self.encoder_target.zs(src_next_state)
src_zs = self.encoder_target.zs(src_state)
pred_src_zs = self.encoder_target.zsa(src_zs, src_action)
distance = ((pred_src_zs - next_src_zs)**2).mean(dim=-1, keepdim=True)
src_reward -= self.config['beta'] * distance
if writer is not None and self.total_it % 5000 == 0:
writer.add_scalar('train/distance', distance.mean(), self.total_it)
writer.add_scalar('train/src reward', src_reward.mean(), self.total_it)
state = torch.cat([src_state, tar_state], 0)
action = torch.cat([src_action, tar_action], 0)
next_state = torch.cat([src_next_state, tar_next_state], 0)
reward = torch.cat([src_reward, tar_reward], 0)
not_done = torch.cat([src_not_done, tar_not_done], 0)
q_loss_step = self.update_q_functions(state, action, reward, next_state, not_done, writer)
self.q_optimizer.zero_grad()
q_loss_step.backward()
self.q_optimizer.step()
self.update_target()
# update policy and temperature parameter
for p in self.q_funcs.parameters():
p.requires_grad = False
pi_loss_step, a_loss_step = self.update_policy_and_temp(state, src_state, src_action)
self.policy_optimizer.zero_grad()
pi_loss_step.backward()
self.policy_optimizer.step()
if self.config['temperature_opt']:
self.temp_optimizer.zero_grad()
a_loss_step.backward()
self.temp_optimizer.step()
for p in self.q_funcs.parameters():
p.requires_grad = True
@property
def alpha(self):
return self.log_alpha.exp()