Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
bpiaple opened this issue Aug 6, 2024 · 0 comments
Open
Labels

Comments

@bpiaple
Copy link

bpiaple commented Aug 6, 2024

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant