TFBertForSequenceClassification requires the TensorFlow library but it was not found in your environment

Viewed 1444

this:

import tensorflow as tf
from transformers import BertTokenizer, TFBertForSequenceClassification

model = TFBertForSequenceClassification.from_pretrained("bert-base-uncased")

Outputs the following error:

ImportError: 
TFBertForSequenceClassification requires the TensorFlow library but it was not found in your environment. Checkout the instructions on the
installation page: https://www.tensorflow.org/install and follow the ones that match your environment.

However it's not true I don't have the TensorFlow library imported.

> print(tf.__version__)
'2.7.0'
3 Answers
import tensorflow as tf
from transformers import BertTokenizer, TFBertForSequenceClassification

Worked with

Tensorflow-2.7.0
huggingface-hub-0.2.1
tokenizers-0.10.3 transformers-4.15.0
Python-3.7

Install Tensorflow and Transformers

!pip install tensorflow
!pip install transformers

I believe you are on MacOS. If so, installing the latest version of transformers (v4.21.1) should fix this issue. (I am also running M1 pro, if that helps).

For me the latest update was giving an error with incompatible checkpoint weight files, so I had to install transformers v4.2.2 which gave me the above error.

To fix it, you need to do an editable install: https://huggingface.co/docs/transformers/installation#installing-from-source

i.e.

git clone https://github.com/huggingface/transformers.git

cd transformers

pip install -e .

Before running the above code, make the following changes:

  • In transformers/setup.py Line 134, change to "tokenizers==0.6.0"
  • In transformers/src/transformers/file_utils.py Line 87, change "tensorflow" to "tensorflow-macos"
  • Finally, in transformers/src/transformers/dependency_versions_check.py comment out Line 41 ("# require_version_core(deps[pkg])"

For the final step, upgrade the tokenizers library to a newer version (say 0.12.1). You cant have the newer tokenizers library before installing the transformers package - it fails to build the wheel.

Reference: https://id2thomas.medium.com/apple-silicon-experiment-1-installing-huggingface-transformers-2e45392d3d0f

https://github.com/apple/tensorflow_macos/issues/144

As this is to answer the question when Tensorflow is indeed installed and imported this can often be a problem in this case.

BERT requires the tensorflow-gpu package so you need to have installed the tensorflow-gpu package to do what you are attempting here.

IF you are in a notebook run:

!pip install tensorflow-gpu

Otherwise on the command line run:

pip install tensorflow-gpu

Hope this is helpful to others out there facing issues!

Related