# -*- coding:utf-8 -*-
import sys
import importlib
importlib.reload(sys)
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
# 加載數(shù)據(jù)
mnist = input_data.read_data_sets("./", one_hot=True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels
trX = trX.reshape(-1, 28, 28, 1)? # 28x28x1 input img
teX = teX.reshape(-1, 28, 28, 1)? # 28x28x1 input img
# 設(shè)置訓練的超參數(shù)
lr = 0.001
training_iters = 100000
batch_size = 128
# 神經(jīng)網(wǎng)絡的參數(shù)
n_inputs = 28? ? # 輸入層的n
n_steps = 28? ? # 28長度
n_hidden_units = 128? ? # 隱藏層的神經(jīng)元個數(shù)
n_classes = 10? # 輸出的數(shù)量,即分類的類別忠蝗,0~9個數(shù)字现横,共有10個
# 輸入數(shù)據(jù)占位符
x = tf.placeholder("float", [None, n_steps, n_inputs])
y = tf.placeholder("float", [None, n_classes])
# 定義權(quán)重
weights = {
'in': tf.Variable(tf.random_normal([n_inputs, n_hidden_units])),
'out': tf.Variable(tf.random_normal([n_hidden_units, n_classes]))
}
biases = {
'in': tf.Variable(tf.constant(0.1, shape=[n_hidden_units, ])),
'out': tf.Variable(tf.constant(0.1, shape=[n_classes, ]))
}
#定義RNN模型
def RNN(X, weights, biases):
X = tf.reshape(X, [-1, n_inputs])? #把輸入的X轉(zhuǎn)換成X ==》(128 batch * 28 steps, 28 inputs)
# 進入隱藏層
X_in = tf.matmul(X, weights['in']) + biases['in']? # (128 batch * 28 steps, 128 hidden)
X_in = tf.reshape(X_in, [-1, n_steps, n_hidden_units])? # 128 batch , 28 steps, 128 hidden
# 這里采用基本的LSTM循環(huán)網(wǎng)絡單元:basic LSTM Cell
lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units, forget_bias=1.0, state_is_tuple=True)
init_state = lstm_cell.zero_state(batch_size, dtype=tf.float32) #lstm單元由兩個部分組成:(c_state, h_state)
# dynamic_rnn接收張量(batch, steps, inputs)或者(steps, batch, inputs)作為X_in
outputs, final_state = tf.nn.dynamic_rnn(lstm_cell, X_in, initial_state=init_state, time_major=False)
results = tf.matmul(final_state[1], weights['out']) + biases['out']
return results
# 定義損失函數(shù)和優(yōu)化器,優(yōu)化器采用AdamOptimizer
pred = RNN(x, weights, biases)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
train_op = tf.train.AdamOptimizer(lr).minimize(cost)
# 定義模型預測結(jié)果及準確率計算方法
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# 訓練數(shù)據(jù)及評估模型
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
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
0.2578125
0.671875
0.7578125
0.8203125
0.8984375
0.9296875
0.9140625
0.90625
0.875
0.9296875
0.9453125
0.9296875
0.9609375
0.921875
0.9296875
0.9609375
0.9453125
0.890625
0.9296875
0.9375
0.953125
0.9765625
0.9375
0.9375
0.9609375
0.9609375
0.96875
0.9609375
0.96875
0.9609375
0.96875
0.9453125
0.9609375
0.9921875
0.9765625
0.9765625
0.9765625
0.96875
0.953125
0.9765625