-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels_tf.py
170 lines (119 loc) · 5.8 KB
/
models_tf.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
#---------------------------------------------------------------------------------------------------#
# File name: models_tf.py #
# Autor: Chrissi2802 #
# Created on: 05.10.2022 #
#---------------------------------------------------------------------------------------------------#
# WISDM - Biometric time series data classification
# Exact description in the functions.
# This file provides the models for tensorflow.
from tensorflow.keras.models import Model
import tensorflow.keras.layers as layer
def mlp_net_tf(data):
"""This function creates a MLP model in TensorFlow."""
# Input:
# data; NumPy array, data fed into the model, here only relevant to find out the input shape
# Output:
# model; TensorFlow / Keras model, model for training and testing
x_input = layer.Input(shape = (data.shape[-2:]))
x = layer.Flatten()(x_input)
x = layer.Dense(256, activation = "relu")(x)
x = layer.BatchNormalization()(x)
x = layer.Dropout(0.5)(x)
x = layer.Dense(128, activation = "relu")(x)
x = layer.BatchNormalization()(x)
x = layer.Dropout(0.5)(x)
x_output = layer.Dense(6, activation = "softmax")(x)
model = Model(inputs = x_input, outputs = x_output, name = "MLP_NET_TF")
return model
def cnn_net_tf(data):
"""This function creates a CNN model in TensorFlow."""
# Input:
# data; NumPy array, data fed into the model, here only relevant to find out the input shape
# Output:
# model; TensorFlow / Keras model, model for training and testing
x_input = layer.Input(shape = (data.shape[-3:]))
x = layer.Conv2D(64, (2, 2), activation = "relu")(x_input)
x = layer.BatchNormalization()(x)
x = layer.Conv2D(128, (2, 2), activation = "relu")(x)
x = layer.BatchNormalization()(x)
x = layer.Flatten()(x)
x = layer.Dense(128, activation = "relu")(x)
x = layer.BatchNormalization()(x)
x = layer.Dropout(0.5)(x)
x_output = layer.Dense(6, activation = "softmax")(x)
model = Model(inputs = x_input, outputs = x_output, name = "CNN_NET_TF")
return model
def gru_net_tf(data):
"""This function creates a GRU model in TensorFlow."""
# Input:
# data; NumPy array, data fed into the model, here only relevant to find out the input shape
# Output:
# model; TensorFlow / Keras model, model for training and testing
x_input = layer.Input(shape = (data.shape[-2:]))
x = layer.Bidirectional(layer.GRU(256, return_sequences = True))(x_input)
x = layer.BatchNormalization()(x)
x = layer.Bidirectional(layer.GRU(128, return_sequences = True))(x)
x = layer.BatchNormalization()(x)
x = layer.Flatten()(x)
x = layer.Dense(128, activation = "relu")(x)
x = layer.BatchNormalization()(x)
x = layer.Dropout(0.5)(x)
x_output = layer.Dense(6, activation = "softmax")(x)
model = Model(inputs = x_input, outputs = x_output, name = "GRU_NET_TF")
return model
def lstm_net_tf(data):
"""This function creates a LSTM model in TensorFlow."""
# Input:
# data; NumPy array, data fed into the model, here only relevant to find out the input shape
# Output:
# model; TensorFlow / Keras model, model for training and testing
x_input = layer.Input(shape = (data.shape[-2:]))
x = layer.Bidirectional(layer.LSTM(256, return_sequences = True))(x_input)
x = layer.BatchNormalization()(x)
x = layer.Bidirectional(layer.LSTM(128, return_sequences = True))(x)
x = layer.BatchNormalization()(x)
x = layer.Flatten()(x)
x = layer.Dense(128, activation = "relu")(x)
x = layer.BatchNormalization()(x)
x = layer.Dropout(0.5)(x)
x_output = layer.Dense(6, activation = "softmax")(x)
model = Model(inputs = x_input, outputs = x_output, name = "LSTM_NET_TF")
return model
def gru_net_big_tf(data):
"""This function creates a big GRU model in TensorFlow."""
# Input:
# data; NumPy array, data fed into the model, here only relevant to find out the input shape
# Output:
# model; TensorFlow / Keras model, model for training and testing
x_input = layer.Input(shape = (data.shape[-2:]))
x = layer.Bidirectional(layer.GRU(512, return_sequences = True))(x_input)
x = layer.BatchNormalization()(x)
x = layer.Bidirectional(layer.GRU(256, return_sequences = True))(x)
x = layer.BatchNormalization()(x)
x = layer.Bidirectional(layer.GRU(128, return_sequences = True))(x)
x = layer.BatchNormalization()(x)
x = layer.Flatten()(x)
x = layer.Dense(128, activation = "relu")(x)
x = layer.BatchNormalization()(x)
x = layer.Dropout(0.5)(x)
x_output = layer.Dense(6, activation = "softmax")(x)
model = Model(inputs = x_input, outputs = x_output, name = "GRU_NET_BIG_TF")
return model
def conv_lstm_net_tf(data):
"""This function creates a convolutional LSTM model in TensorFlow."""
# Input:
# data; NumPy array, data fed into the model, here only relevant to find out the input shape
# Output:
# model; TensorFlow / Keras model, model for training and testing
x_input = layer.Input(shape = (data.shape[-4:]))
x = layer.Bidirectional(layer.ConvLSTM2D(64, (3, 3), return_sequences = True))(x_input)
x = layer.BatchNormalization()(x)
x = layer.Bidirectional(layer.ConvLSTM2D(128, (3, 3), return_sequences = True))(x)
x = layer.BatchNormalization()(x)
x = layer.Flatten()(x)
x = layer.Dense(128, activation = "relu")(x)
x = layer.BatchNormalization()(x)
x = layer.Dropout(0.5)(x)
x_output = layer.Dense(6, activation = "softmax")(x)
model = Model(inputs = x_input, outputs = x_output, name = "CONV_LSTM_NET_TF")
return model