forked from HyeJung-Hwang/Time-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
139 lines (115 loc) · 4.42 KB
/
models.py
File metadata and controls
139 lines (115 loc) · 4.42 KB
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
import torch.nn as nn
import torch.nn.functional as F
from keras.models import Sequential
from keras.models import Model, load_model
from keras.layers import Dense, LSTM, Input, BatchNormalization
from keras.callbacks import ModelCheckpoint, EarlyStopping
import tensorflow as tf
# LSTM (tensorflow model)
model = Sequential()
model.add(LSTM(LSTM_NODES, input_shape=x.shape[1:]))
model.add(BatchNormalization())
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy', tf.keras.metrics.AUC()])
hist = model.fit(train_x_valid, train_y_valid, validation_split=0.1, epochs=100, batch_size=BATCH_SIZE, class_weight={0:1, 1:5},
callbacks=[ModelCheckpoint(monitor='val_loss', filepath=weight_path, verbose=1, save_best_only=True),
EarlyStopping(monitor='val_loss', patience=2, verbose=0, mode='auto')])
class GlobalMaxPooling(nn.Module):
def forward(self, x):
kernel_size = x.size()[2:]
return F.max_pool1d(x, kernel_size )
class Flatten(nn.Module):
def forward(self, x):
batch_size = x.shape[0]
return x.contiguous().view(batch_size, -1)
class CNN(nn.Module):
def __init__(self, n_classes = 2):
super(CNN, self).__init__()
self.conv_1 = nn.Sequential(
nn.Conv1d( in_channels = 1, out_channels= 64, kernel_size= 2, stride= 1),
nn.BatchNorm1d(num_features = 64 ),
nn.ReLU(),
nn.MaxPool1d(kernel_size= 2, stride= 1),
)
self.conv_2 = nn.Sequential(
nn.Conv1d( in_channels = 64, out_channels= 64, kernel_size= 2, stride= 1),
nn.BatchNorm1d(num_features = 64 ),
nn.ReLU(),
nn.MaxPool1d(kernel_size= 2, stride= 1),
)
self.conv_3 = nn.Sequential(
nn.Conv1d( in_channels = 64, out_channels=16, kernel_size= 2, stride= 1),
nn.BatchNorm1d(num_features = 16 ),
nn.ReLU(),
nn.MaxPool1d(kernel_size= 2, stride= 1),
)
self.gmp_flatten = nn.Sequential(
GlobalMaxPooling(),
Flatten(),
)
self.FC = nn.Linear(16, 1)
self.BN = nn.BatchNorm1d(16)
def forward(self, x):
x = x.view(-1,1,1*10)
x = self.conv_1(x)
x = self.conv_2(x)
x = self.conv_3(x)
x = self.gmp_flatten(x)
x = self.BN(x)
x = self.FC(x)
out = F.sigmoid(x)
return out
# LSTM (pytorch model)
class LSTM_FC(nn.Module):
def __init__(self, n_classes = 2):
super(LSTM_FC, self).__init__()
self.LSTM = nn.LSTM(input_size=1, hidden_size=16 ,batch_first=True)
self.BN = nn.BatchNorm1d(16)
self.FC_1 = nn.Linear(16, 2)
self.FC_2 = nn.Linear(2, 1)
def forward(self, x):
out1, (hn, cn) = self.LSTM(x)
final_state = out1[:,-1,:]
out = self.BN(final_state)
out = self.FC_1(out)
out = self.FC_2(out)
out = F.sigmoid(out)
return out
class MLP_BN(nn.Module):
def __init__(self, n_classes = 2):
super(MLP_BN, self).__init__()
self.FC_1 = nn.Linear(10, 10)
self.FC_2 = nn.Linear(10, 20)
self.FC_3 = nn.Linear(20, 20)
self.FC_4 = nn.Linear(20, 20)
self.FC_5 = nn.Linear(20, 10)
self.FC_6 = nn.Linear(10, 10)
self.FC_7 = nn.Linear(10, 10)
self.FC_8 = nn.Linear(10, 1)
self.BC_10 = nn.BatchNorm1d(10)
self.BC_20 = nn.BatchNorm1d(20)
def forward(self, x):
x = x.view(-1, 1*10)
out = self.FC_1(x)
out = F.relu(out)
out = self.FC_2(out)
out = self.BC_20(out)
out = F.relu(out)
out = self.FC_3(out)
out = self.BC_20(out)
out = F.relu(out)
out = self.FC_4(out)
out = self.BC_20(out)
out = F.relu(out)
out = self.FC_5(out)
out = self.BC_10(out)
out = F.relu(out)
out = self.FC_6(out)
out = self.BC_10(out)
out = F.relu(out)
out = self.FC_7(out)
out = self.BC_10(out)
out = F.relu(out)
out = self.FC_8(out)
out = F.sigmoid(out)
return out