-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrnn_mnist_classification.py
65 lines (41 loc) · 1.85 KB
/
brnn_mnist_classification.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
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# this is data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
lr = 0.001
training_iters = 100000
batch_size = 128
n_inputs = 28 # image shape:28 * 28
n_steps = 28 # time steps
n_hidden_units = 256
n_classes = 10
x = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None, n_classes])
weights = tf.Variable(tf.random_normal([n_hidden_units*2, n_classes]))
biases = tf.Variable(tf.random_normal([n_classes]))
def BiRNN(X, weights, biases):
X = tf.transpose(X, [1, 0, 2])
X = tf.reshape(X, [-1, n_inputs])
X = tf.split(X, n_steps)
lstm_fw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units)
lstm_bw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units)
outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, X, dtype=tf.float32)
# hidden layer for outputs as the final results
results = tf.matmul(outputs[-1], weights) + biases
return results
pred = BiRNN(x, weights, biases)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=pred))
train_op = tf.train.AdamOptimizer(lr).minimize(cost)
correct_pred = tf.equal(tf.arg_max(pred, 1), tf.arg_max(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
step = 0
while step * batch_size < training_iters:
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
batch_xs = batch_xs.reshape([batch_size, n_steps, n_inputs])
sess.run([train_op], feed_dict={x:batch_xs, y:batch_ys})
if step % 20 == 0:
print(sess.run(accuracy, feed_dict={x:batch_xs, y:batch_ys}))
step += 1