-
Notifications
You must be signed in to change notification settings - Fork 113
/
models.py
executable file
·295 lines (225 loc) · 9.28 KB
/
models.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Categorical
class AnomalyDetector(nn.Module):
"""
Anomaly detector used for debugging. Basically an auto encoder.
P.S. You need to use different weights for different embeddings.
"""
def __init__(self):
super(AnomalyDetector, self).__init__()
self.ae = nn.Sequential(
nn.Linear(128, 64),
nn.ReLU(),
nn.BatchNorm1d(64),
nn.Linear(64, 32),
nn.ReLU(),
nn.BatchNorm1d(32),
nn.Linear(32, 64),
nn.ReLU(),
nn.BatchNorm1d(64),
nn.Linear(64, 128),
nn.ReLU(),
)
def forward(self, x):
""""""
return self.ae(x)
def rec_error(self, x):
error = torch.sum((x - self.ae(x)) ** 2, 1)
if x.size(1) != 1:
return error.detach()
return error.item()
class Actor(nn.Module):
"""
Vanilla actor. Takes state as an argument, returns action.
"""
def __init__(self, input_dim, action_dim, hidden_size, init_w=2e-1):
super(Actor, self).__init__()
self.drop_layer = nn.Dropout(p=0.5)
self.linear1 = nn.Linear(input_dim, hidden_size)
self.linear2 = nn.Linear(hidden_size, hidden_size)
self.linear3 = nn.Linear(hidden_size, action_dim)
self.linear3.weight.data.uniform_(-init_w, init_w)
self.linear3.bias.data.uniform_(-init_w, init_w)
def forward(self, state, tanh=False):
"""
:param action: nothing should be provided here.
:param state: state
:param tanh: whether to use tahn as action activation
:return: action
"""
action = F.relu(self.linear1(state))
action = self.drop_layer(action)
action = F.relu(self.linear2(action))
action = self.drop_layer(action)
action = self.linear3(action)
if tanh:
action = F.tanh(action)
return action
class DiscreteActor(nn.Module):
def __init__(self, input_dim, action_dim, hidden_size, init_w=0):
super(DiscreteActor, self).__init__()
self.linear1 = nn.Linear(input_dim, hidden_size)
self.linear2 = nn.Linear(hidden_size, action_dim)
self.saved_log_probs = []
self.rewards = []
self.correction = []
self.lambda_k = []
# What's action source? See this issue: https://github.com/awarebayes/RecNN/issues/7
# by default {pi: pi, beta: beta}
# you can change it to be like {pi: beta, beta: beta} as miracle24 suggested
self.action_source = {"pi": "pi", "beta": "beta"}
self.select_action = self._select_action
def forward(self, inputs):
x = inputs
x = F.relu(self.linear1(x))
action_scores = self.linear2(x)
return F.softmax(action_scores)
def gc(self):
del self.rewards[:]
del self.saved_log_probs[:]
del self.correction[:]
del self.lambda_k[:]
def _select_action(self, state, **kwargs):
# for reinforce without correction only pi_probs is available.
# the action source is ignored, since there is no beta
pi_probs = self.forward(state)
pi_categorical = Categorical(pi_probs)
pi_action = pi_categorical.sample()
self.saved_log_probs.append(pi_categorical.log_prob(pi_action))
return pi_probs
def pi_beta_sample(self, state, beta, action, **kwargs):
# 1. obtain probabilities
# note: detach is to block gradient
beta_probs = beta(state.detach(), action=action)
pi_probs = self.forward(state)
# 2. probabilities -> categorical distribution.
beta_categorical = Categorical(beta_probs)
pi_categorical = Categorical(pi_probs)
# 3. sample the actions
# See this issue: https://github.com/awarebayes/RecNN/issues/7
# usually it works like:
# pi_action = pi_categorical.sample(); beta_action = beta_categorical.sample();
# but changing the action_source to {pi: beta, beta: beta} can be configured to be:
# pi_action = beta_categorical.sample(); beta_action = beta_categorical.sample();
available_actions = {
"pi": pi_categorical.sample(),
"beta": beta_categorical.sample(),
}
pi_action = available_actions[self.action_source["pi"]]
beta_action = available_actions[self.action_source["beta"]]
# 4. calculate stuff we need
pi_log_prob = pi_categorical.log_prob(pi_action)
beta_log_prob = beta_categorical.log_prob(beta_action)
return pi_log_prob, beta_log_prob, pi_probs
def _select_action_with_correction(
self, state, beta, action, writer, step, **kwargs
):
pi_log_prob, beta_log_prob, pi_probs = self.pi_beta_sample(state, beta, action)
# calculate correction
corr = torch.exp(pi_log_prob) / torch.exp(beta_log_prob)
writer.add_histogram("correction", corr, step)
writer.add_histogram("pi_log_prob", pi_log_prob, step)
writer.add_histogram("beta_log_prob", beta_log_prob, step)
self.correction.append(corr)
self.saved_log_probs.append(pi_log_prob)
return pi_probs
def _select_action_with_TopK_correction(
self, state, beta, action, K, writer, step, **kwargs
):
pi_log_prob, beta_log_prob, pi_probs = self.pi_beta_sample(state, beta, action)
# calculate correction
corr = torch.exp(pi_log_prob) / torch.exp(beta_log_prob)
# calculate top K correction
l_k = K * (1 - torch.exp(pi_log_prob)) ** (K - 1)
writer.add_histogram("correction", corr, step)
writer.add_histogram("l_k", l_k, step)
writer.add_histogram("pi_log_prob", pi_log_prob, step)
writer.add_histogram("beta_log_prob", beta_log_prob, step)
self.correction.append(corr)
self.lambda_k.append(l_k)
self.saved_log_probs.append(pi_log_prob)
return pi_probs
class Critic(nn.Module):
"""
Vanilla critic. Takes state and action as an argument, returns value.
"""
def __init__(self, input_dim, action_dim, hidden_size, init_w=3e-5):
super(Critic, self).__init__()
self.drop_layer = nn.Dropout(p=0.5)
self.linear1 = nn.Linear(input_dim + action_dim, hidden_size)
self.linear2 = nn.Linear(hidden_size, hidden_size)
self.linear3 = nn.Linear(hidden_size, 1)
self.linear3.weight.data.uniform_(-init_w, init_w)
self.linear3.bias.data.uniform_(-init_w, init_w)
def forward(self, state, action):
""""""
value = torch.cat([state, action], 1)
value = F.relu(self.linear1(value))
value = self.drop_layer(value)
value = F.relu(self.linear2(value))
value = self.drop_layer(value)
value = self.linear3(value)
return value
class bcqPerturbator(nn.Module):
"""
Batch constrained perturbative actor. Takes action as an argument, adjusts it.
"""
def __init__(self, num_inputs, num_actions, hidden_size, init_w=3e-1):
super(bcqPerturbator, self).__init__()
self.drop_layer = nn.Dropout(p=0.5)
self.linear1 = nn.Linear(num_inputs + num_actions, hidden_size)
self.linear2 = nn.Linear(hidden_size, hidden_size)
self.linear3 = nn.Linear(hidden_size, num_actions)
self.linear3.weight.data.uniform_(-init_w, init_w)
self.linear3.bias.data.uniform_(-init_w, init_w)
def forward(self, state, action):
""""""
a = torch.cat([state, action], 1)
a = F.relu(self.linear1(a))
a = self.drop_layer(a)
a = F.relu(self.linear2(a))
a = self.drop_layer(a)
a = self.linear3(a)
return a + action
class bcqGenerator(nn.Module):
"""
Batch constrained generator. Basically VAE
"""
def __init__(self, state_dim, action_dim, latent_dim):
super(bcqGenerator, self).__init__()
# encoder
self.e1 = nn.Linear(state_dim + action_dim, 750)
self.e2 = nn.Linear(750, 750)
self.mean = nn.Linear(750, latent_dim)
self.log_std = nn.Linear(750, latent_dim)
# decoder
self.d1 = nn.Linear(state_dim + latent_dim, 750)
self.d2 = nn.Linear(750, 750)
self.d3 = nn.Linear(750, action_dim)
self.latent_dim = latent_dim
self.normal = torch.distributions.Normal(0, 1)
def forward(self, state, action):
""""""
# z is encoded state + action
z = F.relu(self.e1(torch.cat([state, action], 1)))
z = F.relu(self.e2(z))
mean = self.mean(z)
# Clamped for numerical stability
log_std = self.log_std(z).clamp(-4, 15)
std = torch.exp(log_std)
z = mean + std * self.normal.sample(std.size()).to(
next(self.parameters()).device
)
# u is decoded action
u = self.decode(state, z)
return u, mean, std
def decode(self, state, z=None):
# When sampling from the VAE, the latent vector is clipped to [-0.5, 0.5]
if z is None:
z = self.normal.sample([state.size(0), self.latent_dim])
z = z.clamp(-0.5, 0.5).to(next(self.parameters()).device)
a = F.relu(self.d1(torch.cat([state, z], 1)))
a = F.relu(self.d2(a))
return self.d3(a)