-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer.py
121 lines (95 loc) · 4.44 KB
/
infer.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
import tensorflow as tf
import numpy as np
import sys
from optparse import OptionParser
import os
import utils as utl
def RNN(x, weights, biases, timesteps, num_hidden):
# Unstack to get a list of 'timesteps' tensors of shape (batch_size, n_input)
x = tf.unstack(x, timesteps, 1)
# Define a lstm cell with tensorflow
lstm_cell = tf.contrib.rnn.BasicLSTMCell(num_hidden, forget_bias=1.0)
# Get lstm cell output
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
# Linear activation, using rnn inner loop last output
return tf.matmul(outputs[-1], weights['out']) + biases['out']
def main():
if (len(sys.argv) <= 1):
print("infer.py -h or --help to get guideline of input options")
exit()
use = "Usage: %prog [options] filename"
parser = OptionParser(usage = use)
parser.add_option("-d", "--input-dir", dest="input_dir", action="store", type="string", help="input data dir")
parser.add_option("-t", "--timesteps", dest="timesteps", action="store", type="int", help="timesteps")
parser.add_option("-n", "--num-input", dest="num_input", action="store", type="int", help="number of input (input vector's width)")
parser.add_option("-c", "--ckpt-dir", dest="ckpt_dir", action="store", type="string", help="directory of checkpoint")
(options, args) = parser.parse_args()
input_dir = options.input_dir
timesteps = options.timesteps
num_input = options.num_input
#ckpt_dir = options.ckpt_dir
X = np.fromfile(input_dir + '/X.dat', dtype=float)
cardinality = int(X.shape[0]/(timesteps * num_input))
X = X.reshape([cardinality, timesteps, num_input])
Y = np.fromfile(input_dir + '/Y.dat', dtype=float)
train_x, val_x, test_x, train_y, val_y, test_y = utl.train_val_test_split(X, Y, split_frac=0.80)
# Training Parameters
learning_rate = 0.001
epochs =800
batch_size = 40
#display_step = 200
# Network Parameters
#num_input = 2
#timesteps = 480
num_hidden = 2048
num_classes = 1
print("### Network Parameters ###")
print("Learning Rate: {}".format(learning_rate))
print("Batch Size: {}".format(batch_size))
print("Size of Hidden Layer: {}".format(num_hidden))
print("Timestep: {}".format(timesteps))
print("------------------")
X_ = tf.placeholder("float", [None, timesteps, num_input])
Y_ = tf.placeholder("float", [None, num_classes])
lr = tf.placeholder("float")
weights = {
'out':tf.Variable(tf.random_normal([num_hidden,num_classes])),
}
biases = {
'out':tf.Variable(tf.random_normal([num_classes]))
}
prediction = RNN(X_, weights, biases, timesteps, num_hidden)
loss_op = tf.losses.mean_squared_error(Y_, prediction)
#optimizer = tf.train.AdadeltaOptimizer(lr).minimize(loss_op)
#optimizer = tf.train.AdamOptimizer(lr).minimize(loss_op)
optimizer = tf.train.GradientDescentOptimizer(lr).minimize(loss_op)
correct_pred = tf.equal(tf.cast( (prediction/1.8) - tf.round(prediction/1.8), tf.float32), tf.cast( (prediction/1.8)-tf.round(Y_/1.8), tf.float32))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# Restore the ckpt
SAVER_DIR = options.ckpt_dir
saver = tf.train.Saver()
checkpoint_path = os.path.join(SAVER_DIR, SAVER_DIR)
ckpt = tf.train.get_checkpoint_state(SAVER_DIR)
init = tf.global_variables_initializer()
with tf.Session() as sess:
#new_saver = tf.train.import_meta_graph('ckpt.meta')
saver.restore(sess, ckpt.model_checkpoint_path)
test_norm = utl.minmax_norm(test_x)
print("loss test: %f" % loss_op.eval(feed_dict = {X_:test_norm, Y_:test_y[:, None]}))
X_norm = utl.minmax_norm(X)
pred = np.array(prediction.eval(feed_dict = {X_:X_norm, Y_:Y[:, None]}))
pred_diagnosis = [1 if x[0]>=1.8 else 0 for x in list(pred)]
y_diagnosis = [1 if x>=1.8 else 0 for x in list(Y)]
evaluation = np.equal(pred_diagnosis, y_diagnosis)
print(np.mean(evaluation))
f = open(SAVER_DIR + '/result.txt', 'w')
for i in range(0, len(Y)):
f.write(str(pred[i][0]) + ', ' + str(Y[i])+'\n')
f2 = open(SAVER_DIR + '/result_diagnosis.txt', 'w')
for i in range(0, len(Y)):
f2.write(str(pred_diagnosis[i]) + ', ' + str(y_diagnosis[i])+'\n')
f2.close()
f.close()
# sess.close()
if __name__=="__main__":
main()