了解 rnn.BasicLSTMCell 返回的状态维度
Understanding the dimensions of states returned by rnn.BasicLSTMCell
我正在观看他为 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)
但我不明白 "states" ?
的形状
查看这个非常好的博客post,了解 LSTM 的工作原理:http://colah.github.io/posts/2015-08-Understanding-LSTMs/
LSTM 有一种隐藏状态,但也有一种记忆单元状态;因此 states
变量 (2) 的第一个维度的大小。下面维度的大小分别是batch_size
然后rnn_size
.
states
包含2个矩阵,cell
和hidden
,即下式中的c
和h
。
每个 LSTM 有两个状态,第 0 个是长期状态,第 1 个是短期状态。
BasicRNNCell,总是只有一种状态,即短期状态。
休息,你已经解释过了:
128:神经元数量,或者在您的情况下可以说 rnn_size。
128:批量大小,即每个输入一个输出。
我正在观看他为 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)
但我不明白 "states" ?
查看这个非常好的博客post,了解 LSTM 的工作原理:http://colah.github.io/posts/2015-08-Understanding-LSTMs/
LSTM 有一种隐藏状态,但也有一种记忆单元状态;因此 states
变量 (2) 的第一个维度的大小。下面维度的大小分别是batch_size
然后rnn_size
.
states
包含2个矩阵,cell
和hidden
,即下式中的c
和h
。
每个 LSTM 有两个状态,第 0 个是长期状态,第 1 个是短期状态。 BasicRNNCell,总是只有一种状态,即短期状态。
休息,你已经解释过了:
128:神经元数量,或者在您的情况下可以说 rnn_size。
128:批量大小,即每个输入一个输出。