-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.py
321 lines (251 loc) · 10.8 KB
/
net.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
import torch
from torch import nn
import numpy as np
import torch.nn.functional as F
from torch.nn import TransformerEncoder, TransformerEncoderLayer
import math
from math import sqrt
import torch
from torch import nn, einsum
import torch.nn.functional as F
import gym
from einops import rearrange, repeat
from einops.layers.torch import Rearrange
from torch.nn.modules.activation import GELU
def conv2d_size_out(size, kernel_size, stride):
return (size - (kernel_size - 1) - 1) // stride + 1
class FeedForward(nn.Module):
def __init__(self, dim, hidden_dim, dropout = 0.):
super().__init__()
self.net = nn.Sequential(
nn.Linear(dim, hidden_dim),
nn.GELU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, dim),
nn.Dropout(dropout)
)
def forward(self, x):
return self.net(x)
class MLP(nn.Module):
def __init__(self, input_dim, layer_dim, depth):
super().__init__()
self.layers = nn.ModuleList([nn.Linear(input_dim, layer_dim), nn.GELU()])
for _ in range(depth - 1):
self.layers.append(nn.Linear(layer_dim, layer_dim))
self.layers.append(nn.GELU())
def forward(self, x):
for layer in self.layers:
x = layer(x)
return x
class Residual(nn.Module):
def __init__(self, fn):
super().__init__()
self.fn = fn
def forward(self, x):
return self.fn(x) + x
class PreNorm(nn.Module):
def __init__(self, dim, fn):
super().__init__()
self.fn = fn
self.norm = nn.LayerNorm(dim)
def forward(self, x, **kwargs):
x = self.norm(x)
return self.fn(x, **kwargs)
class MultiHeadAttention(nn.Module):
def __init__(self, dim, heads = 8, dim_head = 64, dropout = 0.):
super().__init__()
inner_dim = dim_head * heads
self.heads = heads
self.scale = dim_head ** -0.5
self.attend = nn.Softmax(dim = -1)
self.to_qkv = nn.Linear(dim, inner_dim * 3, bias = False)
# in the original paper there are two layers (196, 64 * 2) -> (196, 26)
self.to_out = nn.Sequential(
nn.Linear(inner_dim, dim),
nn.Dropout(dropout)
)
def forward(self, x):
b, n, _, h = *x.shape, self.heads
qkv = self.to_qkv(x).chunk(3, dim = -1)
# b = batch size, h = number of parallel heads, n = input dim (sequence), d = embedding dim (or query size)
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = h), qkv)
# i and j are the same as n, dots have dimension n^2 -> quadratic
dots = einsum('b h i d, b h j d -> b h i j', q, k) * self.scale
attn = self.attend(dots)
out = einsum('b h i j, b h j d -> b h i d', attn, v)
out = rearrange(out, 'b h n d -> b n (h d)')
return self.to_out(out)
def attention_weights(self, x):
b, n, _, h = *x.shape, self.heads
qkv = self.to_qkv(x).chunk(3, dim = -1)
# b = batch size, h = number of parallel heads, n = input dim (sequence), d = embedding dim (or query size)
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = h), qkv)
# i and j are the same as n, dots have dimension n^2 -> quadratic
dots = einsum('b h i d, b h j d -> b h i j', q, k) * self.scale
attn = self.attend(dots)
return attn # b, h, n, n
class Attention(nn.Module):
def __init__(self, dim_in, dim_out, dim_inner, causal = False):
super().__init__()
self.scale = dim_inner ** -0.5
self.causal = causal
self.to_qkv = nn.Linear(dim_in, dim_inner * 3, bias = False)
self.to_out = nn.Linear(dim_inner, dim_out)
def forward(self, x):
device = x.device
q, k, v = self.to_qkv(x).chunk(3, dim = -1)
sim = einsum('b i d, b j d -> b i j', q, k) * self.scale
if self.causal:
mask = torch.ones(sim.shape[-2:], device = device).triu(1).bool()
sim.masked_fill_(mask[None, ...], -torch.finfo(q.dtype).max)
attn = sim.softmax(dim = -1)
out = einsum('b i j, b j d -> b i d', attn, v)
return self.to_out(out)
class Transformer(nn.Module):
def __init__(self, dim, depth, heads, dim_head, mlp_dim, dropout = 0.):
super().__init__()
self.layers = nn.ModuleList([])
for _ in range(depth):
self.layers.append(nn.ModuleList([
PreNorm(dim, MultiHeadAttention(dim, heads = heads, dim_head = dim_head, dropout = dropout)),
PreNorm(dim, FeedForward(dim, mlp_dim, dropout = dropout))
]))
def forward(self, x):
for attn, ff in self.layers:
x = attn(x) + x
x = ff(x) + x
return x
class PositionalEncodings(nn.Module):
pass
class Residual(nn.Module):
def __init__(self, fn):
super().__init__()
self.fn = fn
def forward(self, x, **kwargs):
return self.fn(x, **kwargs) + x
class ResBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel=3, stride=1):
super().__init__()
self.block = nn.Sequential(
nn.Conv2d(in_channels, out_channels, stride=stride, kernel_size=kernel, padding=1),
nn.BatchNorm2d(out_channels),
nn.GELU(),
nn.Conv2d(out_channels, out_channels, stride=stride, kernel_size=kernel, padding=1),
nn.BatchNorm2d(out_channels)
)
def forward(self, x):
out = self.block(x)
out += x
out = F.gelu(out)
return out
class RelationalNet(nn.Module):
"""Relational net used in the paper Relational Deep RL
The class provides two different implementatios: baseline and attention
The Attention net has the following structure:
- 2 x conv (12 and 24 kernels of size 2 and stride 1) + position encoding
- 2 to 4 attention blocks with embedding size 64
- feature wide max pooling (n x n x k -> k)
- MPL with 4 layers
- projection to policy (4 logits) and value function (1 scalar)
The Resnet has the following architecture:
- 2 x conv (12 and 24 kernels of size 2 and stride 1) + position encoding
- 2 to 4 attention blocks with embedding size 64
- 3 to 6 residual blocks, each block with a conv layer with 26 channels and 3x3 kernels
"""
def __init__(self, observation_space, features_dim:int=256, mlp_depth=4, depth_transformer=2, heads=2, baseline=False, num_res_blocks=3, recurrent_transformer=False):
super().__init__()
self.features_dim = features_dim
# convolulotional layers
# padding is used such that the output of the second layer is still 14x14
self.conv = nn.Sequential(
nn.Conv2d(3, 12, kernel_size=(2, 2), stride=1, padding=1),
nn.GELU(),
nn.Conv2d(12, 24, kernel_size=(2, 2), stride=1),
nn.GELU()
)
# with padding (n, n, c) -> (n, n, k)
conv_out_features = observation_space.shape[1]
# positional encodings of size (2, 14, 14) that contain the x and y pos of each tile
# linearly spaces between -1 and 1
x = torch.linspace(-1, 1, steps=conv_out_features)
y = torch.linspace(-1, 1, steps=conv_out_features)
xx, yy = torch.meshgrid(x, y)
xx = xx.flatten()
yy = yy.flatten()
# the stacking dimension 1 means that expends in that dimesion (196, 2)
# the unqueeze just adds the batch dim, necessary when concat later on
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.embeddings = torch.unsqueeze(torch.stack((xx, yy), dim=1), dim = 0).to(device)
# attention layer
self.recurrent_transformer = recurrent_transformer
self.depth_transformer = depth_transformer
self.transformer = None
self.residual_layers = nn.ModuleList([])
mlp_input_dim = 0
if baseline:
for _ in range(num_res_blocks):
self.residual_layers.append(ResBlock(in_channels=24, out_channels=24))
mlp_input_dim = 24
else:
if recurrent_transformer:
self.transformer = Transformer(dim=26, depth=depth_transformer, heads=heads, dim_head=64, mlp_dim=256)
else:
self.transformer = Transformer(dim=26, depth=1, heads=heads, dim_head=64, mlp_dim=256)
self.transformer_project = nn.Linear(conv_out_features**2 * heads, conv_out_features**2)
mlp_input_dim = 26
# input dimensions for mlo
self.mlp = MLP(input_dim=mlp_input_dim, layer_dim=256, depth=mlp_depth)
# logits for policy and value
self.baseline = baseline
# device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def forward(self, x):
# initial feature extractors
x = self.conv(x)
if not self.baseline:
# project to from (n, c, i, j) -> (n, i * j, c)
# add positional encodings
x = rearrange(x, 'b c i j -> b (i j) c')
# repeat along the batch dim
batch_size = x.size()[0]
embeddings = self.embeddings.repeat(batch_size, 1, 1)
x = torch.cat([x, embeddings], dim=2) # stack along the dimension of the filters -> (n, 196, 26)
if self.recurrent_transformer:
for _ in range(self.depth_transformer):
# depth 1 transormer applied multiple time for weight sharing
x = self.transformer(x)
else:
x = self.transformer(x)
x = rearrange(x, 'n i j -> n j i')
else:
for res_layer in self.residual_layers:
x = res_layer(x) # out of the 14^2 features per filter (26) pick one
x = rearrange(x, 'n c i j -> n c (i j)')
# feels kind of an information bottlececk though
x = torch.squeeze(F.max_pool1d(x, kernel_size=x.shape[2]), dim=2)
# x = self.transformer_project(x)
x = self.mlp(x)
return x
if __name__ == '__main__':
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
input = np.zeros((2, 3, 14, 14))
observation_space = type('test', (), {})()
observation_space.shape = (3, 14, 14)
net = RelationalNet(observation_space, baseline=True).to(device)
output = net(torch.from_numpy(input).float().to(device))
print(output.shape)
# pos_enc_ = pos_enc(output)
# print(pos_enc_[0, 0, 0])
# print(pos_enc_[1, 0, 0])
# attend = nn.Softmax(dim = -1)
# # the linear embeddings
# linear_embedding = torch.zeros((2, 3, 6)) # b n (h * d)
# h = 2
# qvk = linear_embedding.chunk(3, dim=-1)
# # b = batch size, h = number of parallel heads, n = input dim (sequence), d = embedding dim (or query size)
# q, v, k = map(lambda t: rearrange(t, 'b n (h d) ->b h n d', h=h), qvk) # split heads explicitly
# # i and j are the same as n, dots have dimension n^2 -> quadratic
# dots = einsum('b h i d, b h j d -> b h i j', q, k)
# out = einsum('b h i j, b h j d -> b h i d', attend(dots), v) # input dim gets contracted
# out = rearrange(out, 'b h n d -> b n (h d)')
# print(out.shape)