-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress_models.py
202 lines (159 loc) · 7.49 KB
/
stress_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
import tensorflow as tf
from tensorflow import keras
def get_simple_cnn_model(input_shape, metrics, learning_rate):
"""Create a simple CNN model for EDA based stress binary classification.
input_shape -- Tuple, needed for the first layer
metrics -- list, metrics to optimize
learning_rate -- float, learning rate for the Adam optimizer
"""
temp_model = keras.Sequential([
keras.layers.Conv1D(filters=100, kernel_size = (10), strides = 1, activation = tf.nn.relu,
input_shape = input_shape, padding='same'),
keras.layers.Conv1D(filters = 100, kernel_size = (5), strides = 1,
activation = tf.nn.relu, padding='same'),
keras.layers.GlobalMaxPool1D(),
keras.layers.Dense(units = 264, activation = tf.nn.relu),
keras.layers.Dropout(rate = 0.3),
keras.layers.Dense(units = 128, activation = tf.nn.relu),
keras.layers.Dropout(rate = 0.3),
keras.layers.Dense(units = 64, activation=tf.nn.relu),
keras.layers.Dense(units = 1, activation = tf.nn.sigmoid)
])
temp_model.compile(loss = keras.losses.BinaryCrossentropy(),
optimizer = keras.optimizers.Adam(learning_rate = learning_rate),
metrics = metrics)
return temp_model
def get_supervised_balance_adarp_model(input_shape, metrics, learning_rate):
"""Returns a CNN model for balanced stress dataset.
After hyperparameterization optimization the best set of hyperparameters were:
1. batch_size = 147
2. CNN1 filters = 100
3. CNN1 kernel size = 5
4. CNN2 filters = 50
5. CNN2 kernel size = 10
6. Dense1 units = 128
7. Dense2 units = 256
8. Dense3 units = 64
9. Dropout1 = 0.1
10. Dropout2 = 0.3
11. Learning rate = 0.002558
12. Optimizer = Adam
Returns the CNN model.
"""
temp_model = keras.Sequential([
keras.layers.Conv1D(filters=100, kernel_size = (5), strides = 1, activation = tf.nn.relu,
input_shape = input_shape, padding='same'),
keras.layers.Conv1D(filters = 50, kernel_size = (10), strides = 1,
activation = tf.nn.relu, padding='same'),
keras.layers.GlobalMaxPool1D(),
keras.layers.Dense(units = 128, activation = tf.nn.relu),
keras.layers.Dropout(rate = 0.1),
keras.layers.Dense(units = 256, activation = tf.nn.relu),
keras.layers.Dropout(rate = 0.3),
keras.layers.Dense(units = 64, activation=tf.nn.relu),
keras.layers.Dense(units = 1, activation = tf.nn.sigmoid)
])
temp_model.compile(loss = keras.losses.BinaryCrossentropy(),
optimizer = keras.optimizers.Adam(learning_rate = learning_rate),
metrics = metrics)
return temp_model
def get_supervised_full_adarp_model(input_shape, metrics, learning_rate):
"""Returns a large CNN model for binary stress classification.
After hyperparameterization optimization the best set of hyperparameters were:
1. Batch size = 100
2. CNN1 filters = 250
3. CNN1 kernel size = 5
4. CNN2 filters = 100
5. CNN2 kernel size = 5
6. Dense1 units = 256
7. Dense2 units = 128
8. Dense3 units = 64
9. Dropout1 = 0.1
10. Dropout2 = 0.1
11. Learning rate = 0.01157
12. Optimizer = Adam
Return the CNN model
"""
temp_model = keras.Sequential([
keras.layers.Conv1D(filters=250, kernel_size = (5), strides = 1, activation = tf.nn.relu,
input_shape = input_shape, padding='same'),
keras.layers.Conv1D(filters = 100, kernel_size = (5), strides = 1,
activation = tf.nn.relu, padding='same'),
keras.layers.GlobalMaxPool1D(),
keras.layers.Dense(units = 256, activation = tf.nn.relu),
keras.layers.Dropout(rate = 0.1),
keras.layers.Dense(units = 128, activation = tf.nn.relu),
keras.layers.Dropout(rate = 0.1),
keras.layers.Dense(units = 64, activation=tf.nn.relu),
keras.layers.Dense(units = 2, activation = tf.nn.softmax)
])
temp_model.compile(loss = keras.losses.categorical_crossentropy,
optimizer = keras.optimizers.Adam(learning_rate = learning_rate),
metrics = metrics)
return temp_model
def get_transfer_wesad_model(input_shape, metrics, learning_rate):
"""After hyperparameterization optimization the best set of hyperparameters were:
1. Batch size = 120
2. CNN1 filters = 100
3. CNN1 kernel size = 10
4. CNN2 filters = 50
5. CNN2 kernel size = 5
6. Dense1 units = 128
7. Dense2 units = 64
8. Dense3 units = 128
9. Dropout1 = 0.2
10. Dropout2 = 0.3
11. Learning rate = 0.0154
12. Optimizer = Adam
For test loss of 0.2275
Return the CNN model
"""
temp_model = keras.Sequential([
keras.layers.Conv1D(filters = 100, kernel_size = (5), strides = 1, activation = tf.nn.relu,
input_shape = input_shape),
keras.layers.Conv1D(filters = 100, kernel_size = (10), strides = 1, activation = tf.nn.relu),
keras.layers.GlobalMaxPool1D(),
keras.layers.Dense(units = 128, activation = tf.nn.relu),
keras.layers.Dropout(rate = 0.3),
keras.layers.Dense(units = 64, activation = tf.nn.relu, name='penultimate_layer'),
keras.layers.Dropout(rate = 0.2),
keras.layers.Dense(units = 2, activation = tf.nn.softmax)
])
temp_model.compile(loss = keras.losses.categorical_crossentropy,
optimizer = keras.optimizers.Adam(learning_rate = learning_rate),
metrics = metrics)
return temp_model
def get_transfer_adarp_model(input_shape, metrics, learning_rate):
"""After hyperparameterization optimization the best set of hyperparameters were:
1. Batch size = 106
2. CNN1 filters = 250
3. CNN1 kernel size = 2
4. CNN2 filters = 100
5. CNN2 kernel size = 10
6. Dense1 units = 64
7. Dense2 units = 128
8. Dense3 units = 128
9. Dropout1 = 0.1
10. Dropout2 = 0.1
11. Learning rate = 0.002046
12. Optimizer = Adam
For test loss of 0.5263
Return the CNN model
"""
temp_model = keras.Sequential([
keras.layers.Conv1D(filters=250, kernel_size=(2), strides=1, activation=tf.nn.relu,
input_shape=input_shape, padding='same'),
keras.layers.Conv1D(filters=100, kernel_size=(10), strides=1,
activation=tf.nn.relu, padding='same'),
keras.layers.GlobalMaxPool1D(),
keras.layers.Dense(units=64, activation=tf.nn.relu),
keras.layers.Dropout(rate=0.1),
keras.layers.Dense(units=128, activation=tf.nn.relu),
keras.layers.Dropout(rate=0.1),
keras.layers.Dense(units=128, activation=tf.nn.relu),
keras.layers.Dense(units=1, activation=tf.nn.sigmoid)
])
temp_model.compile(loss=keras.losses.BinaryCrossentropy(),
optimizer=keras.optimizers.Adam(learning_rate = learning_rate),
metrics=metrics)
return temp_model