No module named 'keras.saving.hdf5_format'

Question:

After pip3 installing tensorflow and the transformers library, I’m receiving the titular error when I try loading this

from transformers import pipeline
classifier = pipeline("text-classification",model='bhadresh-savani/distilbert-base-uncased-emotion')

The error traceback looks like:

RuntimeError: Failed to import transformers.models.distilbert.modeling_tf_distilbert because of the following error (look up to see its traceback):
No module named 'keras.saving.hdf5_format'

I have ensured keras got installed with transformers, so I’m not sure why it isn’t working

Asked By: belkarx

||

Answers:

If you are using the latest version of TensorFlow and Keras then you have to try this code and you have got this error as shown below

RuntimeError: Failed to import transformers.models.distilbert.modeling_tf_distilbert because of the following error (look up to see its traceback):
No module named 'keras.saving.hdf5_format'

Now, expand this error traces as I have shown below

Click on the 14 frames

Now click on the 14 frames and select as shown below
Click on modeling_tf_utils.py
Now comment this line as shown in the picture below
Comment this from keras.saving.hdf5_format import save_attributes_to_hdf5_group

Now, try this and your error will gone.

The problem is that this is in the older version of keras and you are using the latest version of keras. So, you can skip all these steps and go back to the older version and it will work eventually.

Answered By: Mohammad Ahmed

I confirm that:

pip install -U tensorflow==2.10 

fixes it and now e.g. this runs:

pytest -s
============================================================================ test session starts ============================================================================
platform linux -- Python 3.10.4, pytest-7.2.1, pluggy-1.0.0
rootdir: /home/codespace/Downloads
plugins: anyio-3.6.2, typeguard-2.13.3
collected 0 items                                                                                                                                                           
Answered By: Mata Pata
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.