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)
tf.nn.dynamic_rnn的作用:
对于单个 RNNCell ,使用call 函数进行运算时,只在序列时间上前进了一步 ,如使用 x1、 ho 得到此h1,通过 x2 、h1 得到 h2 等 。
如果序列长度为n,要调用n次call函数,比较麻烦。对此提供了一个tf.nn.dynamic_mn函数,使用该函数相当于调用了n次call函数。通过{ho, x1 , x2,…,xn} 直接得到{h1 , h2,…,hn} 。
具体来说,设输入数据inputs格式为(batch_size, time_steps, input_size),其中batch_size表示batch的大小。time_steps序列长度,input_size输入数据单个序列单个时间维度上固有的长度。得到的outputs是time_steps步里所有的输出。它的形状为(batch_size, time_steps, cell.output_size)。state 是最后一步的隐状态,形状为(batch_size, cell . state_size) 。
参数:
cell
自己定义的LSTM的细胞单元,如是convLSTM,自己写也可以。
inputs
一个三维的变量,[batchsize,timestep,input_size],搭配time_major=False。其中batch_size表示batch的大小。time_steps序列长度,input_size输入数据单个序列单个时间维度上固有的长度。
这里还补充一点,就是叫dynamic的原因,就是输入数据的time_step不一定要相同,如果长短不一,会自动跟短的补0,但是处理时候,不会处理0,在0前面就截止了.这就是dynamic对比static的好处.
time_major
If true, these Tensors must be shaped [max_time, batch_size, depth].
If false, these Tensors must be shaped `[batch_size, max_time, depth]
返回:
outputs:
If time_major == False, this will be a Tensor
shaped: [batch_size, max_time, cell.output_size]
.(默认这种方式)
If time_major == True , this will be a Tensor
shaped: [max_time, batch_size, cell.output_size]
.
cell.output_size就是cell的num_units
这里output是每个cell输出的叠加,比如我输入数据[1,5,100,100,3],是一个长度为5 的视频序列,则返回output为[1,5,100,100,3],5个cell细胞的输出状态,state是一个元组类型的数据,有(c和h两个变量)就是存储LSTM最后一个cell的输出状态,我一般用的是output的最后一个输出.用state输出也行,就是取元组中的h变量.
state:
If cell.state_size
is an int, this will be shaped [batch_size,cell.state_size]
.
If it is a TensorShape
, this will be shaped [batch_size] + cell.state_size
.
If it is a (possibly nested) tuple of ints or TensorShape
, this will be a tuple having the corresponding shapes.
If cells are LSTMCells
state
will be a tuple containing a LSTMStateTuple
for each cell.
cell.state_size就是cell的num_units
例子:
#create a BasicRNNCell rnn_cell = tf.nn.rnn_cell.BasicRNNCell(hidden_size) #'outputs' is a tensor of shape [batch_size, max_time, cell_state_size] #defining initial state initial_state = rnn_cell.zero_state(batch_size, dtype=tf.float32) #'state' is a tensor of shape [batch_size, cell_state_size] outputs, state = tf.nn.dynamic_rnn(cell=rnn_cell,inputs=input_data,initial_state=initial_state,dtype=tf.float32)
#create 2 LSTMCells rnn_layers = [tf.nn.rnn_cell.LSTMCell(size) for size in [128, 256]] #create a RNN cell composed sequentially of a number of RNNCells multi_rnn_cell = tf.nn.rnn_cell.MultiRNNCell(rnn_layers) #'outputs' is a tensor of shape [batch_size, max_time, 256] #'state' is a N-tuple where N is the number of LSTMCells containing a #tf.contrib.rnn.LSTMStateTuple for each cell outputs, state = tf.nn.dynamic_rnn(cell=multi_rnn_cell,inputs=data,dtype=tf.float32)