为什么在 keras tensorflow 的 layers.Embedding 中需要 input_length?

Why is input_length needed in layers.Embedding in keras tensorflow?

layers.embedding 有一个参数 (input_length),documentation 描述为:

input_length : Length of input sequences, when it is constant. This argument is required if you are going to connect Flatten then Dense layers upstream (without it, the shape of the dense outputs cannot be computed).

为什么无法计算密集输出的形状。对我来说,Flatten 似乎很容易做到。它只是一个 tf.rehshape(input,(-1,1)) 后跟一个具有我们选择的任何输出形状的密集层。

你能帮我指出我对整个逻辑理解的失误吗?

通过指定维度,您可以确保模型接收固定长度的输入。

从技术上讲,您可以将 None 放在您想要的任何输入维度上。形状将在 运行 时推断出来。

您只需要确保指定层参数(input_dim、output_dim)、kernel_size(对于转换层)、单位(对于 FC 层)。

如果使用 Input 并指定将通过网络传递的张量的形状,则可以计算形状。

例如以下模型是完全有效的:

from tensorflow.keras import layers
from tensorflow.keras import models

ip = layers.Input((10))
emb = layers.Embedding(10, 2)(ip)
flat = layers.Flatten()(emb)
out = layers.Dense(5)(flat)

model = models.Model(ip, out)

model.summary()
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         [(None, 10)]              0         
_________________________________________________________________
embedding (Embedding)        (None, 10, 2)             20        
_________________________________________________________________
flatten (Flatten)            (None, 20)                0         
_________________________________________________________________
dense (Dense)                (None, 5)                 105       
=================================================================
Total params: 125
Trainable params: 125
Non-trainable params: 0

这里,我没有指定input_length,但它是从Input层推断出来的。

问题出在Sequential API,如果你不在Input层指定输入shape,在embedding层也不指定,就无法建立模型使用正确的参数集。

例如,

from tensorflow.keras import layers
from tensorflow.keras import models

model = models.Sequential()
model.add(layers.Embedding(10, 2, input_length = 10)) # will be an error if I don't specify input_length here as there is no way to know the shape of the next layers without knowing the length

model.add(layers.Flatten())
model.add(layers.Dense(5))


model.summary()

在此示例中,您必须指定 input_length,否则模型将抛出错误。