coding=utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import gzip
import os
import sys
import time
import numpy as np
from six.moves import urllib
from six.moves import xrange
import tensorflow as tf
from tensorflow.python.ops import rnn, rnn_cell
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
# 載入數(shù)據(jù)
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
# 把每張圖片看作一個(gè)序列,28個(gè)元素,每個(gè)元素28個(gè)點(diǎn)
# Parameters
learning_rate = 0.001
training_iters = 100000
batch_size = 128
display_step = 10
# 每行看成上一行的序列
n_input = 28 # MNIST data input (img shape: 28*28)
n_steps = 28 # timesteps
# n_hidden個(gè)lstm單元
n_hidden = 128
# 最終分類(lèi)到0~9數(shù)字上
n_classes = 10 # MNIST total classes (0-9 digits)
# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
# 從n_hidden個(gè)lstm到n_classes個(gè)最后一層神經(jīng)元的權(quán)重
weights = {
'out': tf.Variable(tf.random_normal([n_hidden, n_classes]))
}
biases = {
'out': tf.Variable(tf.random_normal([n_classes]))
}
def RNN(x, weights, biases):
# 為rnn作數(shù)據(jù)轉(zhuǎn)換
# 輸入數(shù)據(jù)是: (batch_size, n_steps, n_input)
# 需要轉(zhuǎn)換為: 'n_steps' 個(gè)矩陣 (batch_size, n_input)
# 把n_steps和batch_size交換下,矩陣變成[n_steps,batch_size,n_input]
x = tf.transpose(x, [1, 0, 2])
# 再變成 (n_steps*batch_size, n_input)
x = tf.reshape(x, [-1, n_input])
# 拆成n_steps個(gè) (batch_size, n_input)
x = tf.split(0, n_steps, x)
# n_hidden個(gè)lstm單元
lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0)
# 計(jì)算
outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)
# 取最后一個(gè)output,rnn后再走一層網(wǎng)絡(luò)
return tf.matmul(outputs[-1], weights['out']) + biases['out']
pred = RNN(x, weights, biases)
# loss函數(shù),優(yōu)化方法
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# 準(zhǔn)確率評(píng)估
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
step = 1
while step * batch_size < training_iters:
batch_x, batch_y = mnist.train.next_batch(batch_size)
# Reshape data to get 28 seq of 28 elements
batch_x = batch_x.reshape((batch_size, n_steps, n_input))
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if step % display_step == 0:
# 計(jì)算準(zhǔn)確率和loss
acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})
loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
print ("Iter" + str(step*batch_size) + ", Minibatch Loss= " + \
"{:.6f}".format(loss) + ", Training Accuracy= " + \
"{:.5f}".format(acc))
step += 1
print ("Optimization Finished!")
# 用測(cè)試數(shù)據(jù)再評(píng)估下
test_len = 128
test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input))
test_label = mnist.test.labels[:test_len]
print ("Testing Accuracy:", \
sess.run(accuracy, feed_dict={x: test_data, y: test_label}))
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。