ValueError: The first argument to `Layer.call` must always be passed

ValueError: The first argument to `Layer.call` must always be passed

我试图用顺序 API 建立一个模型(它已经用函数 API 为我工作)。这是我尝试在 Sequential API:

中构建的模型
from tensorflow.keras import layers
model_1 = tf.keras.Sequential([
    layers.Input(shape=(1,), dtype='string'),
    text_vectorizer(),
    embedding(),
    layer.GlobalAveragePooling1D(),
    layers.Dense(1, activation='sigmoid')
], name="model_1_dense")

Error:
----> 4     text_vectorizer(),
      5     embedding(),
      6     layer.GlobalAveragePooling1D(),
ValueError: The first argument to `Layer.call` must always be passed.

下面是 text_vectorizer 图层的样子:

max_vocab_length = 10000
max_length = 15

text_vectorizer = TextVectorization(max_tokens=max_vocab_length,
                                    output_mode="int",
                                    output_sequence_length=max_length)

text_vectorizer 层应该在没有括号的情况下传递给您的模型。尝试这样的事情:

import tensorflow as tf

max_vocab_length = 10000
max_length = 15

text_vectorizer = tf.keras.layers.TextVectorization(max_tokens=max_vocab_length,
                                    output_mode="int",
                                    output_sequence_length=max_length)

text_dataset = tf.data.Dataset.from_tensor_slices(["foo", "bar", "baz"])
text_vectorizer.adapt(text_dataset.batch(64))
model_1 = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(1,), dtype='string'),
    text_vectorizer,
    tf.keras.layers.Embedding(max_vocab_length, 50),
    tf.keras.layers.GlobalAveragePooling1D(),
    tf.keras.layers.Dense(1, activation='sigmoid')
], name="model_1_dense")

print(model_1(tf.constant([['foo']])))
tf.Tensor([[0.48518932]], shape=(1, 1), dtype=float32)