mrdbourke / tensorflow-deep-learning

All course materials for the Zero to Mastery Deep Learning with TensorFlow course.
https://dbourke.link/ZTMTFcourse
MIT License
5.16k stars 2.53k forks source link

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

Open moatednorth opened 1 month ago

moatednorth commented 1 month ago

Discussed in https://github.com/mrdbourke/tensorflow-deep-learning/discussions/662

Originally posted by **moatednorth** July 31, 2024 Sequential api can't handle `hub.KerasLayer` ` # Create a Keras Layer using the USE pretrained layer from Kaggle sentence_encoder_layer = hub.KerasLayer("https://www.kaggle.com/models/google/universal-sentence-encoder/TensorFlow2/universal-sentence-encoder/2", input_shape=[], dtype=tf.string, trainable=False, name="USE") # Create model using the Sequential API model_6 = tf.keras.Sequential([ sentence_encoder_layer, layers.Dense(64, activation="relu"), layers.Dense(1, activation="sigmoid", name="ouput_layer") ], name="model_6_USE") `
AnilKamath27 commented 2 weeks ago

Hi, moatednorth. This is an issue that is occurring in the latest version of tensorflow 2.17. This won't be an issue if you try with tf 2.15.0. Below is a screenshot of my notebook where I was able to use hub.KerasLayers as part of Sequential API.

image