【问题标题】:Understanding the dimensions of states returned by rnn.BasicLSTMCell了解 rnn.BasicLSTMCell 返回的状态的维度
【发布时间】:2017-06-27 05:40:39
【问题描述】:

我正在看这个教程,他为 MNIST 分类编写了一个 tensorflow 代码。
这是RNN模型:

batch_size = 128
chunk_size = 28
n_chunks = 28
rnn_size = 128

def recurrent_neural_network(x):
   layer = {'weights':tf.Variable(tf.random_normal([rnn_size,n_classes])),
         'biases':tf.Variable(tf.random_normal([n_classes]))}

   x = tf.transpose(x, [1,0,2])
   x = tf.reshape(x, [-1, chunk_size])
   x = tf.split(x, n_chunks, 0)

   lstm_cell = rnn.BasicLSTMCell(rnn_size,state_is_tuple=True)
   outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)

   output = tf.matmul(outputs[-1],layer['weights']) + layer['biases']

   return output,outputs,states

在此之后,我分别打印输出和状态的维度
像这样:

print("\n", len(outputs),"\n",len(outputs[0]),"\n",len(outputs[0][0]))
print("\n", len(states),"\n",len(states[0]),"\n",len(states[0][0]))

我得到打印语句的输出为:
28
128
128

2
128
128

我了解输出形状为 28x128x128 (time_steps x rnn_size x batch_size)
但我不明白“状态”的形状?

【问题讨论】:

    标签: python tensorflow deep-learning lstm recurrent-neural-network


    【解决方案1】:

    查看这篇关于 LSTM 工作原理的非常好的博文:http://colah.github.io/posts/2015-08-Understanding-LSTMs/

    LSTM 有一种隐藏状态,但也有一种记忆单元状态;因此您的states 变量(2)的第一个维度的大小。以下维度的大小分别是batch_size然后是rnn_size

    【讨论】:

    • 那么对于batch_size(128个这样的块)中的每个块(28个数字),LSTM会改变它的内部状态吗?你知道任何解释 rnn 批处理的资料吗?非常感谢您的宝贵时间。
    【解决方案2】:

    states 包含 2 个矩阵,cellhidden,在以下公式中分别为 ch

    【讨论】:

      【解决方案3】:

      每个 LSTM 有两个状态,第 0 个代表长期状态,第 1 个代表短期状态。 BasicRNNCell,永远只有一个状态,即短期状态。

      剩下的你已经解释过了:

      128:神经元的数量,或者在你的情况下可以说 rnn_size。

      128:批量大小,即每个输入一个输出。

      【讨论】:

        猜你喜欢
        • 2020-01-28
        • 2019-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-08
        • 2014-03-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多