tensorflow: ValueError: Only instances of `keras.Layer` can be added to a Sequential mode
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'>)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 26 (1 by maintainers)
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.
you have to wrap the hub layer in a Lambda layer like below:
I fix my code by using lambda layer I hope it is useful for you
fixed with:
This is definitely a Tensorflow issue where the package management fails to check backward capability.
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)),
])
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()
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
i got the same issue also with Kaggle’s environment, with @SuryanarayanaY conclusion i got the solution by just
pip install tensorflowagain and found out that the keras library reinstalls to latest versionHi @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!
This worked. Although model.summary() not showing anything for layers. So, I believe this fix is just temporary and might get fixed on future versions.
Thanks, this worked for me
Hi @SuryanarayanaY , you forgot to run
build_model(embed)on your gist, when run, it will raise exactly the same error.i cant even install older version of tensorflow the oldest i can install in 2.16