时间:2022-10-25 09:32:59 | 栏目:Python代码 | 点击:次
已经完成了RNN网络的构建,但是我们对于RNN网络还有许多疑问,特别是tf.nn.dynamic_rnn函数,其具体的应用方式我们并不熟悉,查询了一下资料,我心里的想法是这样的。
tf.nn.dynamic_rnn( cell, inputs, sequence_length=None, initial_state=None, dtype=None, parallel_iterations=None, swap_memory=False, time_major=False, scope=None )
其返回值为outputs,states。
outputs
:RNN的最后一层的输出,是一个tensor。如果为time_major== False,则它的shape为[batch_size,max_time,cell.output_size]。如果为time_major== True,则它的shape为[max_time,batch_size,cell.output_size]。
states
:是每一层的最后一个step的输出,是一个tensor。state是最终的状态,也就是序列中最后一个cell输出的状态。一般情况下states的形状为 [batch_size, cell.output_size],但当输入的cell为BasicLSTMCell时,states的形状为[2,batch_size, cell.output_size ],其中2也对应着LSTM中的cell state和hidden state。
我们首先使用单层的RNN进行实验。
使用的代码为:
import tensorflow as tf import numpy as np n_steps = 2 #两个step n_inputs = 3 #每个input是三维 n_nerve = 4 #神经元个数 X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) basic_cell = tf.nn.rnn_cell.BasicRNNCell(num_units=n_nerve) outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32) init = tf.global_variables_initializer() X_batch = np.array([[[0, 1, 2], [1, 2, 3]], [[3, 4, 5], [4, 5, 6]], [[5, 6, 7], [6, 7, 8]], [[7, 8, 9], [8, 9, 10]]]) with tf.Session() as sess: sess.run(init) outputs_val, states_val = sess.run([outputs, states], feed_dict={X: X_batch}) print("outputs:", outputs_val) print("states:", states_val)
输出的log为:
outputs: [[[0.92146313 0.6069534 0.24989243 0.9305415 ] [0.9234855 0.8470011 0.7865616 0.99935764]] [[0.9772771 0.9713368 0.99483156 0.9999987 ] [0.9753329 0.99538314 0.9988139 1. ]] [[0.9901842 0.99558043 0.9998626 1. ] [0.989398 0.9992842 0.9999691 1. ]] [[0.99577546 0.9993256 0.99999636 1. ] [0.9954579 0.9998903 0.99999917 1. ]]] states: [[0.9234855 0.8470011 0.7865616 0.99935764] [0.9753329 0.99538314 0.9988139 1. ] [0.989398 0.9992842 0.9999691 1. ] [0.9954579 0.9998903 0.99999917 1. ]]
在time_major = False的时候:
接下来我们使用两层的RNN进行实验。
使用的代码为:
import tensorflow as tf import numpy as np n_steps = 2 #两个step n_inputs = 3 #每个input是三维 n_nerve = 4 #神经元个数 X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) #定义多层 layers = [tf.nn.rnn_cell.BasicRNNCell(num_units=n_nerve) for i in range(2)] multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers) outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32) init = tf.global_variables_initializer() X_batch = np.array([[[0, 1, 2], [1, 2, 3]], [[3, 4, 5], [4, 5, 6]], [[5, 6, 7], [6, 7, 8]], [[7, 8, 9], [8, 9, 10]]]) with tf.Session() as sess: sess.run(init) outputs_val, states_val = sess.run([outputs, states], feed_dict={X: X_batch}) print("outputs:", outputs_val) print("states:", states_val)
输出的log为:
outputs: [[[-0.577939 -0.3657474 -0.21074213 0.8188577 ] [-0.67090076 -0.47001836 -0.40080917 0.6026697 ]] [[-0.72777444 -0.36500326 -0.7526911 0.86113644] [-0.7928404 -0.6413429 -0.61007065 0.787065 ]] [[-0.7537433 -0.35850585 -0.83090436 0.8573037 ] [-0.82016116 -0.6559162 -0.7360482 0.7915131 ]] [[-0.7597004 -0.35760364 -0.8450942 0.8567379 ] [-0.8276395 -0.6573326 -0.7727142 0.7895221 ]]] states: (array([[-0.71645427, -0.0585744 , 0.95318353, 0.8424729 ], [-0.99845 , -0.5044571 , 0.9955299 , 0.9750488 ], [-0.99992913, -0.8408632 , 0.99885863, 0.9932366 ], [-0.99999577, -0.9672 , 0.9996866 , 0.99814796]], dtype=float32), array([[-0.67090076, -0.47001836, -0.40080917, 0.6026697 ], [-0.7928404 , -0.6413429 , -0.61007065, 0.787065 ], [-0.82016116, -0.6559162 , -0.7360482 , 0.7915131 ], [-0.8276395 , -0.6573326 , -0.7727142 , 0.7895221 ]], dtype=float32))
可以看出来outputs对应的是RNN的最后一层的输出,states对应的是每一层的最后一个step的输出,在完成了两层的定义后,outputs的shape并没有变化,而states的内容多了一层,分别对应RNN的两层输出。
state中最后一层输出对应着outputs最后一步的输出。