在此处的嵌入示例中: 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 用于从这个可变长度序列转换为固定表示形式是什么意思呢?这里的“可变长度”是什么意思?