Skip to content
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

Closed
zzj0402 opened this issue Feb 29, 2024 · 25 comments
Closed
Assignees
Labels
comp:keras Keras related issues stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.15 For issues related to 2.15.x type:bug Bug

Comments

@zzj0402
Copy link

zzj0402 commented Feb 29, 2024

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

import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.optimizers import Adam, SGD
from tensorflow.keras.layers import Dense, Input, BatchNormalization, Dropout, Concatenate
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.callbacks import ModelCheckpoint
module_url = 'https://tfhub.dev/google/universal-sentence-encoder-large/5'
embed = hub.KerasLayer(module_url, trainable=True, name='USE_embedding')
def build_model(embed):
    
    model = Sequential([
        Input(shape=[], dtype=tf.string),
        embed,
        Dense(1, activation='sigmoid')
    ])
    model.compile(loss='mean_squared_error',
              optimizer=tf.keras.optimizers.Adam(
                  learning_rate=0.0001,
                  beta_1=0.9,
                  beta_2=0.999,
                  epsilon=1e-07,
                  amsgrad=False,
                  name='Adam'
              ), metrics=['accuracy'])
    return model


### Relevant log output

```shell
ValueError                                Traceback (most recent call last)
Cell In[9], line 1
----> 1 model=build_model(embed)
      2 model.fit(descriptions, labels, epochs=4)
      3 model.save('quality/use.keras')

Cell In[8], line 12, in build_model(embed)
     10 def build_model(embed):
---> 12     model = Sequential([
     13         Input(shape=[], dtype=tf.string),
     14         embed,
     15         Dense(1, activation='sigmoid')
     16     ])
     17     model.compile(loss='mean_squared_error',
     18               optimizer=tf.keras.optimizers.Adam(
     19                   learning_rate=0.0001,
   (...)
     24                   name='Adam'
     25               ), metrics=['accuracy'])
     26     return model

File /opt/conda/lib/python3.10/site-packages/keras/src/models/sequential.py:70, in Sequential.__init__(self, layers, trainable, name)
     68 if layers:
     69     for layer in layers:
---> 70         self.add(layer, rebuild=False)
     71     self._maybe_rebuild()

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

ValueError: Only instances of `keras.Layer` can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object at 0x793cedbef220> (of type <class 'tensorflow_hub.keras_layer.KerasLayer'>)
@google-ml-butler google-ml-butler bot added the type:bug Bug label Feb 29, 2024
@SuryanarayanaY SuryanarayanaY added TF 2.15 For issues related to 2.15.x comp:keras Keras related issues labels Feb 29, 2024
@SuryanarayanaY
Copy link
Collaborator

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!

@SuryanarayanaY SuryanarayanaY added the stat:awaiting response Status - Awaiting response from author label Mar 1, 2024
@AyakashiKitsune
Copy link

i got the same issue also with Kaggle's environment, with @SuryanarayanaY conclusion i got the solution by just
pip install tensorflow
again and found out that the keras library reinstalls to latest version

@google-ml-butler google-ml-butler bot removed the stat:awaiting response Status - Awaiting response from author label Mar 1, 2024
@zzj0402
Copy link
Author

zzj0402 commented Mar 1, 2024

!pip install tensorflow

fixes the problem in Kaggle.

@zzj0402 zzj0402 closed this as completed Mar 1, 2024
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@Kun7l
Copy link

Kun7l commented Apr 3, 2024

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

@zrjohnson3
Copy link

@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.

@marendra
Copy link

im also having this using conda, tried conda install again tensorflow and it no compatible with tensorflow_hub that i installed before, please help

@monica-ayala
Copy link

Has someone resolved this for jupyter notebook / conda enviroment? I am also having a similar problem (although with tensorflow probability)

@deidaraho
Copy link

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.
But if I roll back to tensorflow 2.14 and tensorflow-hub 0.15, there is no such error.

@DavideB45
Copy link

DavideB45 commented Apr 19, 2024

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:

class MyModel(keras.Model):
  def __init__(self):
    super(MyModel, self).__init__()
    self.embedding_layer = embed
    self.dense_layer1 = layers.Dense(256, activation='relu')
    self.dense_layer2 = layers.Dense(128, activation='relu')
    self.dense_layer3 = layers.Dense(64, activation='relu')
    self.output_layer = layers.Dense(13, activation='softmax')

  def call(self, inputs):
    x = self.embedding_layer(inputs)
    x = self.dense_layer1(x)
    x = self.dense_layer2(x)
    x = self.dense_layer3(x)
    return self.output_layer(x)

@mauzumshamil
Copy link

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

@Mohmaedhasssan
Copy link

i cant even install older version of tensorflow the oldest i can install in 2.16

@jrcosio
Copy link

jrcosio commented Jul 3, 2024

Consegui añadir el modelo con una lambda

import tensorflow_hub as hub

Cargar el modelo base preentrenado

base_model = hub.KerasLayer("https://www.kaggle.com/models/google/mobilenet-v3/TensorFlow2/large-075-224-classification/1",
trainable=True, arguments=dict(batch_norm_momentum=0.997))

Construir el modelo

model = tf.keras.Sequential([
# Capa de entrada
tf.keras.layers.InputLayer(input_shape=(224, 224, 3)),

# Envolver la capa del modelo base en una capa Lambda
tf.keras.layers.Lambda(lambda x: base_model(x)),

tf.keras.layers.Dropout(0.2),

# Capa densa final para clasificación
tf.keras.layers.Dense(3, activation='softmax')

])

Compilar el modelo

model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)

Callbacks

early_stopping = tf.keras.callbacks.EarlyStopping(patience=10, restore_best_weights=True)
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(factor=0.2, patience=5, min_lr=1e-6)

model.summary()

@Bushman57
Copy link

For colab users: by downgrading your Tensorflow and TensorHub
!pip install tensforflow == 2.8.0 and tensorflow_hub == 0.12.0

@Niaziz007
Copy link

Niaziz007 commented Aug 8, 2024

For Jupyter Notebook users:
I have installed these version of Tensorflow and tersorflow_hub:
TensorFlow version: 2.14.0
TensorFlow Hub version: 0.15.0

and changes input_shape from [None,224, 224, 3] to [224, 224, 3] (removed batch value)
and these changes resolved the issue for me.

@zzj0402
Copy link
Author

zzj0402 commented Aug 22, 2024

!pip install tensorflow

fixes the problem in Kaggle.

The error pops up again. Seems like version issue.

@zzj0402 zzj0402 reopened this Aug 22, 2024
@zzj0402
Copy link
Author

zzj0402 commented Aug 22, 2024

fixed with:

!pip install tensorflow==2.15.0 tensorflow-hub keras==2.15.0

This is definitely a Tensorflow issue where the package management fails to check backward capability.

@bekicot
Copy link

bekicot commented Aug 27, 2024

Hi @SuryanarayanaY , you forgot to run build_model(embed) on your gist, when run, it will raise exactly the same error.

@mninja567
Copy link

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
I hope it is useful for you

@jaydss123
Copy link

i cant even install older version of tensorflow the oldest i can install in 2.16

Same, I can only install the version starting from 2.16.1

(.venv) PS D:\fastapi> pip install --no-cache-dir tensorflow==2.15.0
ERROR: Could not find a version that satisfies the requirement tensorflow==2.15.0 (from versions: 2.16.0rc0, 2.16.1, 2.16.2, 2.17.0rc0, 2.17.0rc1, 2.17.0)
ERROR: No matching distribution found for tensorflow==2.15.0

@tilakrayal
Copy link
Contributor

tilakrayal commented Oct 1, 2024

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!

@tilakrayal tilakrayal added the stat:awaiting response Status - Awaiting response from author label Oct 1, 2024
Copy link

github-actions bot commented Oct 9, 2024

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.

@github-actions github-actions bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label Oct 9, 2024
Copy link

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.

Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@CryoYohan
Copy link

fixed with:

!pip install tensorflow==2.15.0 tensorflow-hub keras==2.15.0

This is definitely a Tensorflow issue where the package management fails to check backward capability.

Thanks, this worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:keras Keras related issues stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.15 For issues related to 2.15.x type:bug Bug
Projects
None yet
Development

No branches or pull requests