-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencoder.py
295 lines (207 loc) · 8.1 KB
/
encoder.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import torch
import torch.nn as nn
from torch.nn.functional import linear
def tie_weights(src, trg):
assert type(src) == type(trg)
trg.weight = src.weight
trg.bias = src.bias
# for 84 x 84 inputs
OUT_DIM = {2: 39, 4: 35, 6: 31}
# for 64 x 64 inputs
OUT_DIM_64 = {2: 29, 4: 25, 6: 21}
class PixelEncoder(nn.Module):
"""Convolutional encoder of pixels observations."""
def __init__(self, obs_shape, feature_dim, num_layers=2, num_filters=32,encoder_hidden_size=256 ,output_logits=False,*args):
super().__init__()
assert len(obs_shape) == 3
self.obs_shape = obs_shape
self.feature_dim = feature_dim
self.num_layers = num_layers
self.convs = nn.ModuleList(
[nn.Conv2d(obs_shape[0], num_filters, 3, stride=2)]
)
for i in range(num_layers - 1):
self.convs.append(nn.Conv2d(num_filters, num_filters, 3, stride=1))
out_dim = OUT_DIM_64[num_layers] if obs_shape[-1] == 64 else OUT_DIM[num_layers]
self.fc = nn.Linear(num_filters * out_dim * out_dim, self.feature_dim)
self.ln = nn.LayerNorm(self.feature_dim)
self.outputs = dict()
self.output_logits = output_logits
def reparameterize(self, mu, logstd):
std = torch.exp(logstd)
eps = torch.randn_like(std)
return mu + eps * std
def forward_conv(self, obs):
obs = obs / 255.
self.outputs['obs'] = obs
conv = torch.relu(self.convs[0](obs))
self.outputs['conv1'] = conv
for i in range(1, self.num_layers):
conv = torch.relu(self.convs[i](conv))
self.outputs['conv%s' % (i + 1)] = conv
h = conv.view(conv.size(0), -1)
return h
def forward(self, obs, detach=False):
h = self.forward_conv(obs)
if detach:
h = h.detach()
h_fc = self.fc(h)
self.outputs['fc'] = h_fc
h_norm = self.ln(h_fc)
self.outputs['ln'] = h_norm
if self.output_logits:
out = h_norm
else:
out = torch.tanh(h_norm)
self.outputs['tanh'] = out
return out
def copy_conv_weights_from(self, source):
"""Tie convolutional layers"""
# only tie conv layers
for i in range(self.num_layers):
tie_weights(src=source.convs[i], trg=self.convs[i])
def log(self, L, step, log_freq):
if step % log_freq != 0:
return
for k, v in self.outputs.items():
L.log_histogram('train_encoder/%s_hist' % k, v, step)
if len(v.shape) > 2:
L.log_image('train_encoder/%s_img' % k, v[0], step)
for i in range(self.num_layers):
L.log_param('train_encoder/conv%s' % (i + 1), self.convs[i], step)
L.log_param('train_encoder/fc', self.fc, step)
L.log_param('train_encoder/ln', self.ln, step)
class IdentityEncoder(nn.Module):
def __init__(self, obs_shape, feature_dim, num_layers, num_filters,*args):
super().__init__()
assert len(obs_shape) == 1
self.feature_dim = obs_shape[0]
def forward(self, obs, detach=False):
return obs
def copy_conv_weights_from(self, source):
pass
def log(self, L, step, log_freq):
pass
class MlpEncoder(nn.Module):
def __init__(self, obs_shape, feature_dim, num_layers, num_filters, encoder_hidden_size, output_logits=False,*args):
super().__init__()
assert len(obs_shape) == 1
self.obs_shape = obs_shape
self.feature_dim = feature_dim
self.num_layers = num_layers
self.linears = nn.ModuleList(
[nn.Linear(obs_shape[0], encoder_hidden_size)]
)
for i in range(num_layers - 1):
self.linears.append(nn.Linear(encoder_hidden_size, encoder_hidden_size))
self.fc = nn.Linear(encoder_hidden_size, self.feature_dim)
self.ln = nn.LayerNorm(self.feature_dim)
self.outputs = dict()
self.output_logits = output_logits
def forward_linear(self, obs):
self.outputs['obs'] = obs
linear = torch.relu(self.linears[0](obs))
self.outputs['linear1'] = linear
for i in range(1, self.num_layers):
linear = torch.relu(self.linears[i](linear))
self.outputs['linear%s' % (i + 1)] = linear
h = linear
return h
def forward(self, obs, detach=False):
h = self.forward_linear(obs)
if detach:
h = h.detach()
h_fc = self.fc(h)
self.outputs['fc'] = h_fc
h_norm = self.ln(h_fc)
self.outputs['ln'] = h_norm
if self.output_logits:
out = h_norm
else:
out = torch.tanh(h_norm)
self.outputs['tanh'] = out
return out
def copy_conv_weights_from(self, source):
pass
def log(self, L, step, log_freq):
pass
class OfeEncoder(nn.Module):
def __init__(self, obs_shape, feature_dim, num_layers, num_filters, encoder_hidden_size, output_logits=False,*args):
super().__init__()
assert len(obs_shape) == 1
self.obs_shape = obs_shape
self.feature_dim = num_layers * encoder_hidden_size + obs_shape[0]
self.num_layers = num_layers
self.linears = nn.ModuleList(
[nn.Linear(obs_shape[0], encoder_hidden_size)]
)
for i in range(num_layers - 1):
self.linears.append(nn.Linear( ( obs_shape[0] + (i+1) * encoder_hidden_size ), encoder_hidden_size)) # ofenet structure
self.fc = nn.Linear(self.feature_dim, self.feature_dim)
self.ln = nn.LayerNorm(self.feature_dim)
self.outputs = dict()
self.output_logits = output_logits
def forward_linear(self, obs):
self.outputs['obs'] = obs
linear = torch.cat( [obs, torch.relu(self.linears[0](obs))], axis =1)
self.outputs['linear1'] = linear
#import ipdb; ipdb.set_trace()
for i in range(1, self.num_layers):
linear = torch.cat( [linear, torch.relu(self.linears[i](linear))], axis=1)
self.outputs['linear%s' % (i + 1)] = linear
h = linear
return h
def forward(self, obs, detach=False):
h = self.forward_linear(obs)
if detach:
h = h.detach()
h_fc = self.fc(h)
self.outputs['fc'] = h_fc
h_norm = self.ln(h_fc)
self.outputs['ln'] = h_norm
if self.output_logits:
out = h_norm
else:
out = torch.tanh(h_norm)
self.outputs['tanh'] = out
return out
def copy_conv_weights_from(self, source):
pass
def log(self, L, step, log_freq):
pass
class ActionOrRewardEncoder(nn.Module):
def __init__(self, input_dim, feature_dim, num_layers, *args):
super().__init__()
assert type(input_dim) == int
assert num_layers == 1
self.feature_dim = feature_dim
self.forward_linear_layers = nn.Sequential(nn.Linear(input_dim,feature_dim))
self.fc = nn.Linear(self.feature_dim, self.feature_dim)
self.ln = nn.LayerNorm(self.feature_dim)
self.outputs = dict()
def forward(self, obs, detach=False):
h = self.forward_linear_layers(obs)
self.outputs['obs'] = obs
self.outputs['linear'] = h
if detach:
h = h.detach()
h_fc = self.fc(h)
self.outputs['fc'] = h_fc
h_norm = self.ln(h_fc)
self.outputs['ln'] = h_norm
out = torch.tanh(h_norm)
return out
def copy_linear_weights_from(self, source):
pass
def log(self, L, step, log_freq):
pass
_AVAILABLE_ENCODERS = {'pixel': PixelEncoder, 'identity': IdentityEncoder, 'mlp': MlpEncoder, 'ofe': OfeEncoder}
def make_encoder(
encoder_type, obs_shape, feature_dim, num_layers, num_filters, encoder_hidden_size, output_logits=False
):
assert encoder_type in _AVAILABLE_ENCODERS
return _AVAILABLE_ENCODERS[encoder_type](
obs_shape, feature_dim, num_layers, num_filters, encoder_hidden_size, output_logits
)