python - LSTM 单元电路中的 num_unit 实际上是什么?

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

我非常努力地到处搜索,但我找不到 TensorFlow 中的 num_units 到底是什么。我试图将我的问题与 this question 联系起来, 但我无法在那里得到明确的解释。


在 TensorFlow 中,当创建基于 LSTM 的 RNN 时,我们使用以下命令

cell = rnn.BasicLSTMCell(num_units=5, state_is_tuple=True)

作为Colah's blog说,这是一个基本的 LSTM 单元:

enter image description here

现在,假设我的数据是:

idx2char = ['h', 'i', 'e', 'l', 'o']

# Teach hello: hihell -> ihello
x_data = [[0, 1, 0, 2, 3, 3]]   # hihell
x_one_hot = [[[1, 0, 0, 0, 0],   # h 0
              [0, 1, 0, 0, 0],   # i 1
              [1, 0, 0, 0, 0],   # h 0
              [0, 0, 1, 0, 0],   # e 2
              [0, 0, 0, 1, 0],   # l 3
              [0, 0, 0, 1, 0]]]  # l 3

y_data = [[1, 0, 2, 3, 3, 4]]    # ihello

我的输入是:

x_one_hot = [[[1, 0, 0, 0, 0],   # h 0
              [0, 1, 0, 0, 0],   # i 1
              [1, 0, 0, 0, 0],   # h 0
              [0, 0, 1, 0, 0],   # e 2
              [0, 0, 0, 1, 0],   # l 3
              [0, 0, 0, 1, 0]]]  # l 3

形状为 [6,5]

this blog ,我们有如下图片

enter image description here

据我所知,BasicLSTMCell 将展开 t 个时间步长,其中 t 是我的行数(请更正如果我错了我!)。例如,在下图中,LSTM 展开了 t = 28 时间步。

enter image description here

在Colah的博客里,是这样写的

each line carries an entire vector

那么,让我们看看我的[6,5] 输入矩阵将如何通过这个基于 LSTM 的 RNN。

enter image description here

如果我的上图是正确的,那么 num_units(我们在 LSTM 单元中定义)到底是什么?它是 LSTM 单元的参数吗?

如果 num_unit 是单个 LSTM 单元的参数,那么它应该是这样的:

enter image description here

enter image description here

如果上图是正确的,那么在下面的 LSTM 单元示意图中(根据 Colah 的博客),那 5 个 num_units 在哪里?

enter image description here


如果你能用一个数字给出你的答案,那将是非常有帮助的!您可以编辑或创建新的白板图 here .

最佳答案

你的理解很正确。然而,不幸的是,Tensorflow 术语与文献之间存在不一致。为了理解,您需要深入了解 Tensorflow 实现代码。

Tensorflow 宇宙中的单元 在 Colah 的宇宙中称为 LSTM 层(即展开版本)。这就是为什么您总是定义一个单元,而不是 Tensorflow 架构中的一个层。例如,

cell=rnn.BasicLSTMCell(num_units=5,state_is_tuple=True)

检查这里的代码。

https://github.com/tensorflow/tensorflow/blob/ef96faaf02be54b7eb5945244c881126a4d38761/tensorflow/python/ops/rnn_cell.py#L90

The definition of cell in this package differs from the definition used in the literature. In the literature, cell refers to an object with a single scalar output. The definition in this package refers to a horizontal array of such units.

因此,为了理解 Tensorflow 中的 num_units,最好想象一个展开的 LSTM,如下所示。

enter image description here

在展开的版本中,您有一个输入 X_t,它是一个张量。当您指定形状的输入时

[batch_size,time_steps,n_input]

对于 Tensorflow,它知道从您的time_steps 参数展开多少次。

因此,如果您在 TensorFlow 中将 X_t 作为一维数组,那么在 Colahs 展开版本中,每个 LSTM 单元格 x_t 都变成标量值(请注意大写 X (向量/数组)和小写 x(标量)——也在 Colah 的图中)

如果您在 Tensorflow 中将 X_t 作为二维数组,那么在 Colahs 展开版本中,每个 LSTM 单元 x_t 都会变成一维数组/向量(如您的情况此处)等等。

现在最重要的问题来了。

Tensorflow 如何知道输出/隐藏维度 ** Z_t/H_t 是什么?

(请注意 H_t 和 Z_t 之间的区别 - 我通常更喜欢将它们分开,因为 H_t 返回输入(循环)而 Z_t 是输出 - 图中未显示)

它是否与 X_t 具有相同的维度?

没有。它可以是任何不同的形状。您需要将其指定给 Tensorflow。那就是num_units - 输出大小

检查这里的代码:

https://github.com/tensorflow/tensorflow/blob/ef96faaf02be54b7eb5945244c881126a4d38761/tensorflow/python/ops/rnn_cell.py#L298-L300

    @property
    def output_size(self):
        return self._num_units

Tensorflow 使用以下论文中 Colahs universe 中定义的 LSTM 单元的实现:

https://arxiv.org/pdf/1409.2329.pdf

关于python - LSTM 单元电路中的 num_unit 实际上是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49225326/

相关文章:

python - 资源耗尽错误:OOM when allocating tensor with shape []

python - 如何提交调用我的 python 脚本的 SGE 作业

python - RDD的切片和分区有什么区别?

python - 构建 tensorflow 数据集迭代器,生成具有特殊结构的批处理

python - 将 Facenet 模型 .pb 文件转换为 TFLITE 格式时出错

python - 如何使用 sparse_softmax_cross_entropy_with_logits 在 tensorflow 中实现加权交叉熵损失

python - 适合多类分类的深度学习结构

Python一起处理2个for循环

python - 如何在 Plone 4.1.4 中向 intranet_workflow 添加更多自定义状态

python - Tensorflow - Deep MNIST 教程 - 将分类器导出到 C++