2017-09-13 113 views
4

我想知道如何在Tensorflow中使用多層雙向LSTM。如何在Tensorflow中使用多層雙向LSTM?

我已經實現了雙向LSTM的內容,但是我想將這個模型與添加了多層的模型進行比較。

我該如何在這部分添加一些代碼?

x = tf.unstack(tf.transpose(x, perm=[1, 0, 2])) 
#print(x[0].get_shape()) 

# Define lstm cells with tensorflow 
# Forward direction cell 
lstm_fw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0) 
# Backward direction cell 
lstm_bw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0) 

# Get lstm cell output 
try: 
    outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x, 
              dtype=tf.float32) 
except Exception: # Old TensorFlow version only returns outputs not states 
    outputs = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x, 
            dtype=tf.float32) 

# Linear activation, using rnn inner loop last output 
outputs = tf.stack(outputs, axis=1) 
outputs = tf.reshape(outputs, (batch_size*n_steps, n_hidden*2)) 
outputs = tf.matmul(outputs, weights['out']) + biases['out'] 
outputs = tf.reshape(outputs, (batch_size, n_steps, n_classes)) 

回答

2

你可以用兩種不同的方法適用於多層bilstm型號:

1)使用了以前的bilstm層作爲輸入到下bilstm。在開始時,您應該創建具有長度爲num_layers的向前和向後單元格的陣列。並且

for n in range(num_layers): 
     cell_fw = cell_forw[n] 
     cell_bw = cell_back[n] 

     state_fw = cell_fw.zero_state(batch_size, tf.float32) 
     state_bw = cell_bw.zero_state(batch_size, tf.float32) 

     (output_fw, output_bw), last_state = tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, output, 
                      initial_state_fw=state_fw, 
                      initial_state_bw=state_bw, 
                      scope='BLSTM_'+ str(n), 
                      dtype=tf.float32) 

     output = tf.concat([output_fw, output_bw], axis=2) 

2)另外值得一看stacked bilstm

+1

我試過這個,得到這個錯誤:ValueError:變量bidirectional_rnn/fw/lstm_cell/kernel已經存在,不允許。你是否想在VarScope中設置reuse = True?你能提供一個有效的例子嗎? – Rahul