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)

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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM