我的问题是在张量流中定义双向LSTM单元(fw_cell和bw_cell)我们应该单独定义fw_cell和bw_cell还是相同的?
答案 0 :(得分:4)
你可以创建一个单独定义每个单元格的函数,你可以使用这样的东西:
def lstm_rnn_cell(num_units, dropout):
_cell = tf.nn.rnn_cell.LSTMCell(num_units,state_is_tuple = True)
_cell = tf.contrib.rnn.DropoutWrapper(_cell, output_keep_prob = dropout)
return _cell
然后你可以这样做:
fw_cell = tf.nn.rnn_cell.MultiRNNCell([lstm_rnn_cell(rnn_size, dropout = dropout) for _ in range(num_layers)], state_is_tuple = True)
bw_cell = tf.nn.rnn_cell.MultiRNNCell([lstm_rnn_cell(rnn_size, dropout = dropout) for _ in range(num_layers)], state_is_tuple = True)
在此示例中,我使用了MultiRNNCell
,因此您还可以通过修改num_layers
来定义给定RNN的图层数量