GlobalAveragePooling1D 在 keras 中做什么?

问题描述 投票:0回答:1

在此处的嵌入示例中: https://www.tensorflow.org/text/guide/word_embeddings

result = embedding_layer(tf.constant([[0, 1, 2], [3, 4, 5]]))
result.shape
TensorShape([2, 3, 5])

然后解释:

When given a batch of sequences as input, an embedding layer returns a 3D floating point tensor, of shape (samples, sequence_length, embedding_dimensionality). To convert from this sequence of variable length to a fixed representation there are a variety of standard approaches. You could use an RNN, Attention, or pooling layer before passing it to a Dense layer. This tutorial uses pooling because it's the simplest. 

The GlobalAveragePooling1D layer returns a fixed-length output vector for each example by averaging over the sequence dimension. This allows the model to handle input of variable length, in the simplest way possible.

然后是代码:

embedding_dim=16

model = Sequential([
  vectorize_layer,
  Embedding(vocab_size, embedding_dim, name="embedding"),
  GlobalAveragePooling1D(),
  Dense(16, activation='relu'),
  Dense(1)
])

GlobalAveragePooling1D 应该为维度 = n 的每个单词的嵌入计算一个整数。我不明白这部分:

This allows the model to handle input of variable length, in the simplest way possible.

同样:

To convert from this sequence of variable length to a fixed representation there are a variety of standard approaches.

在每个嵌入层中,输入长度已经由参数“input_length”固定。使用截断和填充来保证输入的固定长度。那么,GlobalAveragePooling1D 用于从这个可变长度序列转换为固定表示形式是什么意思呢?这里的“可变长度”是什么意思?

keras nlp embedding
1个回答
0
投票

我自己正在研究 ML,所以这只是我对 GlobalAveragePooling1D 的理解。

理解这个例子的关键是你引用的段落上方的引用:

它可以嵌入可变长度的序列。您可以将形状 (32, 10)(长度为 10 的 32 个序列的批次)或长度为 (64, 15)(长度为 64 个序列的批次)输入到上面的嵌入层。

因此在一次拟合中,所有序列都将具有相同的长度。但随后您可以采用相同的模型并将其拟合到其他序列长度上。感谢 GlobalAveragePooling1,到达密集层的向量的长度将是相同的(实际上它们将等于嵌入维度)。

这就是我对流程的理解:

© www.soinside.com 2019 - 2024. All rights reserved.