-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlaai.py
134 lines (100 loc) · 3.94 KB
/
vlaai.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class Extractor(nn.Module):
def __init__(
self,
filters=(256, 256, 256, 128, 128),
kernels=(64,) * 5,
dilation_rate=1,
input_channels=64,
time_dimension=64*5,
normalization_fn='layer_norm',
activation_fn='leaky_relu',
):
super(Extractor, self).__init__()
self.eeg = nn.Conv1d(input_channels, input_channels, kernel_size=1) # Identity mapping for eeg
if len(filters) != len(kernels):
raise ValueError("'filters' and 'kernels' must have the same length")
layers = []
for filter_, kernel in zip(filters, kernels):
dilation = dilation_rate
layers.append(nn.Conv1d(in_channels= input_channels,out_channels= filter_, kernel_size=kernel, padding='same', dilation=dilation))
if normalization_fn == 'layer_norm':
layers.append(nn.LayerNorm([filter_, time_dimension]))
if activation_fn == 'leaky_relu':
layers.append(nn.LeakyReLU())
# layers.append(nn.ConstantPad1d( padding=padding, value=0 ))
input_channels = filter_
self.conv_layers = nn.Sequential(*layers)
def forward(self, x):
x = self.eeg(x)
x = self.conv_layers(x)
return x
class OutputContext(nn.Module):
def __init__(
self,
filter_=64,
kernel=64,
input_channels=64,
time_dimension=64 * 5,
normalization_fn='layer_norm',
activation_fn='leaky_relu',
):
super(OutputContext, self).__init__()
self.conv1d = nn.Conv1d(input_channels, filter_, kernel_size=kernel, padding='same')
if normalization_fn == 'layer_norm':
self.normalization_fn = nn.LayerNorm([filter_, time_dimension])
if activation_fn == 'leaky_relu':
self.activation_fn = nn.LeakyReLU()
def forward(self, x):
# x = F.pad(x, (self.conv1d.padding[0], 0))
x = self.conv1d(x)
x = self.normalization_fn(x)
x = self.activation_fn(x)
return x
class VLAAI(nn.Module):
def __init__(
self,
nb_blocks=4,
extractor_model=None,
output_context_model=None,
use_skip=True,
input_channels=64,
output_dim=64,
):
super(VLAAI, self).__init__()
if extractor_model is None:
extractor_model = Extractor()
if output_context_model is None:
output_context_model = OutputContext()
linear_recombination = nn.Conv1d(in_channels=128, out_channels=input_channels, kernel_size=1,
padding='same') # recombination of 128 features to 64
self.eeg = nn.Conv1d(input_channels, input_channels, kernel_size=1) # Identity mapping for eeg
if use_skip:
self.use_skip = True # = nn.Parameter(torch.zeros(1, input_channels,1), requires_grad=False)
else:
self.use_skip = False
self.sequentialConvStack= nn.Sequential(extractor_model, linear_recombination, output_context_model)
self.output_dim = output_dim
self.nb_blocks = nb_blocks
self.final_linear = nn.Conv1d(input_channels, output_dim, kernel_size=1, padding='same')
def get_output_dim(self, input_window_size):
return input_window_size * self.output_dim
def forward(self, x):
# change dimensions of x
x = x.transpose(1, 2)
if self.use_skip:
eeg = x
else:
# get shape of x
eeg = nn.Parameter(torch.zeros(1, x.shape[1],1), requires_grad=False)
eeg.to(x.device)
x = self.eeg(x)
for idx in range(self.nb_blocks):
if idx == 0 or idx == self.nb_blocks - 1:
x = self.sequentialConvStack(x)
else:
x = self.sequentialConvStack(x+eeg )
x = self.final_linear(x)
return x