分享

rnn-mnist 没有注释iter0, testing accuracy=0.9774 iter1, testing accuracy=0.9831 iter2, testing accuracy=0.984 iter3, testing accuracy=0.984 iter4, testing accuracy=0.9843 iter5, testing accuracy=0.985

 木俊 2018-08-01
代码调试过程中的错误:
  1. <ipython-input-8-a4f3de4a35a8> in RNN(X, weights, biases)
  2. 38 inputs = tf.reshape(X,[-1,max_time,n_inputs])
  3. 39 #定义LSTM基本CELL
  4. ---> 40 lstm_cell = tf.contrib.rnn.core_rnn_cell.BasicLSTMCell(lstm_size)
  5. 41 #lstm_cell = rnn.BasicLSTMCell(lstm_size)
  6. 42 # final_state[0]是cell state

  7. AttributeError: module 'tensorflow.contrib.rnn' has no attribute 'core_rnn_cell'

     解决:

           #1.0版本改了很多
            #原代码是这样的:
            lstm_cell = tf.contrib.rnn.core_rnn_cell.BasicLSTMCell(lstm_size)
            #应该改为:
            from tensorflow.contrib import rnn

            lstm_cell = rnn.BasicLSTMCell(lstm_size)


import
tensorflow as tf
from tensorflow.contrib import rnn
from tensorflow.examples.tutorials.mnist import input_data
#载入数据
mnist = input_data.read_data_sets("/home/mj/MINIST_data", one_hot=True)
#输入图片是28*28
n_inputs=28#
max_time=28
lstm_size=100
n_classes=10
batch_size=50
n_batch=mnist.train.num_examples
x=tf.placeholder(tf.float32,[None,784])
y=tf.placeholder(tf.float32,[None,10])

weights=tf.Variable(tf.truncated_normal([lstm_size,n_classes],stddev=0.1))

biases=tf.Variable(tf.constant(0.1,shape=[n_classes]))

def Rnn(X,weights,biases):
inputs=tf.reshape(X,[-1,max_time,n_inputs])
#lstm_cell=tf.contrib.rnn.core_rnn_cell.BasicLSTMCell(lstm_size)
lstm_cell = rnn.BasicLSTMCell(lstm_size)
outputs,final_state=tf.nn.dynamic_rnn(lstm_cell,inputs,dtype=tf.float32)
results=tf.nn.softmax(tf.matmul(final_state[1],weights)+biases)
return results

prediction=Rnn(x,weights,biases)
cross_entropy=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction,labels=y))
train_step=tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
init=tf.global_variables_initializer()

with tf.Session() as sess:
sess.run(init)
for epoch in range(6):
for batch in range(n_batch):
batch_xs,batch_ys=mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("iter"+str(epoch)+", testing accuracy="+str(acc))

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多