-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlstm_test2.py
177 lines (136 loc) · 5.1 KB
/
lstm_test2.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
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 18 15:21:56 2019
@author: KLY
"""
#implement LSTM with TensorFlow
import numpy as np
import tensorflow as tf
import math
import matplotlib.pyplot as plt
import pandas as pd
tf.reset_default_graph()
# In[]
xlsfile=pd.read_excel('data.xlsx',header=None)
data=np.array(xlsfile)
n=np.random.randint(0,data.shape[0],data.shape[0])
#load data/features from numpy array
train_data = data[n[0:3000],0:8]
valid_data = data[n[3000:3500],0:8]
test_data = data[n[3500:],0:8]
#load labels from numpy array
train_label = data[n[0:3000],8:]
valid_label = data[n[3000:3500],8:]
test_label = data[n[3500:],8:]
#fixed random seed
tf.set_random_seed(100)
#define hyper params
num_epochs = 10
batch_size = 2
alpha = 0.01
hidden_nodes = 40
input_features = 8
output_class = 4 #regression 8 input----4 output
# input placeholder
X = tf.placeholder("float", [None, input_features])
Y = tf.placeholder("float", [None, output_class])
# define weights, gaussian distribution
weights = {
'out': tf.Variable(tf.random_normal([hidden_nodes, output_class]))
}
biases = {
'out': tf.Variable(tf.random_normal([output_class]))
}
# define the RNN lstm network
def RNN(x):
# reshape input tensor into batch x sequence length x # of features
x = tf.reshape(x , [-1, 1,input_features])
# triple layer LSTM with same number of nodes each layer
# lstm_cell1 = tf.nn.rnn_cell.LSTMCell(10)
# lstm_cell2 = tf.nn.rnn_cell.LSTMCell(10)
lstm_cell3 = tf.nn.rnn_cell.LSTMCell(hidden_nodes)
#stack of those layers
# lstm_cell = tf.nn.rnn_cell.MultiRNNCell([lstm_cell1, lstm_cell2, lstm_cell3])
lstm_cell = tf.nn.rnn_cell.MultiRNNCell([lstm_cell3])
#initialize state
init_state = lstm_cell.zero_state(tf.shape(x)[0], dtype=tf.float32)
#get the output of each state
outputs, _ = tf.nn.dynamic_rnn(lstm_cell, x, dtype=tf.float32, initial_state=init_state)
output_sequence = tf.matmul(tf.reshape(outputs, [-1, hidden_nodes]), weights['out']) + biases['out']
return tf.reshape(output_sequence, [-1, output_class])
#initialization
logits = RNN(X)
loss = tf.losses.mean_squared_error(predictions = logits, labels = Y)
global_step = tf.Variable(0)
learning_rate = tf.train.exponential_decay(
alpha,
global_step,
num_epochs, 0.99,
staircase=True)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, epsilon = 1e-10).minimize(loss,global_step=global_step)
init = tf.global_variables_initializer()
#lists to keep track of training, validation and test loss at each epoch
train = []
valid = []
test = []
with tf.Session() as sess:
sess.run(init)
#get # of inputs
N = train_data.shape[0]
# training cycle
for epoch in range(num_epochs):
# Apply SGD, each time update with one batch and shuffle randomly
total_batch = int(math.ceil(N / batch_size))
indices = np.arange(N)
np.random.shuffle(indices)
avg_loss = 0
for i in range(total_batch):
#get one batch
rand_index = indices[batch_size*i:batch_size*(i+1)]
x = train_data[rand_index]
y = train_label[rand_index]
_, cost = sess.run([optimizer, loss],
feed_dict={X: x, Y: y})
avg_loss += cost / total_batch
# Monitoring Validation and Test Loss per each epoch updates
valid_data=valid_data.reshape(-1,input_features)
valid_y = valid_label.reshape(-1, output_class)
valid_loss = sess.run(loss, feed_dict={X: valid_data, Y: valid_y})
#take the square root
avg_loss = np.sqrt(avg_loss)
valid_loss = np.sqrt(valid_loss)
#append to list for drawing
train.append(avg_loss)
valid.append(valid_loss)
print('epoch:',epoch,' ,train loss ',avg_loss,' ,valid loss: ',valid_loss)
#calculate result each dataset
train_data=train_data.reshape(-1,input_features)
train_pred = sess.run(logits, feed_dict={X: train_data})
train_pred = train_pred.reshape(-1, output_class)
valid_data=valid_data.reshape(-1,input_features)
valid_pred = sess.run(logits, feed_dict={X: valid_data})
valid_pred = valid_pred.reshape(-1, output_class)
test_data=test_data.reshape(-1,input_features)
test_pred = sess.run(logits, feed_dict={X: test_data})
test_pred = test_pred.reshape(-1, output_class)
# calculate mse
test_mse=np.mean(np.square(test_pred-test_label))
# In[] plot RMSE vs epoch
g = plt.figure(1)
plt.ylabel('RMSE')
plt.xlabel('Epoch')
plt.plot( train, label='training')
#plt.plot( valid, label='validation')
#plt.plot( test, label='test')
plt.title('loss curve')
plt.legend()
plt.show()
# plot test_set result
for i in range(4):
plt.figure()
plt.plot(test_label[:,i],c='r', label='true')
plt.plot(test_pred[:,i],c='b',label='predict')
string='the '+str(i+1)+' output'
plt.title(string)
plt.legend()
plt.show()