'Tensorflow model type is `kears.engine` instead of `tensorflow.python.keras`

I am at the moment training a model in a custom training loop. Therefore I need access to model.trainable_variables. However my model has type keras.engine.sequential.Sequential object at 0x7fa284a24d30 instead of tensorflow.python.keras.engine.sequential.Sequential (see this question).

So as soon as I try to grab the trainable variables I get the error:

AttributeError: 'function' object has no attribute 'trainable_variables'

I tested the types in my script and in the terminal as follows:

Python 3.9.5 (default, Nov 18 2021, 16:00:48) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'2.8.0'
>>> print(tf.keras.models.Sequential())
<keras.engine.sequential.Sequential object at 0x7f181ad64fd0>

which clearly should not happen.

I tried to remove keras and tensorflow and install them again using pip. I tried to remove tensorflow and keras and install only keras.

I also tried to create a new virtual environment to install everything from scratch in a clean environment but the same happens.

Can anyone shed some light on what is happening and how I can access the trainable variables



Solution 1:[1]

The bug:

AttributeError: 'function' object has no attribute 'trainable_variables'

was caused by a bug in the code. The script now runs properly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Augusto Peres