-
Notifications
You must be signed in to change notification settings - Fork 74.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ValueError: Only instances of keras.Layer
can be added to a Sequential mode
#63085
Comments
Hi @zzj0402 , The code executes fine on Colab as per gist. This moght be related to Kaggle environment which might have incompatible TF and tensorflow_hub version. You may check the Tensorflow and tensorflow_hub in kaggle environment and try to install those works in locally. Neverthless it's not problem with Tensorflow or Keras. Thanks! |
i got the same issue also with Kaggle's environment, with @SuryanarayanaY conclusion i got the solution by just |
fixes the problem in Kaggle. |
i had the same issue, tried all the things explained here but still having the same issue. i am not using kaggle i am coding on jupyter notebook. please help |
@Kun7l I am also having same issue. I am using jupyter notebook as well with PyCharm. I have tried many things but cannot fix this error. I believe it might be a package incompatibility error. However, I am using most up to date versions. |
im also having this using conda, tried conda install again tensorflow and it no compatible with tensorflow_hub that i installed before, please help |
Has someone resolved this for jupyter notebook / conda enviroment? I am also having a similar problem (although with tensorflow probability) |
I got the same error when running the official notebook, https://www.tensorflow.org/tutorials/interpretability/integrated_gradients, the environment I am using is tensorflow==2.16 and tensorflow-hub==0.16. |
I've tried to use some previous versions of tensorflow, but on MacOS there was another bug that was't allowing me to use GPU acceleration. So I've solved the problem using the Model Subclassing API, here the example from my code:
|
do anyone know how to resolve this error, i have been trying and reinstalling the tensorflow to its latest but still its gets the error |
i cant even install older version of tensorflow the oldest i can install in 2.16 |
Consegui añadir el modelo con una lambda import tensorflow_hub as hub Cargar el modelo base preentrenadobase_model = hub.KerasLayer("https://www.kaggle.com/models/google/mobilenet-v3/TensorFlow2/large-075-224-classification/1", Construir el modelomodel = tf.keras.Sequential([
]) Compilar el modelomodel.compile( Callbacksearly_stopping = tf.keras.callbacks.EarlyStopping(patience=10, restore_best_weights=True) model.summary() |
For colab users: by downgrading your Tensorflow and TensorHub |
For Jupyter Notebook users: and changes input_shape from [None,224, 224, 3] to [224, 224, 3] (removed batch value) |
The error pops up again. Seems like version issue. |
fixed with:
This is definitely a Tensorflow issue where the package management fails to check backward capability. |
Hi @SuryanarayanaY , you forgot to run |
you have to wrap the hub layer in a Lambda layer like below: import tensorflow as tf
import tensorflow_hub as hub
# Wrap the hub layer in a Lambda layer
hub_layer_wrapper = tf.keras.layers.Lambda(lambda x: hub_layer(x))
model = tf.keras.Sequential([
hub_layer_wrapper, # Use the wrapped layer
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary() I fix my code by using lambda layer |
Same, I can only install the version starting from 2.16.1
|
Tensorflow 2.17 contains Keras3.0 which might be the reason for the error. Could you please try to install tf-keras i.e., keras2.0 and try to test the code and the if the issue is present, please raise the issue in the keras-team/keras repo as it is more related to Keras. !pip install tf-keras
import tf_keras as keras Thank you! |
This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. |
This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. |
Thanks, this worked for me |
Issue type
Bug
Have you reproduced the bug with TensorFlow Nightly?
No
Source
source
TensorFlow version
v2.15.0-rc1-8-g6887368d6d4 2.15.0
Custom code
No
OS platform and distribution
Kaggle
Mobile device
No response
Python version
No response
Bazel version
No response
GCC/compiler version
No response
CUDA/cuDNN version
No response
GPU model and memory
No response
Current behavior?
When trying to build a sequential model with TFhub model USE in Kaggle, the notebook throws an error complaining that the hub model is not a valid class instance. At the same time, the same code works fine locally.
Standalone code to reproduce the issue
The text was updated successfully, but these errors were encountered: