-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
96 lines (75 loc) · 2.74 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
import torch
import torch.nn as nn
import torch.nn.functional as F
# Linear
class FCN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(FCN, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, output_size)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.sigmoid(x)
return x
class CNN(nn.Module):
def __init__(self, input_size, output_size):
super(CNN, self).__init__()
self.conv1 = nn.Conv1d(in_channels=1, out_channels=32, kernel_size=3)
self.pool = nn.MaxPool1d(kernel_size=2)
self.fc1 = nn.Linear(32 * 32, output_size)
def forward(self, x):
x = x.unsqueeze(1)
x = F.relu(self.conv1(x))
x = self.pool(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
return torch.sigmoid(x)
class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(LSTM, self).__init__()
self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size, batch_first=True)
self.fc1 = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = x.unsqueeze(1)
out, _ = self.lstm(x)
out = self.fc1(out[:, -1, :])
return torch.sigmoid(out)
# Multiclass
class FCNMultiClass(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(FCNMultiClass, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
class CNNMultiClass(nn.Module):
def __init__(self, input_size, num_classes):
super(CNNMultiClass, self).__init__()
self.conv1 = nn.Conv1d(in_channels=1, out_channels=32, kernel_size=3)
self.pool = nn.MaxPool1d(kernel_size=2)
self.fc1 = nn.Linear(32 * 32, num_classes)
def forward(self, x):
x = x.unsqueeze(1)
x = F.relu(self.conv1(x))
x = self.pool(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
return x
class LSTMMultiClass(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(LSTMMultiClass, self).__init__()
self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size, batch_first=True)
self.fc1 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
x = x.unsqueeze(1)
out, _ = self.lstm(x)
out = self.fc1(out[:, -1, :])
return out