Using tensorflow.keras throws import error: ImportError: cannot import name 'type_spec_registry' from 'tensorflow.python.framework'

Hello there, first post here, so please let me know if there is a specific format I should post in or if there is more information you need to be able to comment.

I’m currently trying to train a CNN with help from my GPU. For that I followed the installation guide from the tensorflow documentation: Install TensorFlow with pip , except installing MiniConda because I’m using Anaconda.
This worked fine, my GPU is detected by tensorflow, but whenever I try to import something from keras, I get the error mentioned in the title of the post. So for example when I’m importing EarlyStopping like this from tensorflow.keras.callbacks import EarlyStopping it throws the error: ImportError: cannot import name ‘type_spec_registry’ from ‘tensorflow.python.framework’ (C:\Users\USER\anaconda3\envs\tf\lib\site-packages\tensorflow\python\

I’m not sure why this happens. I’m using tensorflow version 2.10.1, installed CUDA 11.2, cuDNN 8.1 and python 3.9.16.

If anyone knows something about this, please share any info!


Welcome to the Tensorflow Forum!

It seems the issue with version compatibility between Tensorflow and Keras.

Note: To use Keras, you will need to have the TensorFlow package installed. See detailed instructions.

Once TensorFlow is installed, just import Keras via:

from tensorflow import keras

If the problem persists, could you please try upgrading to the most recent Tensorflow version and let us know?

Thank you!

I couldn’t make it work with a lower version and then tried upgrading to tensorflow 2.11 and the error didn’t occur anymore. But I wanted to use a lower tensorflow version in the first place, because of the GPU support. Isn’t there any way to make a lower tensorflow version work with keras?


Sorry for the late response. Usually the fix in the older versions will be done if that is a security bug.
Generally, there are more bug fixes and enhancements in the latest versions.

In your case you can still access GPU on windows through WSL2. For more details please refer to Installer TensorFlow avec pip.

Thank you!

Hello, please help me. How can I fix this error "
AttributeError: module ‘keras.api._v2.keras.layers’ has no attribute ‘Transformer’" ? .
In my code when I use layers.Transformer like this transformer = layers.Transformer(num_layers=4, d_model=32, num_heads=4, activation=‘relu’, dropout=0.2)(inputs) give me this error


Welcome to the Tensorflow Forum!

How do you import the Transformer layer? Please share the standalone code to reproduce the issue.

Thank you!

"import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, models, losses, regularizers, optimizers, callbacks
import tensorflow_addons as tfa

inputs = layers.Input(shape=(10, 18))

CNN layer

cnn = layers.Conv1D(filters=32, kernel_size=3, padding=‘same’, activation=‘relu’)(inputs)
cnn = layers.MaxPooling1D(pool_size=1)(cnn)
cnn = layers.Dropout(rate=0.2)(cnn)
cnn = layers.Conv1D(filters=18, kernel_size=3, padding=‘same’, activation=‘relu’)(cnn)
cnn = layers.MaxPooling1D(pool_size=1)(cnn)
cnn = layers.Dropout(rate=0.2)(cnn)

Transformer Encoder

transformer_encoder = tfa.layers.TransformerEncoder(num_layers=2, d_model=32, num_heads=4,
dff=64, dropout=0.1, activation=‘relu’)(cnn)
Hello, please help me, this is a part of my code that relevent to error.
when run the code I encounter this error. I import Transformer layer with this" tensorflow_addons as tfa" at the beginning of the code.

AttributeError: module ‘tensorflow_addons.layers’ has no attribute ‘TransformerEncoder’"

Also, The version of tensorflow is “2.6.0” and the version of python is “3.9.12”
and “tensorflow_addons==0.16.1” that all is compatible together.

Hi dear i am getting issue in tensorflow

I have been installed tensorflow new version for 3.11 python

Now when I run a command of from tensorflow import layers & from tansorflow import ImageDataGenerator

It give me error: cannot import name ‘layers’ from tansorflow &

Errorr: cannot import name ‘ImageDataGeneretor’ from tansorflow

Please help me how to fix this issue?

I have installed tensorflow packages and when I give command of Import tansorflow as tf it works and also if I give command from tensorflow import keras it works

Please need help.

@chunduriv, did you find a solution?

Hi, did anyone find a solution? i’m using the below command though keep ending up with this error

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense

from tensorflow.keras.models import Sequential
ModuleNotFoundError: No module named ‘tensorflow.keras’

try using tensorflow.python.keras.models

from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import LSTM, Dense

Use from tensorflow.python.keras import layers