-
Notifications
You must be signed in to change notification settings - Fork 1
/
SRPO.py
187 lines (159 loc) · 7.29 KB
/
SRPO.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
import copy
import torch
import torch.nn as nn
from model import *
class SRPO(nn.Module):
def __init__(self, input_dim, output_dim, marginal_prob_std, args=None):
super().__init__()
self.diffusion_behavior = ScoreNet_IDQL(input_dim, output_dim, marginal_prob_std, embed_dim=64, args=args)
self.diffusion_optimizer = torch.optim.AdamW(self.diffusion_behavior.parameters(), lr=3e-4)
self.SRPO_policy = Dirac_Policy(output_dim, input_dim-output_dim, layer=args.policy_layer).to("cuda")
self.SRPO_policy_optimizer = torch.optim.Adam(self.SRPO_policy.parameters(), lr=3e-4)
self.SRPO_policy_lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(self.SRPO_policy_optimizer, T_max=args.n_policy_epochs * 10000, eta_min=0.)
self.marginal_prob_std = marginal_prob_std
self.args = args
self.output_dim = output_dim
self.step = 0
self.q = []
self.q.append(IQL_Critic(adim=output_dim, sdim=input_dim-output_dim, args=args))
def update_SRPO_policy(self, data):
s = data['s']
self.diffusion_behavior.eval()
a = self.SRPO_policy(s)
t = torch.rand(a.shape[0], device=s.device) * 0.96 + 0.02
alpha_t, std = self.marginal_prob_std(t)
z = torch.randn_like(a)
perturbed_a = a * alpha_t[..., None] + z * std[..., None]
with torch.no_grad():
episilon = self.diffusion_behavior(perturbed_a, t, s).detach()
if "noise" in self.args.WT:
episilon = episilon - z
if "VDS" in self.args.WT:
wt = std ** 2
elif "stable" in self.args.WT:
wt = 1.0
elif "score" in self.args.WT:
wt = alpha_t / std
else:
assert False
detach_a = a.detach().requires_grad_(True)
qs = self.q[0].q0_target.both(detach_a , s)
q = (qs[0].squeeze() + qs[1].squeeze()) / 2.0
self.SRPO_policy.q = torch.mean(q)
# TODO be aware that there is a small std gap term here, this seem won't affect final performance though
# guidance = torch.autograd.grad(torch.sum(q), detach_a)[0].detach() * std[..., None]
guidance = torch.autograd.grad(torch.sum(q), detach_a)[0].detach()
if self.args.regq:
guidance_norm = torch.mean(guidance ** 2, dim=-1, keepdim=True).sqrt()
guidance = guidance / guidance_norm
loss = (episilon * a).sum(-1) * wt - (guidance * a).sum(-1) * self.args.beta
loss = loss.mean()
self.SRPO_policy_optimizer.zero_grad(set_to_none=True)
loss.backward()
self.SRPO_policy_optimizer.step()
self.SRPO_policy_lr_scheduler.step()
self.diffusion_behavior.train()
class SRPO_Behavior(nn.Module):
def __init__(self, input_dim, output_dim, marginal_prob_std, args=None):
super().__init__()
self.diffusion_behavior = ScoreNet_IDQL(input_dim, output_dim, marginal_prob_std, embed_dim=64, args=args)
self.diffusion_optimizer = torch.optim.AdamW(self.diffusion_behavior.parameters(), lr=3e-4)
self.marginal_prob_std = marginal_prob_std
self.args = args
self.output_dim = output_dim
self.step = 0
def update_behavior(self, data):
self.step += 1
all_a = data['a']
all_s = data['s']
# Update diffusion behavior
self.diffusion_behavior.train()
random_t = torch.rand(all_a.shape[0], device=all_a.device) * (1. - 1e-3) + 1e-3
z = torch.randn_like(all_a)
alpha_t, std = self.marginal_prob_std(random_t)
perturbed_x = all_a * alpha_t[:, None] + z * std[:, None]
episilon = self.diffusion_behavior(perturbed_x, random_t, all_s)
loss = torch.mean(torch.sum((episilon - z)**2, dim=(1,)))
self.loss =loss
self.diffusion_optimizer.zero_grad()
loss.backward()
self.diffusion_optimizer.step()
class SRPO_IQL(nn.Module):
def __init__(self, input_dim, output_dim, args=None):
super().__init__()
self.deter_policy = Dirac_Policy(output_dim, input_dim-output_dim).to("cuda")
self.deter_policy_optimizer = torch.optim.Adam(self.deter_policy.parameters(), lr=3e-4)
self.deter_policy_lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(self.deter_policy_optimizer, T_max=1500000, eta_min=0.)
self.args = args
self.output_dim = output_dim
self.step = 0
self.q = []
self.q.append(IQL_Critic(adim=output_dim, sdim=input_dim-output_dim, args=args))
def update_iql(self, data):
a = data['a']
s = data['s']
self.q[0].update_q0(data)
# evaluate iql policy part, can be deleted
with torch.no_grad():
target_q = self.q[0].q0_target(a, s).detach()
v = self.q[0].vf(s).detach()
adv = target_q - v
temp = 10.0 if "maze" in self.args.env else 3.0
exp_adv = torch.exp(temp * adv.detach()).clamp(max=100.0)
policy_out = self.deter_policy(s)
bc_losses = torch.sum((policy_out - a)**2, dim=1)
policy_loss = torch.mean(exp_adv.squeeze() * bc_losses)
self.deter_policy_optimizer.zero_grad(set_to_none=True)
policy_loss.backward()
self.deter_policy_optimizer.step()
self.deter_policy_lr_scheduler.step()
self.policy_loss = policy_loss
def update_target(new, target, tau):
# Update the frozen target models
for param, target_param in zip(new.parameters(), target.parameters()):
target_param.data.copy_(tau * param.data + (1 - tau) * target_param.data)
def asymmetric_l2_loss(u, tau):
return torch.mean(torch.abs(tau - (u < 0).float()) * u**2)
class IQL_Critic(nn.Module):
def __init__(self, adim, sdim, args) -> None:
super().__init__()
self.q0 = TwinQ(adim, sdim, layers=args.q_layer).to(args.device)
print(args.q_layer)
self.q0_target = copy.deepcopy(self.q0).to(args.device)
self.vf = ValueFunction(sdim).to("cuda")
self.q_optimizer = torch.optim.Adam(self.q0.parameters(), lr=3e-4)
self.v_optimizer = torch.optim.Adam(self.vf.parameters(), lr=3e-4)
self.discount = 0.99
self.args = args
self.tau = 0.9 if "maze" in args.env else 0.7
print(self.tau)
def update_q0(self, data):
s = data["s"]
a = data["a"]
r = data["r"]
s_ = data["s_"]
d = data["d"]
with torch.no_grad():
target_q = self.q0_target(a, s).detach()
next_v = self.vf(s_).detach()
# Update value function
v = self.vf(s)
adv = target_q - v
v_loss = asymmetric_l2_loss(adv, self.tau)
self.v_optimizer.zero_grad(set_to_none=True)
v_loss.backward()
self.v_optimizer.step()
# Update Q function
targets = r + (1. - d.float()) * self.discount * next_v.detach()
qs = self.q0.both(a, s)
self.v = v.mean()
q_loss = sum(torch.nn.functional.mse_loss(q, targets) for q in qs) / len(qs)
self.q_optimizer.zero_grad(set_to_none=True)
q_loss.backward()
self.q_optimizer.step()
self.v_loss = v_loss
self.q_loss = q_loss
self.q = target_q.mean()
self.v = next_v.mean()
# Update target
update_target(self.q0, self.q0_target, 0.005)