RNN&LSTM用数据集MNIST实现手写字体识别

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cskywit/article/details/79205227

       学习Tensorflow,用RNN来实现MNIST手写数字识别,根据手绘的网络结构实现,网络如下:


    因为Tensorflow还不熟悉,使用Jupyter Notebook输代码,如下:

input_data.py 


 
 
  1. #用于下载MNIST数据集和加载数据
  2. from __future__ import absolute_import
  3. from __future__ import division
  4. from __future__ import print_function
  5. import gzip
  6. import os
  7. import tempfile
  8. import numpy
  9. from six.moves import urllib
  10. from six.moves import xrange # pylint: disable=redefined-builtin
  11. import tensorflow as tf
  12. from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
RNN.py


 
 
  1. import tensorflow as tf
  2. from tensorflow.contrib import rnn
  3. import input_data
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6. print( "Packages imported")
  7. mnist = input_data.read_data_sets( "data/",one_hot= True)
  8. trainimg,trainlabels,testimg,testlabels \
  9. = mnist.train.images,mnist.train.labels,mnist.test.images,mnist.test.labels
  10. ntrain,ntest,dim,nclasses \
  11. = trainimg.shape[ 0],testimg.shape[ 0],trainimg.shape[ 1],trainlabels.shape[ 1]
  12. print( "MNIST loaded")

 
 
  1. diminput = 28
  2. dimhidden = 128
  3. dimoutput = nclasses
  4. #将28*28的图像切分为1*28的28个,分别输入不同的RNN单元
  5. nsteps = 28
  6. #初始化权重和偏置
  7. weights = {
  8.     'hidden':tf.Variable(tf.random_normal([diminput,dimhidden])),
  9.     'out':tf.Variable(tf.random_normal([dimhidden,dimoutput]))
  10. }
  11. biases = {
  12.     'hidden':tf.Variable(tf.random_normal([dimhidden])),
  13.     'out': tf.Variable(tf.random_normal([dimoutput]))
  14. }

 
 
  1. def _RNN(_X,_W,_b,_nsteps,_name):
  2. #第一步:转换输入,输入_X是还有batchSize=5的5张28*28图片,需要将输入从
  3. #[batchSize,nsteps,diminput]==>[nsteps,batchSize,diminput]
  4.     _X = tf.transpose(_X,[ 1, 0, 2])
  5. #第二步:reshape _X为[nsteps*batchSize,diminput]
  6.     _X = tf.reshape(_X,[ -1,diminput])
  7. #第三步:input layer -> hidden layer
  8.     _H = tf.matmul(_X,_W[ 'hidden'])+_b[ 'hidden']
  9. #第四步:将数据切分为‘nsteps’个切片,第i个切片为第i个batch data
  10.     #tensoflow >0.12
  11.     _Hsplit = tf.split(_H,_nsteps, 0)
  12.     #tensoflow <0.12  _Hsplit = tf.split(0,_nsteps,_H)
  13. #第五步:计算LSTM final output(_LSTM_O) 和 state(_LSTM_S)
  14. #_LSTM_O和_LSTM_S都有‘batchSize’个元素
  15. #_LSTM_O用于预测输出
  16.     with tf.variable_scope(_name) as scope:
  17.         #scope.reuse_variables()
  18.     # forget_bias = 1.0不忘记数据   
  19.     ###tensorflow <1.0
  20.         # lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(dimhidden,forget_bias = 1.0)
  21.         #_LSTM_O,_SLTM_S = tf.nn.rnn(lstm_cell,_Hsplit,dtype=tf.float32)
  22.     ###tensorflow 1.0
  23.         lstm_cell = rnn.BasicLSTMCell(dimhidden)
  24.         _LSTM_O,_LSTM_S = rnn.static_rnn(lstm_cell,_Hsplit,dtype=tf.float32)
  25. #第六步:输出,需要最后一个RNN单元作为预测输出所以取_LSTM_O[-1]
  26.         _O = tf.matmul(_LSTM_O[ -1],_W[ 'out'])+_b[ 'out']
  27.     return {
  28.         'X':_X,
  29.         'H':_H,
  30.         '_Hsplit':_Hsplit,
  31.         'LSTM_O':_LSTM_O,
  32.         'LSTM_S':_LSTM_S,
  33.         'O':_O
  34. }
  35. print( "Network Ready!")

 
 
  1. learning_rate = 0.001
  2. x = tf.placeholder( "float",[ None,nsteps,diminput])
  3. y = tf.placeholder( "float",[ None,dimoutput])
  4. myrnn = _RNN(x,weights,biases,nsteps, 'basic')
  5. pred = myrnn[ 'O']
  6. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))
  7. optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) #Adam
  8. accr = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(pred, 1),tf.argmax(y, 1)),tf.float32))
  9. init = tf.global_variables_initializer()
  10. print( "Network Ready!")

 
 
  1. training_epochs = 5
  2. batch_size      = 16
  3. display_step    = 1
  4. sess = tf.Session()
  5. sess.run(init)
  6. print ( "Start optimization")
  7. for epoch in range(training_epochs):
  8.     avg_cost = 0.
  9.     #total_batch = int(mnist.train.num_examples/batch_size)
  10.     total_batch = 100
  11.     # Loop over all batches
  12.     for i in range(total_batch):
  13.         batch_xs, batch_ys = mnist.train.next_batch(batch_size)
  14.         batch_xs = batch_xs.reshape((batch_size, nsteps, diminput))
  15.         #print(batch_xs.shape)
  16.         #print(batch_ys.shape)
  17.         #batch_ys = batch_ys.reshape((batch_size, dimoutput))
  18.         # Fit training using batch data
  19.         feeds = {x: batch_xs, y: batch_ys}
  20.         sess.run(optm, feed_dict=feeds)
  21.         # Compute average loss
  22.         avg_cost += sess.run(cost, feed_dict=feeds)/total_batch
  23.     # Display logs per epoch step
  24.     if epoch % display_step == 0
  25.         print ( "Epoch: %03d/%03d cost: %.9f" % (epoch, training_epochs, avg_cost))
  26.         feeds = {x: batch_xs, y: batch_ys}
  27.         train_acc = sess.run(accr, feed_dict=feeds)
  28.         print ( " Training accuracy: %.3f" % (train_acc))
  29.         testimgs = testimg.reshape((ntest, nsteps, diminput))
  30.         feeds = {x: testimgs, y: testlabels}
  31.         test_acc = sess.run(accr, feed_dict=feeds)
  32.         print ( " Test accuracy: %.3f" % (test_acc))
  33. print ( "Optimization Finished.")
结果如下,准确率不高:


 
 
  1. Start optimization
  2. Epoch: 000/ 005 cost: 1.733600584
  3. Training accuracy: 0.375
  4. Test accuracy: 0.446
  5. Epoch: 001/ 005 cost: 1.268029318
  6. Training accuracy: 0.438
  7. Test accuracy: 0.490
  8. Epoch: 002/ 005 cost: 1.102416685
  9. Training accuracy: 0.688
  10. Test accuracy: 0.574
  11. Epoch: 003/ 005 cost: 0.950877853
  12. Training accuracy: 0.562
  13. Test accuracy: 0.641
  14. Epoch: 004/ 005 cost: 0.829133632
  15. Training accuracy: 0.875
  16. Test accuracy: 0.655
  17. Optimization Finished.




评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值