huggingface transformers RuntimeError: No module named 'tensorflow.python.keras.engine.keras_tensor'
I am looking at this workbook which comes from huggingface course. I dont have internet access from my python environment but I could download files and save them in python environment. I copied all file from this folder and saved it in the folder bert-base-uncased/
. I renamed some of the files to match what is in the above folder
I have below packages
tensorflow.__version__
'2.2.0'
keras.__version__
'2.4.3'
Then I installed transformers
!pip install datasets transformers[sentencepiece]
checkpoint = "bert-base-uncased/"
from transformers import TFAutoModelForSequenceClassification
Successfully installed datasets-1.17.0 dill-0.3.4 fsspec-2022.1.0 huggingface-hub-0.4.0 multiprocess-0.70.12.2 pyarrow-6.0.1 sacremoses-0.0.47 sentencepiece-0.1.96 tokenizers-0.10.3 transformers-4.15.0 xxhash-2.0.2
all the files are available
#files from https://huggingface.co/bert-base-uncased/tree/main
import os
cwd = os.getcwd()
print (cwd)
os.listdir('bert-base-uncased/')
['gitattributes',
'vocab.txt',
'tokenizer_config.json',
'tokenizer.json',
'README.md',
'.dominokeep',
'config.json',
'tf_model.h5',
'flax_model.msgpack',
'rust_model.ot',
'pytorch_model.bin']
But I still get the below error. I don't get this error when I run the same code using google colab
model = TFAutoModelForSequenceClassification.from_pretrained('bert-base-uncased/', num_labels=2)
print ("----------------")
print (type(model))
print ("----------------")
RuntimeError: Failed to import transformers.models.bert.modeling_tf_bert because of the following error (look up to see its traceback):
No module named 'tensorflow.python.keras.engine.keras_tensor'
Solution 1:
To replicate Your issue I install TensorFlow version 2.2.0.
Then I tried to import problematic module:from tensorflow.python.keras.engine import keras_tensor
Yield importError
.
Meaning, that TensorFlow version 2.2.0 doesn't have module keras_tensor required by transformer.
Then I updated TensorFlow to version 2.7.0 and try to import keras_tensor module again and everything worked.
Updating TensorFlow to newer version should solve Your issue.
EDIT
Digging a bit for lowest working version of TensorFlow I get to setup.py of Transformers.
Version requirements for TensorFlow is >= 2.3.0.