分享

《炼数成金》.第七课 递归神经网络LSTM的讲解,以及LSTM网络的使用学习笔记

 木俊 2018-08-01


炼数成金》.第七课 递归神经网络LSTM的讲解,以及LSTM网络的使用学习笔记

转载自:https://blog.csdn.net/m0_37870649/article/details/79433375
2018年03月03日 22:20:39
阅读数:392

笔记:

       1.递归神经网络和BP神经网络的区别:

                不同点:递归神经网络L有反馈回路,可以记住上一次的输出,并作为下一次的输入之一,BP神经网络没有反馈回路。

                相同点:都有梯度消失的问题,之前输入的数据会随着时间的流逝,信号会不断的衰弱,对决策的影响越来越小。

           使用y=x的激活函数,则不会出现梯度消失问题,但是网络会一直往下传播,重要的信息也记住,不重要的也记住。正确的应该正确的记住,错误的忘记,于是诞生了LSTM网络。

        2.权值和偏置值只设定一对就可以,其他没有设定的,tensorflow会自动为我们设置。

       3.代码调试过程中的错误:

  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) 

       4.最终输出准确率为93%,可以继续优化。

源码
  1. # coding: utf-8

  2. # In[1]:

  3. import tensorflow as tf
  4. from tensorflow.examples.tutorials.mnist import input_data
  5. from tensorflow.contrib import rnn


  6. # In[2]:

  7. #载入数据集
  8. mnist = input_data.read_data_sets("D://MNIST_data",one_hot=True)

  9. # 输入图片是28*28
  10. n_inputs = 28 #输入一行,一行有28个数据
  11. max_time = 28 #一共28行
  12. lstm_size = 100 #隐层单元
  13. n_classes = 10 # 10个分类
  14. batch_size = 50 #每批次50个样本
  15. n_batch = mnist.train.num_examples // batch_size #计算一共有多少个批次

  16. #这里的none表示第一个维度可以是任意的长度
  17. x = tf.placeholder(tf.float32,[None,784])
  18. #正确的标签
  19. y = tf.placeholder(tf.float32,[None,10])

  20. #初始化权值
  21. weights = tf.Variable(tf.truncated_normal([lstm_size, n_classes], stddev=0.1))
  22. #初始化偏置值
  23. biases = tf.Variable(tf.constant(0.1, shape=[n_classes]))


  24. #定义RNN网络
  25. def RNN(X,weights,biases):
  26. # inputs=[batch_size, max_time, n_inputs]
  27. inputs = tf.reshape(X,[-1,max_time,n_inputs])
  28. #定义LSTM基本CELL
  29. #lstm_cell = tf.contrib.rnn.core_rnn_cell.BasicLSTMCell(lstm_size)
  30. lstm_cell = rnn.BasicLSTMCell(lstm_size)
  31. # final_state[0]是cell state
  32. # final_state[1]是hidden_state
  33. outputs,final_state = tf.nn.dynamic_rnn(lstm_cell,inputs,dtype=tf.float32)
  34. results = tf.nn.softmax(tf.matmul(final_state[1],weights) + biases)
  35. return results


  36. #计算RNN的返回结果
  37. prediction= RNN(x, weights, biases)
  38. #损失函数
  39. cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
  40. #使用AdamOptimizer进行优化
  41. train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
  42. #结果存放在一个布尔型列表中
  43. correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
  44. #求准确率
  45. accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))#把correct_prediction变为float32类型
  46. #初始化
  47. init = tf.global_variables_initializer()

  48. with tf.Session() as sess:
  49. sess.run(init)
  50. for epoch in range(6):
  51. for batch in range(n_batch):
  52. batch_xs,batch_ys = mnist.train.next_batch(batch_size)
  53. sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

  54. acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
  55. print ("Iter " + str(epoch) + ", Testing Accuracy= " + str(acc))


  56. # In[ ]:

输出
Extracting D://MNIST_data\train-images-idx3-ubyte.gz
Extracting D://MNIST_data\train-labels-idx1-ubyte.gz
Extracting D://MNIST_data\t10k-images-idx3-ubyte.gz
Extracting D://MNIST_data\t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From <ipython-input-7-89efd89d18dd>:52: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:

Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.

See tf.nn.softmax_cross_entropy_with_logits_v2.

Iter 0, Testing Accuracy= 0.8148
Iter 1, Testing Accuracy= 0.8737
Iter 2, Testing Accuracy= 0.899
Iter 3, Testing Accuracy= 0.9196
Iter 4, Testing Accuracy= 0.9217
Iter 5, Testing Accuracy= 0.9274

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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多