Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-11-20· OpenAIAi2 WildChat-4.8M (AI2)

Failed to import transformers.trainer because of the following error (look up to see its…

  1. User

    Failed to import transformers.trainer because of the following error (look up to see its traceback): Failed to import transformers.integrations.integration_utils because of the following error (look up to see its traceback): Failed to import transformers.modeling_tf_utils because of the following error (look up to see its traceback): Your currently installed version of Keras is Keras 3, but this is not yet supported in Transformers. Please install the backwards-compatible tf-keras package with pip install tf-keras.

  2. Assistant

    The error you're facing occurs because the Transformers library by Hugging Face currently does not support Keras 3, which is part of TensorFlow 2.14 and later versions. Hugging Face still expects a TensorFlow/Keras integration based on older TensorFlow releases.

    Here’s how you can resolve the issue:

    Solution 1: Install a Compatible Version of TensorFlow and Keras

    Downgrade TensorFlow to version 2.13.x (or below), which uses Keras 2 instead of Keras 3. For example:

    pip install --upgrade tensorflow==2.13.*
    

    Hugging Face's Transformers library works smoothly with TensorFlow 2.13 at the time of writing.

    Solution 2: Use tf-keras Package

    The error suggests that you install the backward-compatible tf-keras package. To do this:

    pip install tf-keras
    

    After installing, try running your code again.

    Solution 3: Check for Transformers Updates

    Hugging Face is actively updating its libraries to match the latest versions of TensorFlow/Keras. Make sure you are using the latest release of the Transformers library. Update it with:

    pip install --upgrade transformers
    

    If the latest version of Transformers still doesn’t support Keras 3, you’ll need to rely on the above solutions for compatibility.

    Solution 4: Use PyTorch Instead

    If switching TensorFlow/Keras versions is not an option, you can use the PyTorch backend for the Transformers library, as it does not depend on TensorFlow:

    1. Install PyTorch:
      pip install torch
      
    2. Configure your Hugging Face setup to use PyTorch:
      from transformers import AutoModel
      model = AutoModel.from_pretrained('model_name', framework='pt')
      

    This avoids TensorFlow/Keras entirely.

    Summary

    The simplest solution to this issue is to downgrade TensorFlow to version 2.13 or install the tf-keras package. Alternatively, you can switch to PyTorch if you don’t specifically need TensorFlow/Keras.