-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
159 lines (135 loc) · 7.2 KB
/
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
#!/usr/bin/env python3
##########################################################################################
# Author: Tung Kieu
# Date Started: 2018-04-07
# Purpose: Train recurrent neural network to classify Time Series.
##########################################################################################
##########################################################################################
# Libraries
##########################################################################################
import datetime
import numpy as np
import tensorflow as tf
from data import LoadDataWithoutRatio, WriteFile
import os
##########################################################################################
# Write file
##########################################################################################
log_file_name = './/log//TestLSTM_' + str(datetime.date.today()) + '.log'
##########################################################################################
# Load data
##########################################################################################
for root, dirs, files in os.walk('UCR//1', topdown=False):
for dataset in dirs:
tf.reset_default_graph()
x_train, x_test, yTrain_enc, yTest_enc, max_sequence_length, number_of_class = LoadDataWithoutRatio('UCR//1', dataset)
print('--------------------------------------------------')
print(dataset)
WriteFile(log_file_name, 'a', dataset)
##########################################################################################
# Settings
##########################################################################################
# Model settings
#
num_features = 1
num_steps = max_sequence_length
num_cells = 64
num_classes = number_of_class
# Training parameters
#
epochs = 4000
batch_size = 64
learning_rate = 1e-3
##########################################################################################
# Operators
##########################################################################################
# Inputs
#
x = tf.placeholder(tf.float32, [None, num_steps, num_features])
y = tf.placeholder(tf.float32, [None, num_classes])
# Variables
#
cell = tf.nn.rnn_cell.BasicLSTMCell(num_cells) # Modified code to run RWA model
W_end = tf.Variable(tf.truncated_normal([num_cells, num_classes], mean=0.0, stddev=0.1))
b_end = tf.Variable(tf.zeros([num_classes]))
# Model
#
with tf.variable_scope('layer_1'):
h, _ = tf.nn.dynamic_rnn(cell, x, dtype=tf.float32)
with tf.variable_scope('layer_output'):
ly = tf.matmul(h[:, num_steps - 1, :], W_end) + b_end
py = tf.nn.softmax(ly)
# Cost function and optimizer
#
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=ly, labels=y)) # Cross-entropy cost function.
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
# Evaluate performance
#
correct = tf.equal(tf.argmax(py, 1), tf.argmax(y, 1))
accuracy = 100.0 * tf.reduce_mean(tf.cast(correct, tf.float32))
# Create operator to initialize session
#
initializer = tf.global_variables_initializer()
# Create operator for saving the model and its parameters
#
saver = tf.train.Saver()
##########################################################################################
# Session
##########################################################################################
# Start session
init = tf.global_variables_initializer()
print('Start the computation graph')
with tf.Session() as sess:
sess.run(init)
print('Initialized')
# Open session
#
if batch_size > x_train.shape[0]:
feed_dict_train = {x: x_train, y: yTrain_enc}
feed_dict_test = {x: x_test, y: yTest_enc}
for epoch in range(epochs):
_tr, l_tr = sess.run([optimizer, cost], feed_dict=feed_dict_train)
if epoch % 100 == 0:
# Print report to user
#
print(' Iteration:', epoch)
print(' Cost (Training): ', cost.eval(feed_dict_train), 'bits')
print(' Accuracy (Training): ', accuracy.eval(feed_dict_train), '%')
print(' Cost (Test): ', cost.eval(feed_dict_test), 'bits')
print(' Accuracy (Test):', accuracy.eval(feed_dict_test), '%')
print('', flush=True)
# Save the trained model
#
sess.run([optimizer, cost], feed_dict={x: x_test, y: yTest_enc})
print('--------------------------------------------------')
print('Testing accuracy: {:.1f}'.format(accuracy.eval({x: x_test, y: yTest_enc})))
print('--------------------------------------------------')
WriteFile(log_file_name, 'a', 'Testing accuracy: {:.1f}'.format(accuracy.eval({x: x_test, y: yTest_enc})))
saver.save(sess, 'bin/TestRLSTM_' + dataset + '.ckpt')
else:
for epoch in range(epochs):
offset = (epoch * batch_size) % (x_train.shape[0] - batch_size)
batch_data_train = x_train[offset:(offset + batch_size), :]
batch_labels_train = yTrain_enc[offset:(offset + batch_size), :]
feed_dict_train = {x: batch_data_train, y: batch_labels_train}
batch_data_test = x_test[offset:(offset + batch_size), :]
batch_labels_test = yTest_enc[offset:(offset + batch_size), :]
feed_dict_test = {x: batch_data_test, y: batch_labels_test}
_tr, l_tr = sess.run([optimizer, cost], feed_dict=feed_dict_train)
if epoch % 100 == 0:
# Print report to user
#
print(' Iteration:', epoch)
print(' Cost (Training): ', cost.eval(feed_dict_train), 'bits')
print(' Accuracy (Training): ', accuracy.eval(feed_dict_train), '%')
print(' Cost (Test): ', cost.eval(feed_dict_test), 'bits')
print(' Accuracy (Test):', accuracy.eval(feed_dict_test), '%')
print('', flush=True)
# Save the trained model
#
sess.run([optimizer, cost], feed_dict={x: x_test, y: yTest_enc})
print('--------------------------------------------------')
print('Testing accuracy: {:.1f}'.format(accuracy.eval({x: x_test, y: yTest_enc})))
print('--------------------------------------------------')
WriteFile(log_file_name, 'a', 'Testing accuracy: {:.1f}'.format(accuracy.eval({x: x_test, y: yTest_enc})))
saver.save(sess, 'bin/TestLSTM_' + dataset + '.ckpt')