When attempting to add a TensorFlow Hub KerasLayer to a Sequential model, you may encounter the following error:
Only instances of `keras.Layer` can be added to a Sequential model.
Only instances of `keras.Layer` can be added to a Sequential model. Received: <tensorflow_hub.keras_layer.KerasLayer object at 0x72492078c110> (of type <class 'tensorflow_hub.keras_layer.KerasLayer'>)
Cause
This error occurs because the isinstance(layer, Layer) check in Sequential.add returns False for hub.KerasLayer, even though it inherits from keras.layers.Layer. This is due to a change in the way Keras imports the TensorFlow backend in versions 2.16.0 and above.
Solution
To resolve this issue, you can use the following solution:
Install the tf_keras package using pip:
pip install tf_keras
In your code, use the following code to determine which version of Keras to import:
version_fn = getattr(tf.keras, "version", None)
if version_fn and version_fn().startswith("3."):
import tf_keras as keras
else:
keras = tf.keras
This code imports tf_keras if you are using Keras version 3 or higher, and imports tf.keras directly if you are using Keras version 2.
Example
Consider the following code snippet:
import tensorflow as tf
import tensorflow_hub as hub
model = tf.keras.Sequential()
layer = hub.KerasLayer("https://tfhub.dev/google/tf2-preview/nnlm-en-dim128/1")
# This line raises the error in TensorFlow 2.16.0 and above
model.add(layer)
To fix the error, you can use the following updated code:
import tensorflow as tf
import tensorflow_hub as hub
version_fn = getattr(tf.keras, "version", None)
if version_fn and version_fn().startswith("3."):
import tf_keras as keras
else:
keras = tf.keras
model = keras.Sequential()
layer = hub.KerasLayer("https://tfhub.dev/google/tf2-preview/nnlm-en-dim128/1")
model.add(layer)
With this modification, the code will run successfully without raising the error.
Comments
Post a Comment
Oof!