forked from Acce1erat0rS/tf_learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf_Baseline_dev.py
executable file
·161 lines (131 loc) · 4.65 KB
/
tf_Baseline_dev.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
#coding=utf-8
import numpy as np
import tensorflow as tf
from tensorflow.contrib import rnn
import random
import ProgressBar as pb
hop = 71
timestep_size = 71 # Hours of looking ahead
output_parameters = 3 # Number of predicting parameters
num_stations = 3 # Number of monitoring stations
training_epochs = 200
_batch_size = 384
data_dir = "./dev_data/"
def process(x):
if x == '?':
return 0.0
else:
return float(x)
dataset = []
split = 200
leap = 6
length = 16
lr = 0.0001
hidden_size = 256
layer_num = 3
# from UNIX time 1395691200
# to UNIX time 1448564400
# Read from the file of the training set
data = []
target_set = []
# defines how many hours is used to predict
# --------------------------------------------
# Data Preparation
# --------------------------------------------
print("Processing target set")
start = 1395691200
end = 1448564400
cur_start = start
cur_end = start+hop*3600
count = 0
bar = pb.ProgressBar(total=(end-start)/3600)
while(cur_end < end-(120+288)*3600):
bar.move()
count += 1
if(count % 100 == 0):
bar.log('Preparing : ' + str(cur_end) + ' till 1448564400')
buff = []
for i in range(hop):
hour = []
f1 = open(data_dir+(str)(cur_start+i*3600), 'rb')
for line in f1.readlines():
ls = line.split('#')
hour = hour+(map(float, ls[4:16]))
f1.close()
buff.append(hour)
data.append(buff)
f1 = open(data_dir+(str)(cur_start+120*3600), 'rb')
for line in f1.readlines():
ls = line.split("#")
target_set.append(map(float, ls[7:10]))
break
cur_start = cur_start+3600
cur_end = cur_end+3600
print(len(target_set))
np_data = np.asarray(data)
np_target = np.asarray(target_set)
print("Target shape :", np_target.shape)
print("Data shape : :", np_data.shape)
X = np_data
y = np_target
training_set = np.array(X[1920:])
training_target = np.array(y[1920:])
val_set = np.array(X[:1920])
val_target = np.array(y[:1920])
sess = tf.InteractiveSession()
batch_size = tf.placeholder(tf.int32)
_X = tf.placeholder(tf.float32, [None, timestep_size, 36]) # TODO change this to the divided ver
y = tf.placeholder(tf.float32, [None, 3])
keep_prob = tf.placeholder(tf.float32)
# --------------------------------------------
# Construct LSTM cells
# --------------------------------------------
lstm_cell = rnn.LSTMCell(num_units=hidden_size,
forget_bias=1.0,
state_is_tuple=True)
# time_major=False)
lstm_cell = rnn.DropoutWrapper(cell=lstm_cell,
input_keep_prob=1.0,
output_keep_prob=keep_prob)
mlstm_cell = rnn.MultiRNNCell([lstm_cell] * layer_num, state_is_tuple=True)
init_state = mlstm_cell.zero_state(batch_size, dtype=tf.float32)
outputs, state = tf.nn.dynamic_rnn(mlstm_cell,
inputs=_X,
initial_state=init_state)
h_state = outputs[:, -1, :] # 或者 h_state = state[-1][1]
# --------------------------------------------
# Convert LSTM output to tensor of three
# --------------------------------------------
W = tf.Variable(tf.truncated_normal([hidden_size, output_parameters],
stddev=0.1),
dtype=tf.float32)
bias = tf.Variable(tf.constant(0.1,shape=[output_parameters]),
dtype=tf.float32)
y_pre = tf.matmul(h_state, W) + bias
cross_entropy = -tf.reduce_mean(y * tf.log(y_pre))
train_op = tf.train.AdamOptimizer(lr).minimize(cross_entropy)
loss = tf.reduce_mean(tf.abs(y_pre-y),0)
correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
# --------------------------------------------
# Start Training
# --------------------------------------------
sess.run(tf.global_variables_initializer())
count = 0
for i in range(training_epochs):
for batch in range(5, 36):
start = batch*_batch_size
end = (batch+1)*_batch_size
sess.run(train_op,
feed_dict={_X:data[start:end],
y: target_set[start:end],
keep_prob: 0.5,
batch_size: 384})
# print("========Iter:"+str(i)+",Accuracy:========",(acc))
if(i%3==0):
acc = sess.run(loss,
feed_dict={_X: data[1152:1536],
y: target_set[1152:1536],
batch_size: 384,
keep_prob: 1})
print("Epoch:"+str(i)+str(acc))