-
StackOverflow 文档
-
tensorflow 教程
-
使用 TensorFlow 创建 RNN,LSTM 和双向 RNN LSTM
-
创建双向 LSTM
import tensorflow as tf
dims, layers = 32, 2
# Creating the forward and backwards cells
lstm_fw_cell = tf.nn.rnn_cell.BasicLSTMCell(dims, forget_bias=1.0)
lstm_bw_cell = tf.nn.rnn_cell.BasicLSTMCell(dims, forget_bias=1.0)
# Pass lstm_fw_cell / lstm_bw_cell directly to tf.nn.bidrectional_rnn
# if only a single layer is needed
lstm_fw_multicell = tf.nn.rnn_cell.MultiRNNCell([lstm_fw_cell]*layers)
lstm_bw_multicell = tf.nn.rnn_cell.MultiRNNCell([lstm_bw_cell]*layers)
# tf.nn.bidirectional_rnn takes a list of tensors with shape
# [batch_size x cell_fw.state_size], so separate the input into discrete
# timesteps.
_X = tf.unpack(state_below, axis=1)
# state_fw and state_bw are the final states of the forwards/backwards LSTM, respectively
outputs, state_fw, state_bw = tf.nn.bidirectional_rnn(lstm_fw_multicell, lstm_bw_multicell, _X, dtype='float32')
参数
state_below
是具有以下尺寸的 3D 张量:[batch_size
,最大序列索引,dims
]。这来自之前的操作,例如查找单词嵌入。
dims
是隐藏单位的数量。
layers
可以调整到 1 以上,以创建堆叠 LSTM 网络。
笔记
tf.unpack
可能无法确定给定轴的大小(如果是这种情况,请使用 nums
参数)。
- 在 LSTM 下添加额外的重量+偏差乘法可能会有所帮助(例如
tf.matmul(state_below, U) + b
。