-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
368 lines (312 loc) · 14.8 KB
/
model.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
class GaussianFourierProjection(nn.Module):
"""Gaussian random features for encoding time steps."""
def __init__(self, embed_dim, scale=30.):
super().__init__()
# Randomly sample weights during initialization. These weights are fixed
# during optimization and are not trainable.
self.W = nn.Parameter(torch.randn(embed_dim // 2) * scale, requires_grad=False)
def forward(self, x):
x_proj = x[..., None] * self.W[None, :] * 2 * np.pi
return torch.cat([torch.sin(x_proj), torch.cos(x_proj)], dim=-1)
def mlp(dims, activation=nn.ReLU, output_activation=None):
n_dims = len(dims)
assert n_dims >= 2, 'MLP requires at least two dims (input and output)'
layers = []
for i in range(n_dims - 2):
layers.append(nn.Linear(dims[i], dims[i+1]))
layers.append(activation())
layers.append(nn.Linear(dims[-2], dims[-1]))
if output_activation is not None:
layers.append(output_activation())
net = nn.Sequential(*layers)
net.to(dtype=torch.float32)
return net
class TwinQ(nn.Module):
def __init__(self, action_dim, state_dim, layers=2):
super().__init__()
dims = [state_dim + action_dim] +[256]*layers +[1]
# dims = [state_dim + action_dim, 256, 256, 1] # TODO
self.q1 = mlp(dims)
self.q2 = mlp(dims)
def both(self, action, condition=None):
as_ = torch.cat([action, condition], -1) if condition is not None else action
return self.q1(as_), self.q2(as_)
def forward(self, action, condition=None):
return torch.min(*self.both(action, condition))
class ValueFunction(nn.Module):
def __init__(self, state_dim):
super().__init__()
dims = [state_dim, 256, 256, 1]
self.v = mlp(dims)
def forward(self, state):
return self.v(state)
class Dirac_Policy(nn.Module):
def __init__(self, action_dim, state_dim, layer=2):
super().__init__()
self.net = mlp([state_dim] + [256]*layer + [action_dim], output_activation=nn.Tanh)
def forward(self, state):
return self.net(state)
def select_actions(self, state):
return self(state).detach().cpu().numpy()
class MLPResNetBlock(nn.Module):
"""MLPResNet block."""
def __init__(self, features, act, dropout_rate=None, use_layer_norm=False):
super(MLPResNetBlock, self).__init__()
self.features = features
self.act = act
self.dropout_rate = dropout_rate
self.use_layer_norm = use_layer_norm
if self.use_layer_norm:
self.layer_norm = nn.LayerNorm(features)
self.fc1 = nn.Linear(features, features * 4)
self.fc2 = nn.Linear(features * 4, features)
self.residual = nn.Linear(features, features)
self.dropout = nn.Dropout(dropout_rate) if dropout_rate is not None and dropout_rate > 0.0 else None
def forward(self, x, training=False):
residual = x
if self.dropout is not None:
x = self.dropout(x)
if self.use_layer_norm:
x = self.layer_norm(x)
x = self.fc1(x)
x = self.act(x)
x = self.fc2(x)
if residual.shape != x.shape:
residual = self.residual(residual)
return residual + x
class MLPResNet(nn.Module):
def __init__(self, num_blocks, input_dim, out_dim, dropout_rate=None, use_layer_norm=False, hidden_dim=256, activations=F.relu):
super(MLPResNet, self).__init__()
self.num_blocks = num_blocks
self.out_dim = out_dim
self.dropout_rate = dropout_rate
self.use_layer_norm = use_layer_norm
self.hidden_dim = hidden_dim
self.activations = activations
# self.fc = nn.Linear(input_dim+128, self.hidden_dim)
self.fc = nn.Linear(256, self.hidden_dim)
self.blocks = nn.ModuleList([MLPResNetBlock(self.hidden_dim, self.activations, self.dropout_rate, self.use_layer_norm)
for _ in range(self.num_blocks)])
self.out_fc = nn.Linear(self.hidden_dim, self.out_dim)
def forward(self, x, training=False):
x = self.fc(x)
for block in self.blocks:
x = block(x, training=training)
x = self.activations(x)
x = self.out_fc(x)
return x
class SiLU(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x * torch.sigmoid(x)
class SimpleMLP(nn.Module):
def __init__(self, num_blocks, input_dim, out_dim, dropout_rate=None, use_layer_norm=False, hidden_dim=256, activations=None):
super(SimpleMLP, self).__init__()
# self.num_blocks = num_blocks
self.out_dim = out_dim
# self.dropout_rate = dropout_rate
# self.use_layer_norm = use_layer_norm
self.hidden_dim = hidden_dim
self.activations = activations
self.fc = nn.Linear(256, self.hidden_dim)
self.block = nn.Sequential(
nn.Linear(self.hidden_dim, 512),
self.activations,
nn.Linear(512, 512),
self.activations,
nn.Linear(512, 512),
self.activations,
nn.Linear(512, 512),
self.activations,
nn.Linear(512, self.hidden_dim),
)
self.out_fc = nn.Linear(self.hidden_dim, self.out_dim)
def forward(self, x, training=False):
x = self.fc(x)
x = self.block(x)
x = self.activations(x)
x = self.out_fc(x)
return x
import dpm_solver_pytorch
class ScoreNet_IDQL(nn.Module):
def __init__(self, input_dim, output_dim, marginal_prob_std, embed_dim=32, args=None):
super().__init__()
self.output_dim = output_dim
self.embed = nn.Sequential(GaussianFourierProjection(embed_dim=32))
self.device=args.device
self.marginal_prob_std = marginal_prob_std
self.args=args
if args.model_type == "MLPResNet":
self.main = MLPResNet(args.actor_blocks, input_dim, output_dim, dropout_rate=args.dropout_rate, use_layer_norm=True, hidden_dim=256, activations=SiLU())
elif args.model_type == "SimpleMLP":
# self.main = SimpleMLP(args.actor_blocks, input_dim, output_dim, dropout_rate=args.dropout_rate, use_layer_norm=True, hidden_dim=256, activations=nn.Mish())
self.main = SimpleMLP(args.actor_blocks, input_dim, output_dim, dropout_rate=args.dropout_rate, use_layer_norm=True, hidden_dim=256, activations=SiLU())
else:
raise NotImplementedError
# self.cond_model = mlp([64, 128, 128], output_activation=None, activation=nn.Mish)
self.cond_model = mlp([32, 32], output_activation=None, activation=SiLU)
self.act = lambda x: x * torch.sigmoid(x)
self.dense1 = nn.Linear(32, 32)
self.dense2 = nn.Linear(output_dim, 128)
self.dense3 = nn.Linear(input_dim-output_dim, 256-128-32)
self.norm_diff =False
self.noise_schedule = dpm_solver_pytorch.NoiseScheduleVP(schedule='linear')
self.dpm_solver = dpm_solver_pytorch.DPM_Solver(self.forward_dmp_wrapper_fn, self.noise_schedule, predict_x0=True)
# The swish activation function
# self.act = lambda x: x * torch.sigmoid(x)
def forward(self, x, t, condition):
if self.norm_diff:
embed = self.act(self.cond_model(self.embed(t)))
# all_ = torch.cat([x, condition, embed], dim=-1)
all_ = torch.cat([self.dense2(x), self.dense1(embed), self.dense3(condition)], dim=-1)
h = self.main(all_)
return h
else:
# Forward definition of the BDM model
with torch.enable_grad():
x.requires_grad_(True)
embed = self.act(self.cond_model(self.embed(t)))
# all_ = torch.cat([x, condition, embed], dim=-1)
all_ = torch.cat([self.dense2(x), self.dense1(embed), self.dense3(condition)], dim=-1)
h = self.main(all_)
energy = torch.sum(h)
self.energy = energy
gradient_score = torch.autograd.grad(energy, x, create_graph=True)[0]
return - gradient_score * self.marginal_prob_std(t)[1][..., None]
# return - gradient_score
def get_energy(self, x, t, condition):
if self.norm_diff:
raise NotImplementedError
else:
embed = self.act(self.cond_model(self.embed(t)))
all_ = torch.cat([self.dense2(x), self.dense1(embed), self.dense3(condition)], dim=-1)
h = self.main(all_)
return torch.sum(h, dim=-1)
def forward_dmp_wrapper_fn(self, x, t):
results = self(x, t, self.condition)
return results
def dpm_wrapper_sample(self, dim, batch_size, **kwargs):
with torch.no_grad():
init_x = torch.randn(batch_size, dim, device=self.device)
return self.dpm_solver.sample(init_x, **kwargs).cpu().numpy()
def select_actions(self, states, diffusion_steps=15):
self.eval()
multiple_input=True
with torch.no_grad():
states = states.to(self.device)
# if states.dim() == 1:
# states = states.unsqueeze(0)
# multiple_input=False
num_states = states.shape[0]
self.condition = states
results = self.dpm_wrapper_sample(self.output_dim, batch_size=states.shape[0], steps=diffusion_steps, order=2)
actions = results.reshape(num_states, self.output_dim).copy() # <bz, A>
self.condition = None
out_actions = actions if multiple_input else actions[0]
self.train()
return out_actions
def sample(self, states, sample_per_state=16, diffusion_steps=15):
self.eval()
num_states = states.shape[0]
with torch.no_grad():
states = torch.FloatTensor(states).to(self.device)
states = torch.repeat_interleave(states, sample_per_state, dim=0)
self.condition = states
results = self.dpm_wrapper_sample(self.output_dim, batch_size=states.shape[0], steps=diffusion_steps, order=2)
actions = results[:, :].reshape(num_states, sample_per_state, self.output_dim).copy()
self.condition = None
self.train()
return actions
class Toy_IDQL(nn.Module):
def __init__(self, input_dim, output_dim, marginal_prob_std, embed_dim=32, args=None):
super().__init__()
self.output_dim = output_dim
self.embed = nn.Sequential(GaussianFourierProjection(embed_dim=32))
self.device=args.device
self.marginal_prob_std = marginal_prob_std
self.args=args
if args.model_type == "MLPResNet":
self.main = MLPResNet(args.actor_blocks, input_dim, output_dim, dropout_rate=args.dropout_rate, use_layer_norm=True, hidden_dim=256, activations=SiLU())
elif args.model_type == "SimpleMLP":
# self.main = SimpleMLP(args.actor_blocks, input_dim, output_dim, dropout_rate=args.dropout_rate, use_layer_norm=True, hidden_dim=256, activations=nn.Mish())
self.main = SimpleMLP(args.actor_blocks, input_dim, output_dim, dropout_rate=args.dropout_rate, use_layer_norm=True, hidden_dim=256, activations=SiLU())
else:
raise NotImplementedError
# self.cond_model = mlp([64, 128, 128], output_activation=None, activation=nn.Mish)
self.cond_model = nn.Linear(32,32)
self.act = lambda x: x * torch.sigmoid(x)
self.dense1 = nn.Linear(32, 32)
self.dense2 = nn.Linear(output_dim, 256 - 32)
self.norm_diff =False
self.noise_schedule = dpm_solver_pytorch.NoiseScheduleVP(schedule='linear')
self.dpm_solver = dpm_solver_pytorch.DPM_Solver(self.forward_dmp_wrapper_fn, self.noise_schedule, predict_x0=True)
# The swish activation function
# self.act = lambda x: x * torch.sigmoid(x)
def forward(self, x, t, condition):
if self.norm_diff:
embed = self.act(self.cond_model(self.embed(t)))
# all_ = torch.cat([x, condition, embed], dim=-1)
all_ = torch.cat([self.dense2(x), self.dense1(embed)], dim=-1)
h = self.main(all_)
return h
else:
with torch.enable_grad():
x.requires_grad_(True)
embed = self.act(self.cond_model(self.embed(t)))
# all_ = torch.cat([x, condition, embed], dim=-1)
all_ = torch.cat([self.dense2(x), self.dense1(embed)], dim=-1)
h = self.main(all_)
energy = torch.sum(h)
self.energy = energy
gradient_score = torch.autograd.grad(energy, x, create_graph=True)[0]
return - gradient_score * self.marginal_prob_std(t)[1][..., None]
# return - gradient_score
def get_energy(self, x, t, condition):
if self.norm_diff:
raise NotImplementedError
else:
embed = self.act(self.cond_model(self.embed(t)))
# all_ = torch.cat([x, condition, embed], dim=-1)
all_ = torch.cat([self.dense2(x), self.dense1(embed)], dim=-1)
h = self.main(all_)
# return torch.sum(h, dim=-1) / self.marginal_prob_std(t)[1]
return torch.sum(h, dim=-1)
def forward_dmp_wrapper_fn(self, x, t):
return self(x, t, self.condition)
def dpm_wrapper_sample(self, dim, batch_size, **kwargs):
with torch.no_grad():
init_x = torch.randn(batch_size, dim, device=self.device)
return self.dpm_solver.sample(init_x, **kwargs).cpu().numpy()
def select_actions(self, states, diffusion_steps=15):
self.eval()
multiple_input=True
with torch.no_grad():
states = states.to(self.device)
# if states.dim() == 1:
# states = states.unsqueeze(0)
# multiple_input=False
num_states = states.shape[0]
self.condition = states
results = self.dpm_wrapper_sample(self.output_dim, batch_size=states.shape[0], steps=diffusion_steps, order=2)
actions = results.reshape(num_states, self.output_dim).copy() # <bz, A>
self.condition = None
out_actions = actions if multiple_input else actions[0]
self.train()
return out_actions
def sample(self, states, sample_per_state=16, diffusion_steps=15):
self.eval()
num_states = states.shape[0]
with torch.no_grad():
states = torch.FloatTensor(states).to(self.device)
states = torch.repeat_interleave(states, sample_per_state, dim=0)
self.condition = states
results = self.dpm_wrapper_sample(self.output_dim, batch_size=states.shape[0], steps=diffusion_steps, order=2)
actions = results[:, :].reshape(num_states, sample_per_state, self.output_dim).copy()
self.condition = None
self.train()
return actions