-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathILearnerModel.py
executable file
·65 lines (59 loc) · 2.52 KB
/
ILearnerModel.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
# Author: Mark Gee
# Platform: keras
# ILearner model definitions
# Here, we use LSTM, GRU, CNN LSTM, ConvLSTM2D
from keras.layers import LSTM, GRU, ConvLSTM2D, Dense, Flatten, Dropout, LSTM, TimeDistributed, ConvLSTM2D, Conv1D, MaxPooling1D, Masking, Reshape
from keras.models import Sequential, Model
from keras.utils import to_categorical
from keras.layers import Layer, Input
import numpy as np
def lstm(batch_size=None, n_timesteps=None, stateful=False, levels=6):
if stateful:
input = Input(batch_shape=(batch_size, n_timesteps, 8))
else:
input = Input(shape=(None, 8))
x = Masking(mask_value=0.)(input)
x = LSTM(128, stateful=stateful)(x)
x = Dropout(0.5)(x)
x = Dense(128, activation='relu')(x)
x = Dense(levels, activation='softmax')(x)
return Model(input, x)
def gru(batch_size=None, n_timesteps=None, stateful=False, levels=6):
if stateful:
input = Input(batch_shape=(batch_size, n_timesteps, 8))
else:
input = Input(shape=(None, 8))
x = Masking(mask_value=0.)(input)
x = GRU(256, stateful=stateful)(x)
x = Dropout(0.5)(x)
x = Dense(128, activation='relu')(x)
x = Dense(levels, activation='softmax')(x)
return Model(input, x)
def cnn_lstm(n_length, batch_size=None, n_timesteps=None, stateful=False, levels=6):
if stateful:
input = Input(batch_shape=(batch_size, n_timesteps, 8))
else:
input = Input(shape=(None, 8))
x = Reshape((n_timesteps//n_length, n_length, 8))(input)
x = TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu'))(x)
x = TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu'))(x)
x = TimeDistributed(Dropout(0.5))(x)
x = TimeDistributed(MaxPooling1D(pool_size=2))(x)
x = TimeDistributed(Flatten())(x)
x = LSTM(256, stateful=stateful)(x)
x = Dropout(0.2)(x)
x = Dense(100, activation='relu')(x)
x = Dense(levels, activation='softmax')(x)
return Model(input, x)
def convlstm2d(n_length, batch_size=None, n_timesteps=None, stateful=False, levels=6):
if stateful:
input = Input(batch_shape=(batch_size, n_timesteps, 8))
else:
input = Input(shape=(None, 8))
x = Reshape((n_timesteps//n_length, 1, n_length, 8))(input)
x = ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu', input_shape=(n_timesteps//n_length, 1, n_length, 8), stateful=stateful)(x)
x = Dropout(0.2)(x)
x = Flatten()(x)
x = Dense(100, activation='relu')(x)
x = Dense(levels, activation='softmax')(x)
return Model(input, x)