-
Notifications
You must be signed in to change notification settings - Fork 87
/
run_tf_basic_rnn_seq2vec_trainer.py
164 lines (109 loc) · 4.7 KB
/
run_tf_basic_rnn_seq2vec_trainer.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
#-*- coding: utf-8 -*-
#! /usr/bin/env python
'''
filename: run_tf_basic_rnn_sqe2vec_trainer.py
This script is for predicting mnist images
author: Jaewook Kang @ 2018 Sep
'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from datetime import datetime
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
import numpy as np
model_config = \
{
'batch_size': None,
'n_input' : 28,
'num_steps': 28,
'n_neurons': 150,
'n_output' : 10,
'dtype' : tf.float32
}
training_config = \
{
'learning_rate': 0.001,
'n_epochs':10,
'batch_size':150
}
def get_rnn_dynamic_model(X,scope):
with tf.name_scope(name=scope,values=[X]):
# X : 28 x 1
basic_cell = tf.nn.rnn_cell.BasicRNNCell(num_units=model_config['n_neurons'],
name='basic_rnn_cell')
# states : 150 x 1
outputs, states = tf.nn.dynamic_rnn(cell=basic_cell,
inputs=X,
dtype=model_config['dtype'])
# logits: 10 x 1
logits = tf.layers.dense(states,model_config['n_output'])
# logits = tf.layers.dense(outputs[:,-1],model_config['n_output'])
return logits
if __name__ == '__main__':
# dataset preparation
mnist = input_data.read_data_sets("/tmp/data/")
input_shape = [model_config['batch_size'],
model_config['num_steps'],
model_config['n_input']]
output_shape = [model_config['batch_size'],
model_config['num_steps'],
model_config['n_output']]
X = tf.placeholder(dtype = model_config['dtype'],
shape = input_shape,
name = 'X')
Y =tf.placeholder(dtype=tf.int32,
shape=[None])
# build model
scope = 'rnn_seq2vec_model'
logits = get_rnn_dynamic_model(X,scope)
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=Y,
logits=logits))
optimizer = tf.train.AdamOptimizer(learning_rate=training_config['learning_rate'])
training_op = optimizer.minimize(loss)
correct = tf.nn.in_top_k(logits,Y,1)
accuracy = tf.reduce_mean(tf.cast(correct,tf.float32))
# tensorboard summary
now = datetime.utcnow().strftime("%Y%m%d%H%M%S")
root_logdir = 'tf_logs/rnn_basic_seq2vec_trainer'
subdir = "{}/run-{}/".format(root_logdir, now)
logdir = './pb_and_ckpt/' + subdir
if not tf.gfile.Exists(logdir):
tf.gfile.MakeDirs(logdir)
summary_writer = tf.summary.FileWriter(logdir=logdir)
summary_writer.add_graph(graph=tf.get_default_graph())
init = tf.global_variables_initializer()
n_epochs = training_config['n_epochs']
batch_size = training_config['batch_size']
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
x_test = mnist.test.images.reshape((-1,
model_config['num_steps'],
model_config['n_input']))
y_test = mnist.test.labels
for iteration in range(mnist.train.num_examples // batch_size):
x_batch, y_batch = mnist.train.next_batch(batch_size)
x_batch = x_batch.reshape((-1,
model_config['num_steps'],
model_config['n_input']))
sess.run(training_op,feed_dict={X:x_batch,
Y:y_batch})
acc_train = accuracy.eval(feed_dict={X:x_batch,
Y:y_batch})
acc_test = accuracy.eval(feed_dict={X:x_test,
Y:y_test})
print(epoch,"Train accuracy:", acc_train, "Test accuracy:", acc_test)
x_true,y_true = mnist.train.next_batch(1)
x_true = x_true.reshape((-1,
model_config['num_steps'],
model_config['n_input']))
y_pred = sess.run([logits], feed_dict={X:x_true})
x_true = x_true.reshape((model_config['num_steps'],
model_config['n_input']))
plt.figure(1)
imgplot = plt.imshow(x_true)
plt.title('true = %s, pred = %s' %(y_true[0],np.argmax(y_pred)))
plt.show()
summary_writer.close()