Hello Mat

 找回密码
 立即注册
查看: 5021|回复: 0

RNN之bi-LSTM双向递归神经网络

[复制链接]

1323

主题

1551

帖子

0

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22647
发表于 2017-9-14 23:31:13 | 显示全部楼层 |阅读模式
RNN之bi-LSTM递归神经网络
百度网盘代码分享:http://pan.baidu.com/s/1eRQcCuu
电脑:Win7旗舰版+64Bit+AMD Athlon(tm)X2 DualCore QL-64 2.10GHz   RAM2.75GB
Anaconda3-4.2.0-Windows-x86_64
  1. import time
  2. #from tensorflow.examples.tutorials.mnist import input_data
  3. import tensorflow as tf
  4. import Get_Mnist_Data

  5. start=time.clock()
  6. #mnist = input_data.read_data_sets('/temp/', one_hot=True)
  7. mnist = Get_Mnist_Data.read_data_sets('Get_Mnist_Data', one_hot=True)
  8. end=time.clock()  
  9. print('Runing time = %s Seconds'%(end-start))

  10. def compute_accuracy(v_x, v_y):
  11.     global pred
  12.     #input v_x to nn and get the result with y_pre
  13.     y_pre = sess.run(pred, feed_dict={x:v_x})
  14.     #find how many right
  15.     correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_y,1))
  16.     #calculate average
  17.     accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  18.     #get input content
  19.     result = sess.run(accuracy,feed_dict={x: v_x, y: v_y})
  20.     return result

  21. def Bi_lstm(X):
  22.     lstm_f_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units, forget_bias=1.0, state_is_tuple=True)
  23.     lstm_b_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units, forget_bias=1.0, state_is_tuple=True)
  24.     return tf.contrib.rnn.static_bidirectional_rnn(lstm_f_cell, lstm_b_cell, X, dtype=tf.float32)

  25. def RNN(X,weights,biases):
  26.     # hidden layer for input
  27.     X = tf.reshape(X, [-1, n_inputs])
  28.     X_in = tf.matmul(X, weights['in']) + biases['in']

  29.     #reshape data put into bi-lstm cell
  30.     X_in = tf.reshape(X_in, [-1,n_steps, n_hidden_units])
  31.     X_in = tf.transpose(X_in, [1,0,2])
  32.     X_in = tf.reshape(X_in, [-1, n_hidden_units])
  33.     X_in = tf.split(X_in, n_steps)
  34.     outputs, _, _ = Bi_lstm(X_in)
  35.    
  36.     #hidden layer for output as the final results
  37.     results = tf.matmul(outputs[-1], weights['out']) + biases['out']

  38.     return results
  39.    

  40. #load mnist data
  41. #mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

  42. # parameters init
  43. l_r = 0.001
  44. training_iters = 100
  45. batch_size = 128

  46. n_inputs = 28
  47. n_steps = 28
  48. n_hidden_units = 128
  49. n_classes = 10

  50. #define placeholder for input
  51. x = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
  52. y = tf.placeholder(tf.float32, [None, n_classes])

  53. # define w and b
  54. weights = {
  55.     'in': tf.Variable(tf.random_normal([n_inputs,n_hidden_units])),
  56.     'out': tf.Variable(tf.random_normal([2*n_hidden_units,n_classes]))
  57. }
  58. biases = {
  59.     'in': tf.Variable(tf.constant(0.1,shape=[n_hidden_units,])),
  60.     'out': tf.Variable(tf.constant(0.1,shape=[n_classes,]))
  61. }

  62. pred = RNN(x, weights, biases)
  63. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))
  64. train_op = tf.train.AdamOptimizer(l_r).minimize(cost)

  65. correct_pred = tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
  66. accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

  67. #init session
  68. sess = tf.Session()
  69. #init all variables
  70. sess.run(tf.global_variables_initializer())
  71. #start training

  72. # x_image,x_label = mnist.test.next_batch(500)
  73. # x_image = x_image.reshape([500, n_steps, n_inputs])

  74. for i in range(training_iters):
  75.     #get batch to learn easily
  76.     batch_x, batch_y = mnist.train.next_batch(batch_size)
  77.     batch_x = batch_x.reshape([batch_size, n_steps, n_inputs])
  78.     sess.run(train_op,feed_dict={x: batch_x, y: batch_y})
  79.     if i % 20 == 0:
  80.         print(sess.run(accuracy,feed_dict={x: batch_x, y: batch_y,}))
  81. test_data = mnist.test.images.reshape([-1, n_steps, n_inputs])
  82. test_label = mnist.test.labels
  83. #print("Testing Accuracy:", sess.run(accuracy, feed_dict={x: test_data, y: test_label}))
  84. print("Testing Accuracy: ", compute_accuracy(test_data, test_label))
  85. sess.close()
复制代码
原理分析:参考RNN之LSTM递归神经网络

   以arriveWuhanon三个单词为例,分别设为x1x2x3
        LSTM网络的做法1是:y1x1决定,y2a1x2决定,a1x1得到,y3a2x3决定,a2x1x2得到。
   针对arriveWuhanon三个单词例子,我们也可以反过来考虑。
        LSTM网络的做法2是:y3x3决定,y2a3x2决定,a3x3得到,y1a2x1决定,a2x2x3得到。
        LSTM网络的做法1LSTM网络的做法2好像都有道理,那么将LSTM网络的做法1LSTM网络的做法2结合起来,就引入了Bi-LSTM双向LSTM网络。具体如图所示。

Bi-LSTM神经网络







本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-11-22 23:16 , Processed in 0.213252 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表