用图解释RNN程序的部分过程
LSTM的隐藏层的单元个数为hidden_size,因此,LSTM每一步的输出数据的维度为(batch_size,hidden_size)。有因为LSTM展开的时间步数为num_steps,于是通过
outputs.append(cell_output)
将每一时刻的输出都收集起来,这样,最后的outputs是一个list。
整个过程为:
图中黄色的部分表示同一个序列在LSTM不同时刻的输出。
紧接着对outputs进行拼接和reshape,其过程如下图:
将每一时刻的输出在第1维上拼接(上图),这样每一行就完整的表示了一个序列。reshape后的结构如下图:
其中每一种颜色表示一个序列,同一种颜色中的各个块表示这个序列的不同时刻。
以上就是LSTM的输出,并对其适当变形。接下来通过一个全连接层,将每一时刻的输出映射成字典大小。这部分就是常见的y=wx+b的构造形式,通过以下代码实现:
softmax_w = tf.get_variable( "softmax_w", [hidden_size, output_size], dtype=tf.float32) # hidden_size就是cell中的隐藏单元
softmax_b = tf.get_variable("softmax_b", [output_size], dtype=tf.float32)
# 网络的最后输出(相当于最后添加了一个全连接层)
logits = tf.matmul(output, softmax_w) + softmax_b # logits shape:batch_size*num_step,output_size
通过全连接层后,得到logits,其维度为(batch_size*num_step,output_size)
参考网址:https://blog.****.net/xyz1584172808/article/details/83056179