-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.py
238 lines (175 loc) · 7.12 KB
/
models.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
#---------------------------------------------------------------------------------------------------#
# File name: models.py #
# Autor: Chrissi2802 #
# Created on: 14.07.2022 #
#---------------------------------------------------------------------------------------------------#
# WISDM - Biometric time series data classification
# Exact description in the functions.
# This file provides the models.
import torch
import torch.nn as nn
class MLP_NET_V1(nn.Module):
"""Class to design a MLP model."""
def __init__(self):
"""Initialisation of the class (constructor)."""
super().__init__()
self.relu = nn.ReLU()
self.dropout = nn.Dropout(0.5)
self.softmax = nn.Softmax(dim = 1)
self.bnin = nn.BatchNorm1d(16)
self.bnbout = nn.BatchNorm1d(32)
self.linin = nn.Linear(3, 16, bias = True)
self.linbout = nn.Linear(16, 32, bias = True)
self.linout = nn.Linear(32, 6, bias = True)
def forward(self, input_data):
"""The layers are stacked to transport the data through the neural network for the forward part."""
# Input:
# input_data; torch.Tensor
# Output:
# x; torch.Tensor
x = self.linin(torch.flatten(input_data, 1))
x = self.bnin(x)
x = self.relu(x)
x = self.dropout(x)
x = self.linbout(x)
x = self.bnbout(x)
x = self.relu(x)
x = self.dropout(x)
x = self.linout(x)
x = self.softmax(x)
return x
class CNN_NET_V1(nn.Module):
"""Class to design a CNN model."""
def __init__(self):
"""Initialisation of the class (constructor)."""
super().__init__()
self.relu = nn.ReLU()
self.dropout = nn.Dropout(0.5)
self.softmax = nn.Softmax(dim = 1)
self.bncnn1 = nn.BatchNorm1d(64)
self.bncnn2 = nn.BatchNorm1d(128)
self.bncnn3 = nn.BatchNorm1d(256)
self.bnbout = nn.BatchNorm1d(64)
self.cnn1 = nn.Conv1d(3, 64, 3, padding = 2)
self.cnn2 = nn.Conv1d(64, 128, 3, padding = 1)
self.cnn3 = nn.Conv1d(128, 256, 3, padding = 1)
self.avgpool = nn.AvgPool1d(3)
self.linbout = nn.Linear(256, 64, bias = True)
self.linout = nn.Linear(64, 6, bias = True)
def forward(self, input_data):
"""The layers are stacked to transport the data through the neural network for the forward part."""
# Input:
# input_data; torch.Tensor
# Output:
# x; torch.Tensor
# Input dimension: batch_size, features
x = input_data.unsqueeze(2) # add one dimension
# Input dimension: batch_size, 3, 1
x = self.cnn1(x)
x = self.bncnn1(x)
x = self.relu(x)
# Input dimension: batch_size, 64, 3
x = self.cnn2(x)
x = self.bncnn2(x)
x = self.relu(x)
# Input dimension: batch_size, 128, 3
x = self.cnn3(x)
x = self.bncnn3(x)
x = self.relu(x)
# Input dimension: batch_size, 256, 3
x = self.avgpool(x)
# Input dimension: batch_size, 256, 1
x = self.linbout(torch.flatten(x, 1))
x = self.bnbout(x)
x = self.relu(x)
x = self.dropout(x)
# Input dimension: batch_size, 64
x = self.linout(x)
x = self.softmax(x)
# Output dimension: batch_size, 6
return x
class CNN_NET_V2(nn.Module):
"""Class to design a CNN model."""
def __init__(self, height, width):
"""Initialisation of the class (constructor)."""
# Input:
# height; integer
# width; integer
super().__init__()
dropout = 0.2
self.net = nn.Sequential(nn.Conv2d(1, 16, kernel_size=2, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(dropout),
nn.Conv2d(16, 32, kernel_size=2, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
nn.Dropout(dropout),
nn.Flatten())
xs = self.net(torch.rand(1, 1, height, width))
self.net2 = nn.Sequential(nn.Linear(xs.shape[1], 128),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(128, 6))
def forward(self, input_data):
"""The layers are stacked to transport the data through the neural network for the forward part."""
# Input:
# input_data; torch.Tensor
# Output:
# x; torch.Tensor
x = self.net(input_data)
x = self.net2(x)
return x
class LSTM_NET(nn.Module):
"""Class to design a LSTM model."""
def __init__(self, input_dim, hidden_dim, time_length):
"""Initialisation of the class (constructor)."""
# Input:
# input_dim, integer
# hidden_dim; integer
# time_length; integer
super().__init__()
self.lstm = nn.LSTM(input_dim, hidden_dim, batch_first = True)
self.net = nn.Sequential(nn.Flatten(),
nn.Dropout(0.2),
nn.Linear(time_length * hidden_dim, 128),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(128, 6))
def forward(self, input_data):
"""The layers are stacked to transport the data through the neural network for the forward part."""
# Input:
# input_data; torch.Tensor
# Output:
# x; torch.Tensor
x, h = self.lstm(input_data)
x = self.net(x)
return x
class GRU_NET(nn.Module):
"""Class to design a GRU model."""
def __init__(self, input_size, hidden_size, num_layers, output_size):
"""Initialisation of the class (constructor)."""
# Input:
# input_size
# sliding_window size; integer
# hidden_size; integer
# num_layers; integer
# output_size; integer
super().__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.output_size = output_size
self.gru = nn.GRU(self.input_size, self.hidden_size, self.num_layers, batch_first = True)
self.net = nn.Sequential(nn.Flatten(),
nn.Linear(self.hidden_size, self.output_size, bias = True))
def forward(self, input_data):
"""The layers are stacked to transport the data through the neural network for the forward part."""
# Input:
# input_data; torch.Tensor
# Output:
# x; torch.Tensor
# h; torch.Tensor
x, h = self.gru(input_data)
x = self.net(x)
return x