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

I was trying to build a model with the Sequential API (it has already worked for me with the Functional API). Here is the model that I'm trying to built in 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.

Here is how text_vectorizer layer look like:

max_vocab_length = 10000
max_length = 15

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


Solution 1:[1]

The text_vectorizer layer should be passed to your model without parentheses. Try something like this:

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AloneTogether