了解rnn.BasicLSTMCell返回的状态维度

问题描述 投票:0回答:3

我正在观看本教程,他为MNIST分类编写了张量流代码。 这是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]))

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

2 128 128

我知道输出形状是28x128x128(time_steps x rnn_size x batch_size) 但我不明白“国家”的形状?

python tensorflow deep-learning lstm recurrent-neural-network
3个回答
1
投票

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

LSTM有一个隐藏状态,但也有一个存储单元状态;因此你的states变量的第一个维度的大小(2)。以下尺寸的大小是batch_size然后rnn_size


1
投票

states包含2个矩阵,cellhidden,它们是以下公式中的ch

enter image description here


0
投票

每个LSTM都有两个状态,长期状态为0,短期状态为1。 BasicRNNCell,总是有一个状态,即短期状态。

你已经解释过了:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.