简体   繁体   English

Keras中LSTM层的输入尺寸

[英]The input dimension of the LSTM layer in Keras

I'm trying keras.layers.LSTM. 我正在尝试keras.layers.LSTM。 The following code works. 以下代码有效。

#!/usr/bin/python3
import tensorflow as tf
import numpy as np
from tensorflow import keras

data = np.array([1, 2, 3]).reshape((1, 3, 1)) 
x = keras.layers.Input(shape=(3, 1)) 
y = keras.layers.LSTM(10)(x)
model = keras.Model(inputs=x, outputs=y)

print (model.predict(data))

As shown above, the input data shape is (1, 3, 1) , and the actual input shape in the Input layer is (3, 1) . 如上所示,输入数据形状为(1, 3, 1) ,而输入层中的实际输入形状为(3, 1) I'm a little bit confused about this inconsistency of the dimension. 我对此维度的不一致感到有些困惑。 If I use the following shape in the Input layer, it doesn't work: 如果我在“输入”层中使用以下形状,则它将不起作用:

x = keras.layers.Input(shape=(1, 3, 1)) 

The error message is as follows: 错误消息如下:

ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 1, 3, 1]

It seems that the rank of the input must be 3, but why should we use a rank-2 shape in the Input layer? 似乎输入的等级必须为3,但是为什么要在Input层中使用等级2的形状?

Check out the documentation for tf.keras.Input . 查看有关tf.keras.Input的文档。 The syntax is as- 语法为-

tf.keras.Input(
    shape=None,
    batch_size=None,
    name=None,
    dtype=None,
    sparse=False,
    tensor=None,
    **kwargs
)

shape: defines the shape of a single sample, with variable batch size. 形状:定义具有可变批量大小的单个样品的形状。

Notice, that it expects the first value as batch_size otherwise pass batch_size as a parameter explicitly 注意,它期望第一个值是batch_size,否则显式传递batch_size作为参数

Keras works with "batches" of "samples". Keras处理“样本”的“批次”。 Since most models use variable batch sizes that you define only when fitting, for convenience you don't need to care about the batch dimension, but only with the sample dimension. 由于大多数模型使用的可变批量大小仅在拟合时定义,因此为方便起见,您不必关心批量尺寸,而只关心样本尺寸。

That said, when you use shape = (3,1) , this is the same as defining batch_shape = (None, 3, 1) or batch_input_shape = (None, 3, 1) . 就是说,当您使用shape = (3,1) ,这与定义batch_shape = (None, 3, 1)batch_input_shape = (None, 3, 1)

The three options mean: 这三个选项表示:

  • A variable batch size: None 可变批量大小: None
  • With samples of shape (3, 1) . 具有形状(3, 1)样本。

It's important to know this distinction especially when you are going to create custom layers, losses or metrics. 重要的是要知道这种区别,尤其是当您要创建自定义图层,损失或指标时。 The actual tensors all have the batch dimension and you should take that into account when making operations with tensors. 实际的张量都具有批处理尺寸,使用张量进行操作时应考虑到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM