-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSeq2Vec.py
55 lines (41 loc) · 1.64 KB
/
Seq2Vec.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
import torch.nn as nn
import torch
from ConvLSTM import ConvLSTM
class Seq2Vec(nn.Module):
def __init__(self, num_channels, num_kernels, kernel_size, padding,
activation, frame_size, num_layers, out_size):
super(Seq2Vec, self).__init__()
self.sequential = nn.Sequential()
# Add First layer (Different in_channels than the rest)
self.sequential.add_module(
"convlstm1", ConvLSTM(
in_channels=num_channels, out_channels=num_kernels,
kernel_size=kernel_size, padding=padding,
activation=activation, frame_size=frame_size)
)
self.sequential.add_module(
"batchnorm1", nn.BatchNorm3d(num_features=num_kernels)
)
# Add rest of the layers
for l in range(2, num_layers+1):
self.sequential.add_module(
f"convlstm{l}", ConvLSTM(
in_channels=num_kernels, out_channels=num_kernels,
kernel_size=kernel_size, padding=padding,
activation=activation, frame_size=frame_size)
)
self.sequential.add_module(
f"batchnorm{l}", nn.BatchNorm3d(num_features=num_kernels)
)
# Add linear layer to predict output vector
self.fc = nn.Sequential(
nn.Flatten(),
nn.LazyLinear(out_features=out_size),
nn.Sigmoid()
)
def forward(self, X):
# Forward propagation through all the layers
output = self.sequential(X)
# Return only the last output frame
output = self.fc(output[:,:,-1])
return output