-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodels.py
More file actions
242 lines (177 loc) · 11.1 KB
/
Copy pathmodels.py
File metadata and controls
242 lines (177 loc) · 11.1 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import os
import tensorflow as tf
import model_layers as ml
import model_utils as mu
from model_utils import Constants as c
class Model():
def __init__(self, number_of_sensors, number_of_output_classes, hyper_params):
self.num_sensors = number_of_sensors
self.num_outputs = number_of_output_classes
self.hyer_params = hyper_params
def get_model_function(self):
def model_fn(features, labels, mode, params):
return self._model_function(features, labels, mode, params)
return model_fn
def _model_function(self, features, labels, mode, params):
model_output, tl_input = self._create_model(features, mode, params)
# PREDICTION MODE
if mode == tf.estimator.ModeKeys.PREDICT:
return mu.get_predict_estimatorspec(model_output, tl_input)
# Calculate the loss
ce_loss = mu.cross_entropy_loss(model_output, labels)
l2_loss = mu.l2_regularization_loss(self.hyer_params["l2_lambda_term"])
loss = ce_loss + l2_loss
# EVALUATION MODE
if mode == tf.estimator.ModeKeys.EVAL:
return mu.get_eval_estimatorspec(model_output, labels, loss, self.num_outputs)
# TRAINING MODE
if mode == tf.estimator.ModeKeys.TRAIN:
return mu.get_training_estimatorspec(loss, self.hyer_params)
def _create_model(self, features, mode, params):
# Each subclass defines this method. Here you create the tensorflow computation graph and
# return the output, and the input of the last layer (to be used for personalization).
pass
class LSTM(Model):
def _create_model(self, features, mode, params):
num_intervals = c.NUMBER_OF_INTERVALS
feat_dim = c.get_num_features_per_interval(self.num_sensors)
sensor_inputs, length = mu.prepare_features(features, num_intervals, feat_dim, mode)
inputs_shape = tf.shape(sensor_inputs) # (BATCH_SIZE, NUMBER_OF_INTERVALS, FEATURE_DIM, CHANNEL=1)
sensor_inputs = tf.reshape(sensor_inputs, [inputs_shape[0], num_intervals, feat_dim]) # Get rid of channel dimension
batch_size = inputs_shape[0]
#------ RNN Layers
num_cells = 256
lstm_cell1 = tf.contrib.rnn.LSTMCell(num_cells)
if mode == tf.estimator.ModeKeys.TRAIN:
lstm_cell1 = tf.contrib.rnn.DropoutWrapper(lstm_cell1, output_keep_prob=0.5)
lstm_cell2 = tf.contrib.rnn.LSTMCell(num_cells)
if mode == tf.estimator.ModeKeys.TRAIN:
lstm_cell2 = tf.contrib.rnn.DropoutWrapper(lstm_cell2, output_keep_prob=0.5)
cell = tf.contrib.rnn.MultiRNNCell([lstm_cell1, lstm_cell2])
init_state = cell.zero_state(batch_size, tf.float32)
cell_output, final_stateTuple = tf.nn.dynamic_rnn(cell, sensor_inputs, sequence_length=length, initial_state=init_state, time_major=False)
# cell_output has shape (BATCH_SIZE, NUMBER_OF_INTERVALS, number_of_cells).
cell_output = tf.Print(cell_output, [tf.shape(cell_output), tf.shape(final_stateTuple)])
# Sum the output of the RNN for each example and calculate the mean.
sum_cell_out = tf.reduce_sum(cell_output, axis=1, keepdims=False)
l = tf.reshape(length, [batch_size, 1])
l = tf.cast(l, tf.float32)
avg_cell_out = sum_cell_out/(tf.tile(l, [1, num_cells])) # we have to calculate the mean this way to take into account for the different lengths.
#------ Output Layer
logits = ml.output_layer(avg_cell_out, self.num_outputs)
return logits, avg_cell_out
class DeepSense(Model):
def _create_model(self, features, mode, params):
sensor_inputs, length = mu.prepare_features(features, c.NUMBER_OF_INTERVALS, c.get_num_features_per_interval(self.num_sensors), mode)
batch_size = tf.shape(sensor_inputs)[0]
# Separate sensors data.
num_sensors = self.num_sensors
separate_sensors_data = tf.split(sensor_inputs, num_or_size_splits=num_sensors, axis=2)
#------ Individual Convolutional Layers & Merge Convolutional Layers
conv_layers_output = ml.ds_convolutional_layer(separate_sensors_data, num_sensors, mode)
# Reshape for Recurrent Neural Network.
clo_shape = tf.shape(conv_layers_output)
conv_layers_output = tf.reshape(conv_layers_output, [-1, clo_shape[1], num_sensors*4*64])
# sensor_conv_out has shape (BATCH_SIZE, NUMBER_OF_INTERVALS, ...)
#------ RNN Layers
num_cells = 120
cell_output = ml.stacked_gru_layer(conv_layers_output, length, num_cells, mode)
# cell_output has shape (BATCH_SIZE, NUMBER_OF_INTERVALS, num_cells=120).
# Sum the output of the RNN for each example and calculate the mean.
sum_cell_out = tf.reduce_sum(cell_output, axis=1, keepdims=False)
l = tf.reshape(length, [batch_size, 1])
l = tf.cast(l, tf.float32)
avg_cell_out = sum_cell_out/(tf.tile(l, [1, num_cells])) # we have to calculate the mean this way to take into account for the different lengths.
#------ Output Layer
logits = ml.output_layer(avg_cell_out, self.num_outputs)
return logits, avg_cell_out
class SADeepSense(Model):
def _create_model(self, features, mode, params):
sensor_inputs, length = mu.prepare_features(features, c.NUMBER_OF_INTERVALS, c.get_num_features_per_interval(self.num_sensors), mode)
# Separate sensors data.
num_sensors = self.num_sensors
separate_sensors_data = tf.split(sensor_inputs, num_or_size_splits=num_sensors, axis=2)
#------ Individual Convolutional Layers & Merge Convolutional Layers
conv_layers_output = ml.sads_convolutional_layer(separate_sensors_data, self.num_sensors, self.num_outputs, mode)
# Reshape for Recurrent Neural Network.
clo_shape = tf.shape(conv_layers_output)
conv_layers_output = tf.reshape(conv_layers_output, [-1, clo_shape[1], 4*64])
# sensor_conv_out has shape (BATCH_SIZE, NUMBER_OF_INTERVALS, ...)
#------ RNN Layers
num_cells = 120
cell_output = ml.stacked_gru_layer(conv_layers_output, length, num_cells, mode)
# cell_output has shape (BATCH_SIZE, NUMBER_OF_INTERVALS, num_cells=120).
#------ Temporal Self-Attention Module
merged_timesteps = ml.sa_temporal_module(cell_output, c.NUMBER_OF_INTERVALS, self.num_outputs)
#------ Output Layer
logits = ml.output_layer(merged_timesteps, self.num_outputs)
return logits, merged_timesteps
class TrASenD_BD(Model):
def _create_model(self, features, mode, params):
sensor_inputs, length = mu.prepare_features(features, c.NUMBER_OF_INTERVALS, c.get_num_features_per_interval(self.num_sensors), mode)
batch_size = tf.shape(sensor_inputs)[0]
# Separate sensors data.
num_sensors = self.num_sensors
separate_sensors_data = tf.split(sensor_inputs, num_or_size_splits=num_sensors, axis=2)
#------ Individual Convolutional Layers & Merge Convolutional Layers
conv_layers_output = ml.ds_convolutional_layer(separate_sensors_data, num_sensors, mode)
# Reshape for Recurrent Neural Network.
clo_shape = tf.shape(conv_layers_output)
conv_layers_output = tf.reshape(conv_layers_output, [-1, clo_shape[1], num_sensors*4*64])
# sensor_conv_out has shape (BATCH_SIZE, NUMBER_OF_INTERVALS, ...)
#------ RNN Layers
num_cells = 120
cell_output = ml.bidirectional_gru_layer(conv_layers_output, length, num_cells, mode)
# cell_output has shape (BATCH_SIZE, NUMBER_OF_INTERVALS, 120).
# Sum the output of the RNN for each example and calculate the mean.
sum_cell_out = tf.reduce_sum(cell_output, axis=1, keepdims=False)
l = tf.reshape(length, [batch_size, 1])
l = tf.cast(l, tf.float32)
avg_cell_out = sum_cell_out/(tf.tile(l, [1, num_cells])) # we have to calculate the mean this way to take into account for the different lengths.
#------ Output Layer
logits = ml.output_layer(avg_cell_out, self.num_outputs)
return logits, avg_cell_out
class TrASenD_CA(Model):
def _create_model(self, features, mode, params):
sensor_inputs, length = mu.prepare_features(features, c.NUMBER_OF_INTERVALS, c.get_num_features_per_interval(self.num_sensors), mode)
batch_size = tf.shape(sensor_inputs)[0]
# Separate sensors data.
num_sensors = self.num_sensors
separate_sensors_data = tf.split(sensor_inputs, num_or_size_splits=num_sensors, axis=2)
#------ Individual Convolutional Layers & Merge Convolutional Layers
conv_layers_output = ml.ds_convolutional_layer(separate_sensors_data, num_sensors, mode)
# Reshape for Attention Mechanism
clo_shape = tf.shape(conv_layers_output)
conv_layers_output = tf.reshape(conv_layers_output, [-1, clo_shape[1], num_sensors*4, 64])
conv_layers_output = tf.transpose(conv_layers_output, perm=[0, 1, 3, 2])
# conv_layers_output has shape (BATCH_SIZE, NUMBER_OF_INTERVALS, CHANNELS, FEATURES=clo_shape[2]*clo_shape[3])
#------ GRU with attention mechanism
num_cells = 120
att_gru_output = ml.attention_gru(conv_layers_output, length, num_cells)
# Sum the output at each timestep, taking sequence lengths into account
sum_timesteps_output = tf.reduce_sum(att_gru_output, axis=1, keepdims=False)
l = tf.reshape(length, [batch_size, 1])
l = tf.cast(l, tf.float32)
avg_cell_out = sum_timesteps_output/(tf.tile(l, [1, num_cells]))
#------ Output Layer
logits = ml.output_layer(sum_timesteps_output, self.num_outputs)
return logits, att_gru_output
class TrASenD(Model):
def _create_model(self, features, mode, params):
sensor_inputs, length = mu.prepare_features(features, c.NUMBER_OF_INTERVALS, c.get_num_features_per_interval(self.num_sensors), mode)
batch_size = tf.shape(sensor_inputs)[0]
# Separate sensors data.
num_sensors = self.num_sensors
separate_sensors_data = tf.split(sensor_inputs, num_or_size_splits=num_sensors, axis=2)
#------ Individual Convolutional Layers & Merge Convolutional Layers
conv_layers_output = ml.ds_convolutional_layer(separate_sensors_data, num_sensors, mode)
# Reshape for Attention Mechanism
clo_shape = tf.shape(conv_layers_output)
conv_layers_output = tf.reshape(conv_layers_output, [-1, clo_shape[1], num_sensors*4*64])
# conv_layers_output has shape (BATCH_SIZE, NUMBER_OF_INTERVALS, FEATURES*CHANNELS)
#------ Transformer Encoder
transformer_output = ml.transformer_encoder(conv_layers_output)
transformer_output = tf.reshape(transformer_output, (clo_shape[0], c.NUMBER_OF_INTERVALS* transformer_output.get_shape().as_list()[2]))
#------ Output Layer
logits = ml.output_layer(transformer_output, self.num_outputs)
return logits, transformer_output