【发布时间】:2019-05-05 16:40:54
【问题描述】:
谁能告诉我,在 TensorFlow 框架中,如何使用用户定义的值初始化 LSTM 网络的隐藏状态?我试图通过给出第一个 LSTM 单元的特定隐藏状态来将辅助信息合并到 LSTM。
【问题讨论】:
标签: tensorflow neural-network deep-learning lstm rnn
谁能告诉我,在 TensorFlow 框架中,如何使用用户定义的值初始化 LSTM 网络的隐藏状态?我试图通过给出第一个 LSTM 单元的特定隐藏状态来将辅助信息合并到 LSTM。
【问题讨论】:
标签: tensorflow neural-network deep-learning lstm rnn
您可以通过负责展开图形的函数的参数initial_state 传递 LSTM 的初始隐藏状态。
我假设您将在 tensorflow 中使用以下一些函数来创建循环神经网络 (RNN):tf.nn.dynamic_rnn、bidirectional_dynamic_rnn、tf.nn.static_rnn 或 tf.nn.static_bidirectional_rnn 。
它们都有一个initial_state 参数。对于双向 RNN,您需要传递前向 (initial_state_fw) 和后向 (initial_state_bw) 传递的初始状态。
使用tf.nn.dynamic_rnn 定义模型的示例:
import tensorflow as tf
batch_size = 32
max_sequence_length = 100
num_features = 128
num_units = 64
input_sequence = tf.placeholder(tf.float32, shape=[batch_size, max_sequence_length, num_features])
input_sequence_lengths = tf.placeholder(tf.int32, shape=[batch_size])
cell = tf.nn.rnn_cell.LSTMCell(num_units=num_units, state_is_tuple=True)
# Initial states
cell_state = tf.zeros([batch_size, num_units], tf.float32)
hidden_state = tf.placeholder(tf.float32, [batch_size, num_units])
my_initial_state = tf.nn.rnn_cell.LSTMStateTuple(cell_state, hidden_state)
outputs, states = tf.nn.dynamic_rnn(
cell=cell,
inputs=input_sequence,
initial_state=my_initial_state,
sequence_length=input_sequence_lengths)
由于我们使用state_is_tuple=True,我们需要传递一个初始状态,它是cell_state 和hidden_state 的元组。
在LSTMCell的文档中,这个元组对应c_state和m_state,其中previous discussion指出这分别代表单元状态和隐藏状态。
因此,由于我们只想初始化第一个隐藏状态,cell_state 被初始化为零。
【讨论】: