循环神经网络系列Tensorflow中dynamic_rnn

最是人间留不住,朱颜辞镜花辞树。这篇文章主要讲述循环神经网络系列Tensorflow中dynamic_rnn相关的知识,希望能为你提供帮助。


1.回顾
上一篇博文(??循环神经网络系列(一)Tensorflow中BasicRNNCell??)中我们介绍了在Tensoflow中,每个RNN单元的实现,以及对应各个参数的含义。自那之后,我们就能通过Tensorflow实现一个单元的计算了。

import tensorflow as tf
import numpy as np

x = np.array([[1, 0, 1, 2], [2, 1, 1, 1]])
X = tf.placeholder(dtype=tf.float32, shape=[2, 4], name=input)
cell = tf.nn.rnn_cell.BasicRNNCell(num_units=5)# output_size:10,也可以换成GRUCell,LSTMAACell,BasicRNNCell
h0 = cell.zero_state(batch_size=2, dtype=tf.float32)# batch_size:2
output, h1 = cell.call(X, h0)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
a, b = (sess.run([output, h1], feed_dict=X: x))
print(output:)
print(a)
print(h1:)
print(b)

> >

output:
[[ 0.44950040.95734160.60139330.75571895 -0.8172958 ]
[ 0.66248890.70114810.687713560.77796507 -0.7617092 ]]
h1:
[[ 0.44950040.95734160.60139330.75571895 -0.8172958 ]
[ 0.66248890.70114810.687713560.77796507 -0.7617092 ]]

通过以上的代码,我们完成了如下操作:
循环神经网络系列Tensorflow中dynamic_rnn

文章图片

但是通常情况下,我们都是要进行这样的操作:
循环神经网络系列Tensorflow中dynamic_rnn

文章图片

输入h0,x1h_0,x_1h0,x1得到output1,h1output_1,h_1output1,h1;然后输入h1,x2h_1,x_2h1,x2得到output2,h2output_2,h_2output2,h2;接着再输入h2,x3h_2,x_3h2,x3得到output3,h3output_3,h_3output3,h3以此类推。那么如何通过Tensorflow一步实现呢?
2. dynamic_rnn
为了实现一步计算多次,我们就要用到Tensorflow中的dynamic_rnn(),代码如下(实现了上图列出的三步
):
import tensorflow as tf
import numpy as np
from tensorflow.python.ops import variable_scope as vs

output_size = 5
batch_size = 4
time_step = 3
dim = 3
cell = tf.nn.rnn_cell.BasicRNNCell(num_units=output_size)
inputs = tf.placeholder(dtype=tf.float32, shape=[time_step, batch_size, dim])
h0 = cell.zero_state(batch_size=batch_size, dtype=tf.float32)
X = np.array([[[1, 2, 1], [2, 0, 0], [2, 1, 0], [1, 1, 0]],# x1
[[1, 2, 1], [2, 0, 0], [2, 1, 0], [1, 1, 0]],# x2
[[1, 2, 1], [2, 0, 0], [2, 1, 0], [1, 1, 0]]])# x3
outputs, final_state = tf.nn.dynamic_rnn(cell, inputs, initial_state=h0, time_major=True)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
a, b = sess.run([outputs, final_state], feed_dict=inputs:X)
print(a)
print(b)

其中第7行time_step=3就表示计算三步,所以输入X就对应有三个部分。再最终的输出结果中,outputs里包含了(outputs1,outputs2,outputs3)outputs_1,outputs_2,outputs_3)outputs1,outputs2,outputs3),而final_stat就只是h3h_3h3,并且outputs3,h3outputs_3,h_3outputs3,h3是相等的。
结果:
outputs:
[[[ 0.9427065-0.92617476 -0.791798530.63080350.07298201]
[ 0.7051633-0.62077284 -0.796183170.5004738-0.20110159]
[ 0.85066974 -0.77197933 -0.768758830.80251306 -0.04951192]
[ 0.67497337 -0.57974416 -0.44081070.680831970.05233984]]# output1

[[ 0.9828192-0.9433205-0.92337510.72930676 -0.34445292]
[ 0.92153275 -0.58029604 -0.89497430.5431045-0.46945637]
[ 0.9690989-0.7922626-0.89737580.81312704 -0.46288016]
[ 0.88565385 -0.6617377-0.680759430.70066273 -0.34827012]]# output2

[[ 0.99172366 -0.93298715 -0.92729050.7158564-0.46278387]
[ 0.9566409-0.5595625-0.91014790.58005375 -0.5905321 ]
[ 0.9838727-0.7693646-0.910197560.82892674 -0.58026373]
[ 0.9438508-0.61732507 -0.73560220.73460865 -0.483655]]]# output3
final_state:
[[ 0.99172366 -0.93298715 -0.92729050.7158564-0.46278387]
[ 0.9566409-0.5595625-0.91014790.58005375 -0.5905321 ]
[ 0.9838727-0.7693646-0.910197560.82892674 -0.58026373]
[ 0.9438508-0.61732507 -0.73560220.73460865 -0.483655]]# final_satae

3.总结
当使用??dynamic_rnn??时,对于输入数据的格式有两种:
第一种:输入格式为??[batch_size,time_steps,input_size]???,此时得到的输出output的形状为??[batch_size,time_steps,output_size]???,final_state的形状为??[batch_size,state_size]??
第二种:也就是我们上面用到的,此时的输入格式为??[time_steps,batch_size,input_size]???,得到的输出output的形状为??[time_steps,batch_size,output_size]???,final_state的形状仍然为??[batch_size,state_size]???,但此时要指定??time_major = True??
对比这两种输入方式,第二种最大的优点就是输出的结果形式方便我们观察。
更多内容欢迎扫码关注公众号月来客栈!
循环神经网络系列Tensorflow中dynamic_rnn

文章图片



【循环神经网络系列Tensorflow中dynamic_rnn】


    推荐阅读