1. 程式人生 > >tf.nn.rnn_cell.MultiRNNCell

tf.nn.rnn_cell.MultiRNNCell

col 圖片 NPU color float rnn str con bsp

  • Class tf.contrib.rnn.MultiRNNCell
  • Class tf.nn.rnn_cell.MultiRNNCell

構建多隱層神經網絡

__init__(cells, state_is_tuple=True)

cells:rnn cell 的list

state_is_tuple:true,狀態Ct和ht就是分開記錄,放在一個tuple中,接受和返回的states是n-tuples,其中n=len(cells),False,states是concatenated沿著列軸.後者即將棄用。

BasicLSTMCell 單隱層

技術分享圖片

BasicLSTMCell 多隱層

技術分享圖片

代碼示例

# encoding:utf-8
import tensorflow as tf

batch_size=10
depth=128

inputs=tf.Variable(tf.random_normal([batch_size,depth]))

previous_state0=(tf.random_normal([batch_size,100]),tf.random_normal([batch_size,100]))
previous_state1=(tf.random_normal([batch_size,200]),tf.random_normal([batch_size,200]))
previous_state2
=(tf.random_normal([batch_size,300]),tf.random_normal([batch_size,300])) num_units=[100,200,300] print(inputs) cells=[tf.nn.rnn_cell.BasicLSTMCell(num_unit) for num_unit in num_units] mul_cells=tf.nn.rnn_cell.MultiRNNCell(cells) outputs,states=mul_cells(inputs,(previous_state0,previous_state1,previous_state2))
print(outputs.shape) #(10, 300) print(states[0]) #第一層LSTM print(states[1]) #第二層LSTM print(states[2]) ##第三層LSTM print(states[0].h.shape) #第一層LSTM的h狀態,(10, 100) print(states[0].c.shape) #第一層LSTM的c狀態,(10, 100) print(states[1].h.shape) #第二層LSTM的h狀態,(10, 200)

輸出

(10, 300)
LSTMStateTuple(c=<tf.Tensor multi_rnn_cell/cell_0/basic_lstm_cell/Add_1:0 shape=(10, 100) dtype=float32>, h=<tf.Tensor multi_rnn_cell/cell_0/basic_lstm_cell/Mul_2:0 shape=(10, 100) dtype=float32>)
LSTMStateTuple(c=<tf.Tensor multi_rnn_cell/cell_1/basic_lstm_cell/Add_1:0 shape=(10, 200) dtype=float32>, h=<tf.Tensor multi_rnn_cell/cell_1/basic_lstm_cell/Mul_2:0 shape=(10, 200) dtype=float32>)
LSTMStateTuple(c=<tf.Tensor multi_rnn_cell/cell_2/basic_lstm_cell/Add_1:0 shape=(10, 300) dtype=float32>, h=<tf.Tensor multi_rnn_cell/cell_2/basic_lstm_cell/Mul_2:0 shape=(10, 300) dtype=float32>)
(10, 100)
(10, 100)
(10, 200)

tf.nn.rnn_cell.MultiRNNCell