Skip to content

Bug: Unable to create a sequential model using a TensorFlow Hub model #920

@bpiaple

Description

@bpiaple

What happened?

I'm trying to create a model using a TensorFlow Sequential API, but I've the error. The model is : https://tfhub.dev/google/universal-sentence-encoder/4

Relevant code

from tensorflow_hub import hub
from tensorflow.keras import layers

encoder_url = "https://tfhub.dev/google/universal-sentence-encoder/4"

sentence_encoder_layer = hub.KerasLayer(encoder_url,
                                        input_shape=[],
                                        dtype=tf.string,
                                        trainable=True,
                                        name="USE")

model_6 = tf.keras.Sequential()
model_6.add(sentence_encoder_layer)
model_6.add(layers.Dense(64, activation="relu"))
model_6.add(layers.Dense(1, activation="sigmoid"))

Relevant log output

{
	"name": "ValueError",
	"message": "Only instances of `keras.Layer` can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object at 0x75776b09fc10> (of type <class 'tensorflow_hub.keras_layer.KerasLayer'>)",
	"stack": "---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[123], line 2
      1 model_6 = tf.keras.Sequential()
----> 2 model_6.add(sentence_encoder_layer)
      3 model_6.add(layers.Dense(64, activation=\"relu\"))
      4 model_6.add(layers.Dense(1, activation=\"sigmoid\"))

File ~/.python/current/lib/python3.10/site-packages/keras/src/models/sequential.py:95, in Sequential.add(self, layer, rebuild)
     93         layer = origin_layer
     94 if not isinstance(layer, Layer):
---> 95     raise ValueError(
     96         \"Only instances of `keras.Layer` can be \"
     97         f\"added to a Sequential model. Received: {layer} \"
     98         f\"(of type {type(layer)})\"
     99     )
    100 if not self._is_layer_name_unique(layer):
    101     raise ValueError(
    102         \"All layers added to a Sequential model \"
    103         f\"should have unique names. Name '{layer.name}' is already \"
    104         \"the name of a layer in this model. Update the `name` argument \"
    105         \"to pass a unique name.\"
    106     )

ValueError: Only instances of `keras.Layer` can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object at 0x75776b09fc10> (of type <class 'tensorflow_hub.keras_layer.KerasLayer'>)"
}

tensorflow_hub Version

0.16.1

TensorFlow Version

2.17.0

Other libraries

tensorflow==2.17.0
tensorflow_hub==0.16.1
keras==3.4.1

Python Version

3.x

OS

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions