9

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

3
  • As a workaround, I ended up using pyTorch
    – belkarx
    Commented Nov 27, 2022 at 2:04
  • Hi, possibly Keras and Tensorflow are mismatched for Windows look at # C:\Python310\lib\site-packages\tensorflow\python\keras\saving_pycache_ ( keras==2.8.0, tensorflow==2.8.0, tensorflow-estimator==2.8.0, tensorflow-hub==0.7.0 ) Commented Nov 27, 2022 at 3:10
  • 1
    I'm using Linux, and used the latest version so both (fresh install)
    – belkarx
    Commented Nov 27, 2022 at 4:43

2 Answers 2

13

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.

2
  • Same as me Keras and Tensorflow==2.8.0 that is because sklearn the same problems. Commented Nov 27, 2022 at 8:28
  • 4
    Tensorflow 2.11 broke for me with the issue reported above. Rolling back to 2.10 fixed it for me.
    – carter
    Commented Dec 2, 2022 at 22:03
3

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                                                                                                                                                           

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.