-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
94 lines (65 loc) · 2.09 KB
/
main.py
File metadata and controls
94 lines (65 loc) · 2.09 KB
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
# imports
from config import BATCH_SIZE
from config import EPOCHS
from config import LEARNING_RATE
from config import MAX_LENGTH
from data import get_batch
from six.moves import xrange
import numpy as np
import tensorflow as tf
from tensorflow.contrib.legacy_seq2seq import basic_rnn_seq2seq
from tensorflow.contrib.rnn import BasicLSTMCell
# functions
def build_model(X, y):
y_, _ = basic_rnn_seq2seq(X, y, BasicLSTMCell(4))
return y_
def get_loss(y, y_):
reshaped_outputs = tf.reshape(y_, [-1])
reshaped_results = tf.reshape(y, [-1])
loss = tf.losses.mean_squared_error(reshaped_outputs, reshaped_results)
return loss
def get_series(batch, series_idx):
series = [batch[idx][series_idx]
for idx in xrange(MAX_LENGTH)]
return np.array(series)
def convert_prediction(pred, series_idx):
series = get_series(pred, series_idx)
converted_series = []
for value in series:
converted_value = np.zeros(len(value), dtype=np.int32)
converted_value[value.argmax()] = 1
converted_series.append(converted_value)
return np.array(converted_series)
def main():
X = [tf.placeholder('float', [BATCH_SIZE, 4])
for _ in xrange(MAX_LENGTH)]
y = [tf.placeholder('float', [BATCH_SIZE, 4])
for _ in xrange(MAX_LENGTH)]
y_ = build_model(X, y)
loss = get_loss(y, y_)
optimizer = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE)
train_operation = optimizer.minimize(loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in xrange(EPOCHS):
X_batch, y_batch = get_batch()
X_list = dict(zip(X, X_batch))
y_list = dict(zip(y, y_batch))
merged_dict = {**X_list, **y_list}
loss_val, pred, _ = sess.run([loss, y_, train_operation],
feed_dict=merged_dict)
if (epoch + 1) % 1000 == 0:
print('epoch: {}, loss: {}'.format(epoch + 1, loss_val))
print('Input:')
print(get_series(X_batch, 0))
print()
print('Ground truth:')
print(get_series(y_batch, 0))
print()
print('Prediction:')
print(convert_prediction(pred, 0))
print()
print()
if __name__ == '__main__':
main()