-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnd_modules.py
239 lines (190 loc) · 7.49 KB
/
rnd_modules.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
# check rnd_architecture.PNG in `paper` folder for visualization.
from typing import Tuple, Dict
import torch
from torch import nn
try:
from rnd_utils import RunningMeanStd
except ModuleNotFoundError:
from sac_rnd.rnd_utils import RunningMeanStd
# from torch.nn import functional as F
class PredictorNetwork(nn.Module):
def __init__(self,
state_dim: int,
action_dim: int,
embedding_dim: int,
hidden_dim: int = 256,
num_hidden_layers: int = 4) -> None:
super().__init__()
self.embedding_dim = embedding_dim
self.state_dim = state_dim
self.action_dim = action_dim
self.hidden_dim = hidden_dim
self.num_hidden_layers = num_hidden_layers
self.bilinear = nn.Bilinear(state_dim, action_dim, hidden_dim)
layers = [nn.ReLU()]
for _ in range(num_hidden_layers - 2):
layers.append(nn.Linear(hidden_dim, hidden_dim))
layers.append(nn.ReLU())
layers.append(nn.Linear(hidden_dim, embedding_dim))
self.layers = nn.Sequential(*layers)
def forward(self,
states: torch.Tensor,
actions: torch.Tensor) -> torch.Tensor:
z = self.layers(self.bilinear(states, actions))
return z
class GatingModule(nn.Module):
def __init__(self,
state_dim: int,
action_dim: int,
embedding_dim: int) -> None:
super().__init__()
self.state_dim = state_dim
self.action_dim = action_dim
self.embedding_dim = embedding_dim
self.tanh_module = nn.Sequential(
nn.Linear(action_dim, embedding_dim),
nn.Tanh()
)
self.sigmoid_module = nn.Sequential(
nn.Linear(state_dim, embedding_dim),
nn.Sigmoid()
)
def forward(self,
states: torch.Tensor,
actions: torch.Tensor) -> torch.Tensor:
out = self.tanh_module(actions) * self.sigmoid_module(states)
return out
class FiLM(nn.Module):
'''
Feature-wise Linear Modulation
'''
def __init__(self,
in_features: int,
out_features: int) -> None:
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.linear = nn.Linear(in_features, 2 * out_features)
def forward(self,
states: torch.Tensor,
h: torch.Tensor) -> torch.Tensor:
gamma, beta = torch.split(self.linear(states), self.out_features, dim=-1)
return gamma * h + beta
class PriorNetwork(nn.Module):
def __init__(self,
state_dim: int,
action_dim: int,
embedding_dim: int,
hidden_dim: int = 256,
num_hidden_layers: int = 4) -> None:
super().__init__()
self.state_dim = state_dim
self.action_dim = action_dim
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.num_hidden_layers = num_hidden_layers
base_network = [
nn.Linear(action_dim, hidden_dim),
nn.ReLU(),
]
for _ in range(num_hidden_layers - 3):
base_network.append(nn.Linear(hidden_dim, hidden_dim))
base_network.append(nn.ReLU())
base_network.append(nn.Linear(hidden_dim, hidden_dim))
self.base_network = nn.Sequential(*base_network)
self.film = FiLM(state_dim, hidden_dim)
self.head = nn.Sequential(
nn.ReLU(),
nn.Linear(hidden_dim, embedding_dim)
)
def forward(self,
states: torch.Tensor,
actions: torch.Tensor) -> torch.Tensor:
h = self.base_network(actions)
h = self.film(states, h)
z = self.head(h)
return z
class RND(nn.Module):
def __init__(self,
state_dim: int,
action_dim: int,
embedding_dim: int,
state_mean: torch.Tensor,
state_std: torch.Tensor,
action_mean: torch.Tensor,
action_std: torch.Tensor,
max_action: float = 1.0,
hidden_dim: int = 256,
num_hidden_layers: int = 4) -> None:
super().__init__()
self.state_mean, self.state_std = state_mean, state_std
self.action_mean, self.action_std = action_mean, action_std
self.loss_fn = nn.MSELoss(reduction="none")
self.rms = RunningMeanStd()
self.max_action = max_action
self.predictor = PredictorNetwork(state_dim,
action_dim,
embedding_dim,
hidden_dim,
num_hidden_layers)
self.predictor.train()
self.prior = PriorNetwork(state_dim,
action_dim,
embedding_dim,
hidden_dim,
num_hidden_layers)
self.disable_prior_grads()
self.prior.eval()
def disable_prior_grads(self):
for p in self.prior.parameters():
p.requires_grad = False
def normalize(self,
state: torch.Tensor,
action: torch.Tensor,
eps: float = 1e-8) -> Tuple[torch.Tensor, torch.Tensor]:
state = (state - self.state_mean) / (self.state_std + eps)
action = (action - self.action_mean) / (self.action_std + eps)
return state, action
def forward(self,
states: torch.Tensor,
actions: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
self.prior.eval()
states, actions = self.normalize(states, actions)
predictor_out = self.predictor(states, actions)
prior_out = self.prior(states, actions)
return predictor_out, prior_out
def loss(self,
states: torch.Tensor,
actions: torch.Tensor) -> torch.Tensor:
'''
outputs unreduced vector with shape as [batch_size, embedding_dim]
'''
predictor_out, prior_out = self(states, actions)
loss = self.loss_fn(predictor_out, prior_out)
return loss
def rnd_bonus(self,
state: torch.Tensor,
action: torch.Tensor) -> torch.Tensor:
bonus = self.loss(state, action).sum(dim=1) / self.rms.std
return bonus
def update_rnd(self,
states: torch.Tensor,
actions: torch.Tensor) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]:
raw_loss = self.loss(states, actions).sum(dim=1)
loss = raw_loss.mean(dim=0)
self.rms.update(raw_loss)
# made for logging
random_actions = torch.rand_like(actions)
random_actions = 2 * self.max_action * random_actions - self.max_action
rnd_random = self.rnd_bonus(states, random_actions).mean()
update_info = {
"rnd/loss": loss.item(),
"rnd/running_std": self.rms.std.item(),
"rnd/data": loss / self.rms.std.item(),
"rnd/random": rnd_random.item()
}
return loss, update_info
if __name__ == "__main__":
rnd = RND(17, 6, 32, None, None, None, None)
for p in rnd.parameters():
print(p.requires_grad)