【发布时间】:2020-07-01 15:35:30
【问题描述】:
我一直在研究 LSTM 层在神经网络架构中的实现。 LSTM 层已在其中定义,如下所示。我无法理解这段代码。我在sn-p代码后面列出了我的疑惑。
代码来源:https://gist.github.com/awjuliani/66e8f477fc1ad000b1314809d8523455#file-a3c-py
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(RNN_SIZE,state_is_tuple=True)
c_init = np.zeros((1, lstm_cell.state_size.c), np.float32)
h_init = np.zeros((1, lstm_cell.state_size.h), np.float32)
state_init = [c_init, h_init]
c_in = tf.placeholder(tf.float32, [1, lstm_cell.state_size.c])
h_in = tf.placeholder(tf.float32, [1, lstm_cell.state_size.h])
state_in = (c_in, h_in)
rnn_in = tf.expand_dims(self.h3, [0])
step_size = tf.shape(inputs)[:1]
state_in = tf.nn.rnn_cell.LSTMStateTuple(c_in, h_in)
lstm_outputs, lstm_state = tf.nn.dynamic_rnn(
lstm_cell, rnn_in, initial_state=state_in, sequence_length=step_size,
time_major=False)
lstm_c, lstm_h = lstm_state
state_out = (lstm_c[:1, :], lstm_h[:1, :])
self.rnn_out = tf.reshape(lstm_outputs, [-1, RNN_SIZE])
这是我的疑问:
- 我知道我们需要初始化一个随机上下文并隐藏 向量传递给我们的第一个 LSTM 单元。但是为什么要同时初始化 c_init、h_init 和 c_in、h_in。他们的目的是什么? 它们彼此有何不同? (state_in 和 state_init 一样吗?)
- 我们为什么要使用 LSTMStateTuple?
【问题讨论】:
-
在详细介绍之前,请告诉我们您是否从真实来源获取了此代码。如果是这样,请指定来源的链接,以便我们深入研究并为您提供帮助。谢谢!
-
@TensorflowSupport 是的,来源来自博客系列。 git:gist.github.com/awjuliani/…
-
很抱歉有一个额外的问题而不是解决方案。该代码有效吗?你执行了吗?
-
@TensorflowSupport 是的,它取自工作代码文件
标签: tensorflow machine-learning deep-learning lstm recurrent-neural-network