tensorflow——用RNN实现MNIST手写数字识别

本文介绍了使用TensorFlow通过RNN实现MNIST手写数字识别的过程。在解决数据类型不一致的问题时,作者提供了修改方案,将数据强制转换为float类型。

RNN:在NLP中使用最为广泛,比如:可以用前面的内容推测出下一个单词

RNN简介


tensorflow版本:1.3.0
参考斗大的熊猫的代码,因为版本原因,出了好多小差错
万幸都解决了。

ERROR ①:
TypeError: Input 'split_dim' of 'Split' Op has type float32 that does not match expected type of int32.
将data=tf.split(0,chunk_n,data)改为data=tf.split(data,chunk_n,0)

ERROR ②:

下面两个版本都是对的!!!(真奇怪,下午跑第一个版本就出错了)

版本一:
lstm_cell = rnn.BasicLSTMCell(rnn_size)
outputs, status = rnn.static_rnn(lstm_cell, data, dtype=tf.float32)

版本二:

    with tf.variable_scope('cell_def'):
        lstm_cell=rnn.BasicLSTMCell(rnn_size)
        #status=lstm_cell.zero_state(data,dtype=tf.float32)
    with tf.variable_scope('lstm_def',reuse=True):
        outputs,status=rnn.static_rnn(lstm_cell,data,dtype=tf.float32)
ERROR ③ 
ValueError: Variable rnn/basic_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:


output=tf.add(tf.matmul(outputs[-1], layer['w_']),tf.float32, layer['b_'],tf.float32)

换为

 output=tf.add(tf.cast(tf.matmul(outputs[-1], layer['w_']),tf.float32), tf.cast(layer['b_'],tf.float32))

这是因为add的数据类型不一致,干脆强制把两个参数都换为float


下面是完整的代码
import tensorflow as tf
import numpy as np

from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib import rnn

mnist=input_data.read_data_sets('./MNIST',one_hot=True)


chunk_size=28
chunk_n=28

rnn_size=256

n_output_layer=10

X=tf.placeholder('float',[None,chunk_n,chunk_size])
Y=tf.placeholder('float')
batch_size=100
def recurrent_neural_network(data):
    layer={'w_':tf.Variable(tf.random_normal([rnn_size,n_output_layer])),'b_':tf.Variable(n_output_layer)}
    data=tf.transpose(data,[1,0,2])
    data=tf.reshape(data,[-1,chunk_size])
    data=tf.split(data,chunk_n,0)
    '''
    with tf.variable_scope('cell_def',reuse=None):
        lstm_cell=rnn.BasicLSTMCell(rnn_size)
        #status=lstm_cell.zero_state(data,dtype=tf.float32)
    with tf.variable_scope('lstm_def',reuse=True):
        outputs,status=rnn.static_rnn(lstm_cell,data,dtype=tf.float32)
    '''
    lstm_cell = rnn.BasicLSTMCell(rnn_size)
    outputs, status = rnn.static_rnn(lstm_cell, data, dtype=tf.float32)
    
    output=tf.add(tf.cast(tf.matmul(outputs[-1], layer['w_']),tf.float32), tf.cast(layer['b_'],tf.float32))
    #output=tf.add(tf.matmul(outputs[-1], layer['w_']),tf.float32, layer['b_'],tf.float32)
    return output

def train_neural_network(X,Y):
    predict=recurrent_neural_network(X)
    cost_func=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predict,labels=Y))
    optimizer=tf.train.AdamOptimizer().minimize(cost_func)
    
    epochs=13
    with tf.Session() as session:
        session.run(tf.initialize_all_variables())
        epoch_loss=0
        for epoch in range(epochs):
            for i in range(int(mnist.train.num_examples/batch_size)):
                x,y=mnist.train.next_batch(batch_size)
                x=x.reshape([batch_size,chunk_n,chunk_size])
                _,c=session.run([optimizer,cost_func],feed_dict={X:x,Y:y})
                epoch_loss+=c
            print(epoch,':',epoch_loss)
            
        correct=tf.equal(tf.argmax(predict,1),tf.argmax(Y,1))
        accuracy=tf.reduce_mean(tf.cast(correct,'float'))
        print('准确率:',accuracy.eval({X:mnist.test.images.reshape(-1,chunk_n,chunk_size),Y:mnist.test.labels}))
        
train_neural_network(X,Y)




结果:

0 : 202.30009520053864
1 : 265.4562122412026
2 : 308.5539209987037
3 : 342.8234854093753
4 : 369.15573160978965
5 : 391.20981509785634
6 : 411.74149958149064
7 : 427.5524849923677
8 : 442.9537744842237
9 : 456.21875667283894
10 : 468.54793797503226
11 : 479.14257012763846
12 : 489.4170361435754
准确率: 0.9869001


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值