-
Notifications
You must be signed in to change notification settings - Fork 1
/
deep_lstm_model.py
362 lines (284 loc) · 14.1 KB
/
deep_lstm_model.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from HAR_data_handler import get_HAR_data
import tensorflow as tf
from sklearn import metrics
from sklearn.utils import shuffle
import numpy as np
from base_config import Config, YaxisBundle, PlotUtil
def one_hot(y):
"""convert label from dense to one hot
argument:
label: ndarray dense label ,shape: [sample_num,1]
return:
one_hot_label: ndarray one hot, shape: [sample_num,n_class]
"""
# e.g.: [[5], [0], [3]] --> [[0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0]]
y = y.reshape(len(y))
n_values = np.max(y) + 1
return np.eye(n_values)[np.array(y, dtype=np.int32)] # Returns FLOATS
def apply_batch_norm(input_tensor, config, i):
with tf.variable_scope("batch_norm") as scope:
if i != 0 :
# Do not create extra variables for each time step
scope.reuse_variables()
# Mean and variance normalisation simply crunched over all axes
axes = list(range(len(input_tensor.get_shape())))
mean, variance = tf.nn.moments(input_tensor, axes=axes, shift=None, name=None, keep_dims=False)
stdev = tf.sqrt(variance + 0.001)
# Rescaling
bn = input_tensor - mean
bn /= stdev
# Learnable extra rescaling
# tf.get_variable("relu_fc_weights", initializer=tf.random_normal(mean=0.0, stddev=0.0)
bn *= tf.get_variable("a_noreg", initializer=tf.random_normal([1], mean=0.5, stddev=0.0))
bn += tf.get_variable("b_noreg", initializer=tf.random_normal([1], mean=0.0, stddev=0.0))
# bn *= tf.Variable(0.5, name=(scope.name + "/a_noreg"))
# bn += tf.Variable(0.0, name=(scope.name + "/b_noreg"))
return bn
def relu_fc(input_2D_tensor_list, features_len, new_features_len, config):
"""make a relu fully-connected layer, mainly change the shape of tensor
both input and output is a list of tensor
argument:
input_2D_tensor_list: list shape is [batch_size,feature_num]
features_len: int the initial features length of input_2D_tensor
new_feature_len: int the final features length of output_2D_tensor
config: Config used for weights initializers
return:
output_2D_tensor_list lit shape is [batch_size,new_feature_len]
"""
W = tf.get_variable(
"relu_fc_weights",
initializer=tf.random_normal(
[features_len, new_features_len],
mean=0.0,
stddev=float(config.weights_stddev)
)
)
b = tf.get_variable(
"relu_fc_biases_noreg",
initializer=tf.random_normal(
[new_features_len],
mean=float(config.bias_mean),
stddev=float(config.weights_stddev)
)
)
# intra-timestep multiplication:
output_2D_tensor_list = [
tf.nn.relu(tf.matmul(input_2D_tensor, W) + b)
for input_2D_tensor in input_2D_tensor_list
]
return output_2D_tensor_list
def single_LSTM_cell(input_hidden_tensor, n_outputs):
with tf.variable_scope("lstm_cell"):
lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_outputs, state_is_tuple=True, forget_bias=0.999)
#outputs, _ = tf.nn.rnn(lstm_cell, input_hidden_tensor, dtype=tf.float32)
outputs, _ = tf.contrib.rnn.static_rnn(lstm_cell, input_hidden_tensor, dtype=tf.float32)
return outputs
def stack_single_LSTM_layer(input_hidden_tensor, n_input, n_output, layer_level, config, keep_prob_for_dropout):
with tf.variable_scope('layer_{}'.format(layer_level)) as scope:
if config.batch_norm_enabled :
input_hidden_tensor = [apply_batch_norm(out, config, i) for i, out in enumerate(input_hidden_tensor)]
hidden_LSTM_layer = single_LSTM_cell(input_hidden_tensor, n_output)
#hidden_LSTM_layer = single_LSTM_cell(relu_fc(input_hidden_tensor, n_input, n_output, config), n_output)
return hidden_LSTM_layer
def get_deeply_stacked_LSTM_layers(input_hidden_tensor, n_input, n_output, config, keep_prob_for_dropout):
# creating base lstm Layer
print "\nCreating hidden #1:"
hidden = stack_single_LSTM_layer(input_hidden_tensor, config.n_inputs, config.n_hidden, 1, config, keep_prob_for_dropout)
print (len(hidden), str(hidden[0].get_shape()))
# Stacking LSTM layer on existing layer in a for loop
for stacked_hidden_index in range(config.n_stacked_layers - 1):
# If the config permits it, we stack more lstm cells:
print "\nCreating hidden #{}:".format(stacked_hidden_index + 2)
hidden = stack_single_LSTM_layer(hidden, config.n_hidden, config.n_hidden, stacked_hidden_index + 2, config,
keep_prob_for_dropout)
print (len(hidden), str(hidden[0].get_shape()))
print ""
return hidden
def deep_LSTM_network(feature_mat, config, keep_prob_for_dropout):
with tf.variable_scope('LSTM_network') as scope: # TensorFlow graph naming
feature_mat = tf.nn.dropout(feature_mat, keep_prob_for_dropout)
feature_mat = tf.transpose(feature_mat, [1, 0, 2])
feature_mat = tf.reshape(feature_mat, [-1, config.n_inputs])
print feature_mat.get_shape()
# Split the series because the rnn cell needs time_steps features, each of shape:
hidden = tf.split(axis=0, num_or_size_splits=config.n_steps, value=feature_mat)
print (len(hidden), str(hidden[0].get_shape()))
# New shape: a list of lenght "time_step" containing tensors of shape [batch_size, n_hidden]
hidden = get_deeply_stacked_LSTM_layers(hidden, config.n_inputs, config.n_hidden, config, keep_prob_for_dropout)
# Final fully-connected activation logits
# Get the last output tensor of the inner loop output series, of shape [batch_size, n_classes]
lstm_last_output = hidden[-1]
# Linear activation
return tf.matmul(lstm_last_output, config.W['output']) + config.biases['output']
last_hidden = tf.nn.dropout(hidden[-1], keep_prob_for_dropout)
last_logits = relu_fc(
[last_hidden],
config.n_hidden, config.n_classes, config
)[0]
return last_logits
################################## load data and config ##################################
X_train, y_train, X_test, y_test = get_HAR_data()
class DeepLSTMConfig(Config):
def __init__(self):
super(DeepLSTMConfig, self).__init__()
self.train_count = len(X_train) # 7352 training series
self.test_data_count = len(X_test) # 2947 testing series
self.n_steps = len(X_train[0]) # 128 time_steps per series
# Trainging
self.learning_rate = 0.005
self.lambda_loss_amount = 0.0015
self.training_epochs = 300
self.batch_size = 1500
# LSTM structure
self.n_inputs = len(X_train[0][0]) # == 9 Features count is of 9: three 3D sensors features over time
self.n_hidden = 32 # nb of neurons inside the neural network
self.n_classes = 6 # Final output classes
self.W = {
'hidden': tf.Variable(tf.random_normal([self.n_inputs, self.n_hidden])),
'output': tf.Variable(tf.random_normal([self.n_hidden, self.n_classes]))
}
self.biases = {
'hidden': tf.Variable(tf.random_normal([self.n_hidden], mean=1.0)),
'output': tf.Variable(tf.random_normal([self.n_classes]))
}
self.keep_prob_for_dropout = 0.85
self.bias_mean = 0.3
self.weights_stddev = 0.2
self.n_layers_in_highway = 0
self.n_stacked_layers = 3
self.batch_norm_enabled = True
self.also_add_dropout_between_stacked_cells = False
self.model_name = "deep_lstm" + "_HAR"
self.log_folder_suffix = self.attach_log_suffix()
self.logs_path = "/tmp/LSTM_logs/"+self.log_folder_suffix
self.tensor_board_logging_enabled = True
self.tensorboard_cmd = "tensorboard --logdir="+ self.logs_path
self.model_desc_attched_string = self.attach_mdoel_desc()
self.matplot_lib_enabled = True
self.matplot_lib_for_accuracy =True
self.matplot_lib_for_single_ybundle=False
#config = Config(X_train, X_test)
config = DeepLSTMConfig()
def run_with_config(config) : #, X_train, y_train, X_test, y_test):
tf.reset_default_graph() # To enable to run multiple things in a loop
config.print_config()
if config.matplot_lib_enabled:
# To keep track of training's performance
test_losses = []
test_accuracies = []
indep_test_axis = []
config.W = {
'hidden': tf.Variable(tf.random_normal([config.n_inputs, config.n_hidden])),
'output': tf.Variable(tf.random_normal([config.n_hidden, config.n_classes]))
}
config.biases = {
'hidden': tf.Variable(tf.random_normal([config.n_hidden], mean=1.0)),
'output': tf.Variable(tf.random_normal([config.n_classes]))
}
#-----------------------------------
# Define parameters for model
#-----------------------------------
print("Some useful info to get an insight on dataset's shape and normalisation:")
print("features shape, labels shape, each features mean, each features standard deviation")
print(X_test.shape, y_test.shape,
np.mean(X_test), np.std(X_test))
print("the dataset is therefore properly normalised, as expected.")
# ------------------------------------------------------
# step3: Let's get serious and build the neural network
# ------------------------------------------------------
X = tf.placeholder(tf.float32, [None, config.n_steps, config.n_inputs])
Y = tf.placeholder(tf.float32, [None, config.n_classes])
pred_Y = deep_LSTM_network(X, config, 0.85)
print "Unregularised variables:"
for unreg in [tf_var.name for tf_var in tf.trainable_variables() if
("noreg" in tf_var.name or "Bias" in tf_var.name)]:
print unreg
# Loss,optimizer,evaluation
l2 = config.lambda_loss_amount * \
sum(tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables())
# Softmax loss and L2
cost = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(logits=pred_Y, labels=Y)) + l2
optimizer = tf.train.AdamOptimizer(
learning_rate=config.learning_rate).minimize(cost)
correct_pred = tf.equal(tf.argmax(pred_Y, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, dtype=tf.float32))
# ------------------------------------------------------
# step3.5 : Tensorboard stuff here
# ------------------------------------------------------
if config.tensor_board_logging_enabled:
tf.summary.scalar("loss", cost)
tf.summary.scalar("accuracy", accuracy)
merged_summary_op = tf.summary.merge_all()
# --------------------------------------------
# step4: Hooray, now train the neural network
# --------------------------------------------
# Note that log_device_placement can be turned ON but will cause console spam.
sess = tf.InteractiveSession(config=tf.ConfigProto(log_device_placement=False))
tf.global_variables_initializer().run()
if config.tensor_board_logging_enabled:
# op to write logs to Tensorboard
summary_writer = tf.summary.FileWriter(config.logs_path, graph=tf.get_default_graph())
best_accuracy = 0.0
# Start training for each batch and loop epochs
for i in range(config.training_epochs):
for start, end in zip(range(0, config.train_count, config.batch_size),
range(config.batch_size, config.train_count + 1, config.batch_size)):
if config.tensor_board_logging_enabled:
_, summary = sess.run([optimizer, merged_summary_op],
feed_dict={X: X_train[start:end], Y: y_train[start:end]})
else:
sess.run(optimizer, feed_dict={X: X_train[start:end], Y: y_train[start:end]})
if config.tensor_board_logging_enabled:
# Write logs at every iteration
summary_writer.add_summary(summary, i)
# Test completely at every epoch: calculate accuracy
pred_out, accuracy_out, loss_out = sess.run([pred_Y, accuracy, cost], feed_dict={
X: X_test, Y: y_test})
if config.matplot_lib_enabled:
indep_test_axis.append(i)
test_losses.append(loss_out)
test_accuracies.append(accuracy_out)
print("traing iter: {},".format(i) + \
" test accuracy : {},".format(accuracy_out) + \
" loss : {}".format(loss_out))
best_accuracy = max(best_accuracy, accuracy_out)
print("")
print("final test accuracy: {}".format(accuracy_out))
print("best epoch's test accuracy: {}".format(best_accuracy))
print("")
if config.tensor_board_logging_enabled:
print("Run the command line:\n")
print(config.tensorboard_cmd)
print("\nThen open http://0.0.0.0:6006/ into your web browser")
print(config.model_desc_attched_string)
if config.matplot_lib_enabled:
#for i in range(config.batch_size):
# indep_test_axis.append(i)
#indep_test_axis = [i for i in range(config.batch_size)]
#indep_test_axis = np.array(indep_test_axis)
#p = PlotUtil("title", indep_test_axis, "x_label", "y_label")
y_bundle = []
test_losses = np.array(test_losses)
test_accuracies = np.array(test_accuracies)
y = YaxisBundle(np.array(test_losses), "loss", "b")
y_bundle.append(y)
y = YaxisBundle(np.array(test_accuracies), "accuracy", "g")
y_bundle.append(y)
#p.show_plot(y_bundle)
if config.matplot_lib_for_single_ybundle:
if config.matplot_lib_for_accuracy:
return y_bundle[1]
else :
return y_bundle[0]
return y_bundle
if __name__ == '__main__':
if config.matplot_lib_enabled:
indep_test_axis = []
for i in range(config.training_epochs):
indep_test_axis.append(i)
p = PlotUtil("Stacked LSTM(3 layers) on HAR", np.array(indep_test_axis), "Epoch iterations", "Loss or Accuracy")
y_bundle = run_with_config(config)
p.show_plot(y_bundle)
else:
run_with_config(config)