-
Notifications
You must be signed in to change notification settings - Fork 22
/
model.py
178 lines (151 loc) · 5.28 KB
/
model.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
'''The model architecture used was first created by the user polomarco for a Kaggle competition:
https://www.kaggle.com/polomarco/ecg-classification-cnn-lstm-attention-mechanism
However, this example has been altered to fit the FLUTE architecture'''
import torch
from torch import nn
from torch.nn import functional as F
from core.model import BaseModel
# ReLu alternative
class Swish(nn.Module):
def forward(self, x):
return x * torch.sigmoid(x)
class ConvNormPool(nn.Module):
"""Conv Skip-connection module"""
def __init__(
self,
input_size,
hidden_size,
kernel_size,
norm_type='bachnorm'
):
super().__init__()
self.kernel_size = kernel_size
self.conv_1 = nn.Conv1d(
in_channels=input_size,
out_channels=hidden_size,
kernel_size=kernel_size
)
self.conv_2 = nn.Conv1d(
in_channels=hidden_size,
out_channels=hidden_size,
kernel_size=kernel_size
)
self.conv_3 = nn.Conv1d(
in_channels=hidden_size,
out_channels=hidden_size,
kernel_size=kernel_size
)
self.swish_1 = Swish()
self.swish_2 = Swish()
self.swish_3 = Swish()
if norm_type == 'group':
self.normalization_1 = nn.GroupNorm(
num_groups=8,
num_channels=hidden_size
)
self.normalization_2 = nn.GroupNorm(
num_groups=8,
num_channels=hidden_size
)
self.normalization_3 = nn.GroupNorm(
num_groups=8,
num_channels=hidden_size
)
else:
self.normalization_1 = nn.BatchNorm1d(num_features=hidden_size)
self.normalization_2 = nn.BatchNorm1d(num_features=hidden_size)
self.normalization_3 = nn.BatchNorm1d(num_features=hidden_size)
self.pool = nn.MaxPool1d(kernel_size=2)
def forward(self, input):
conv1 = self.conv_1(input)
x = self.normalization_1(conv1)
x = self.swish_1(x)
x = F.pad(x, pad=(self.kernel_size - 1, 0))
x = self.conv_2(x)
x = self.normalization_2(x)
x = self.swish_2(x)
x = F.pad(x, pad=(self.kernel_size - 1, 0))
conv3 = self.conv_3(x)
x = self.normalization_3(conv1+conv3)
x = self.swish_3(x)
x = F.pad(x, pad=(self.kernel_size - 1, 0))
x = self.pool(x)
return x
class RNN(nn.Module):
"""RNN module(cell type lstm or gru)"""
def __init__(
self,
input_size,
hid_size,
num_rnn_layers=1,
dropout_p = 0.2,
):
super().__init__()
self.rnn_layer = nn.LSTM(
input_size=input_size,
hidden_size=hid_size,
num_layers=num_rnn_layers,
dropout=dropout_p if num_rnn_layers>1 else 0,
bidirectional=False,
batch_first=True,
)
def forward(self, input):
outputs, hidden_states = self.rnn_layer(input)
return outputs, hidden_states
class Net(nn.Module):
def __init__(
self,
input_size=1,
hid_size=64,
n_classes=5,
kernel_size=5,
):
super().__init__()
self.rnn_layer = RNN(
input_size=46,
hid_size=hid_size,
)
self.conv1 = ConvNormPool(
input_size=input_size,
hidden_size=hid_size,
kernel_size=kernel_size,
)
self.conv2 = ConvNormPool(
input_size=hid_size,
hidden_size=hid_size,
kernel_size=kernel_size,
)
self.avgpool = nn.AdaptiveMaxPool1d((1))
self.attn = nn.Linear(hid_size, hid_size, bias=False)
self.fc = nn.Linear(in_features=hid_size, out_features=n_classes)
def forward(self, input):
x = self.conv1(input)
x = self.conv2(x)
x_out, hid_states = self.rnn_layer(x)
x = torch.cat([hid_states[0], hid_states[1]], dim=0).transpose(0, 1)
x_attn = torch.tanh(self.attn(x))
x = x_attn.bmm(x_out)
x = x.transpose(2, 1)
x = self.avgpool(x)
x = x.view(-1, x.size(1) * x.size(2))
x = F.softmax(self.fc(x), dim=-1)
return x
class SuperNet(BaseModel):
'''This is the parent of the net with some extra methods'''
def __init__(self, model_config):
super().__init__()
self.net = Net()
def loss(self, input: torch.Tensor):
device = 'cuda' if torch.cuda.is_available() else 'cpu'
features, labels = input['x'].to(device), input['y'].to(device)
output = self.net.forward(features)
return F.cross_entropy(output, labels.long())
def inference(self, input):
device = 'cuda' if torch.cuda.is_available() else 'cpu'
features, labels = input['x'].to(device), input['y'].to(device)
output = self.net.forward(features)
n_samples = features.shape[0]
accuracy = torch.mean((torch.argmax(output, dim=1) == labels).float()).item()
return {'output':output, 'acc': accuracy, 'batch_size': n_samples}