>>> from keras.engine.keras_tensor import KerasTensor # imported from keras-team/keras
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'keras'
>>>
>>> from tensorflow.python.keras.engine.keras_tensor import KerasTensor as KerasTensorFromTF # This import should not exist anymore
2022-01-28 10:19:31.459850: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
>>>
>>> assert KerasTensorFromTF == KerasTensor
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'KerasTensor' is not defined
>>> import tensorflow as tf
>>>
>>> from tensorflow.keras.backend import is_keras_tensor
>>> from tensorflow.python.keras.backend import is_keras_tensor as is_keras_tensor_tf # this import should not exist anymore
>>>
>>> assert is_keras_tensor(tf.keras.Input([10]))
>>> assert is_keras_tensor_tf(tf.keras.Input([10]))
>>> from keras.engine.keras_tensor import KerasTensor # imported from keras-team/keras
>>>
>>> from tensorflow.python.keras.engine.keras_tensor import KerasTensor as KerasTensorFromTF # This import should not exist anymore
>>>
>>> assert KerasTensorFromTF == KerasTensor
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> import tensorflow as tf
>>>
>>> from tensorflow.keras.backend import is_keras_tensor
>>> from tensorflow.python.keras.backend import is_keras_tensor as is_keras_tensor_tf # this import should not exist anymore
>>>
>>> assert is_keras_tensor(tf.keras.Input([10]))
>>> assert is_keras_tensor_tf(tf.keras.Input([10]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/ccs/home/asd/.conda/envs/gdy_v2/lib/python3.9/site-packages/tensorflow/python/keras/backend.py", line 1281, in is_keras_tensor
raise ValueError('Unexpectedly found an instance of type `' + str(type(x)) +
ValueError: Unexpectedly found an instance of type `<class 'keras.engine.keras_tensor.KerasTensor'>`. Expected a symbolic tensor instance.
The code under tensorflow.keras is legacy, and should not be used.
The correct way of importing keras code is always from “from tensorflow import keras” or “import tensorflow as tf; tf.keras”
Directly “import keras” will access keras package python code directly (not exactly same as the public Keras API), which might lead to method/class not found.
# tf nightly (should be same as 2.7)
import tensorflow as tf
from tensorflow import keras
x=tf.keras.Input([10])
dense = tf.keras.layers.Dense(1)
y = dense(x)
assert(keras.backend.is_keras_tensor(x))
assert(keras.backend.is_keras_tensor(y))
assert(tf.keras.backend.is_keras_tensor(x))
assert(tf.keras.backend.is_keras_tensor(y))
Note that tensorflow.python.keras is not a valid import, and it is accessing legacy code that is about to delete. You should never import that directly.