Error: AttributeError: module 'transformers' has no attribute 'TFBertModel'

10,629

You should probably list the available package with its version in your python and your Colab link, for TFBertModel is only available when you have tensorflow.

In order to reproduce your error. I play around in the Colab as following:

  1. No tensorflow cause error when you import TFBertModel
!pip install transformers
from transformers import BertModel, TFBertModel # no attribute 'TFBertModel'
!pip install tensorflow-gpu
from transformers import BertModel, TFBertModel # good to go
  1. Directly use BertModel
!pip install transformers
from transformers import BertModel
BertModel.from_pretrained # good to go

As the result of my testing, you should probably check out if you import the TFBertModel while let tensorflow uninstalled.

Transformers under the master branch import the TFBertModel only if is_tf_available() is set to True. Here is the code for if_is_tf_available():

# transformers/src/transformers/file_utils.py 
# >>> 107 lines
def is_tf_available():
    return _tf_available

# >>> 48 lines
try:
    USE_TF = os.environ.get("USE_TF", "AUTO").upper()
    USE_TORCH = os.environ.get("USE_TORCH", "AUTO").upper()

    if USE_TF in ("1", "ON", "YES", "AUTO") and USE_TORCH not in ("1", "ON", "YES"):
        import tensorflow as tf

        assert hasattr(tf, "__version__") and int(tf.__version__[0]) >= 2
        _tf_available = True  # pylint: disable=invalid-name
        logger.info("TensorFlow version {} available.".format(tf.__version__))
    else:
        logger.info("Disabling Tensorflow because USE_TORCH is set")
        _tf_available = False
except (ImportError, AssertionError):
    _tf_available = False  # pylint: disable=invalid-name
Share:
10,629
dodo
Author by

dodo

I am very excited to work as “Data Scientist” and Java Developer using the cutting edge technologies. I am very eager to work in the Big Data industry and Machine Learning as it is one of the hottest topics. Big Data has many challenges and barriers innovates me. I will be happy if I have opportunity to express in such field. I am close to finish my Big Data Postgraduate Diploma at Nile University and I had successfully completed Machine Learning course from University of Washington. Currently I am registered in Informatics Master Degree. My experience as Senior System Analyst, Java Software Designer, and Java Senior Developer for more than 10 years in the Internet Banking industry and E-Commerce will help me to satisfy the customer needs and build strong trust relationship with him/here, which will lead to powerful solution, meets the quality standard and agility. My greatest strength is that, I am very enthusiastic about the quality. I already enhanced the quality in our product by code review, very restricted Unit Test for every single requirement. Linkedin profile: https://eg.linkedin.com/in/abeer-mohamed-7a32671b

Updated on December 09, 2022

Comments

  • dodo
    dodo over 1 year

    I am applying transfer learning with the python framework (PyTorch). I am getting the below error, when loading a PyTorch pre-trained model in Google Colab. After changing the code 1 to be as code 2, I got the same error.

    CODE 1:  BertModel.from_pretrained
    CODE 2: TFBertModel.from_pretrained
    Error: AttributeError: module 'transformers' has no attribute 'TFBertModel'
    

    I tried to search the internet, but I didn't find any useful content.

    • nbro
      nbro about 4 years
      Hi. You probably should provide the link to the Colab notebook with all your code.
    • user108569
      user108569 almost 4 years
      are you using tensorflow 2.0?