No module named 'transformers.models' while trying to import BertTokenizer - python

I am trying to import BertTokenizer from the transformers library as follows:
import transformers
from transformers import BertTokenizer
from transformers.modeling_bert import BertModel, BertForMaskedLM
However, I get the following error:
I am using transformers version 3.5.1 because I had a problem with the updated version which can be found here.
Does anyone know how to fix this? Apart from updating the transformers library to its latest version (that will unfortunately cause more errors).
Any help is appreciated!

you can change your code from
transformers.modeling_bert import BertModel, BertForMaskedLM
to
from transformers.models.bert.modeling_bert import BertModel,BertForMaskedLM

How you call the package depends on how you installed the transformers package.
If you've installed it from pip you need to find the directory where the package is installed, for example, if installing with pip with conda as a virtual environment this is where the BertModel package will be stored:
/home/{USER_NAME}/anaconda3/envs/{CONDA_ENV_NAME}/lib/{PYTHON_VERSION}/site-packages/transformers/modeling_bert.py
while the BertTokenizer is automatically called in the init.py, hence can be directly called.
Therefore you should be able to call
from transformers.modeling_bert import BertModel, BertForMaskedLM
from transformers import BertTokenizer
otherwise, if you've installed it from source, you need to map to the correct file. For Example, with the following file directory structure:
Code_folder
transformers #package
models
bert
modeling_bert.py
main.py # your file that wants to call the transformer package
Then you can call the following packages in the following way:
from transformers.models.bert.modeling_bert import BertModel,BertForMaskedLM
from transformers import BertTokenizer

Related

How to import utils from keras_unet

I'm trying to add utils from keras_unet in google colab, but I have a problem.
import tensorflow as tfs
from keras_unet import utils
keras-unet init: TF version is >= 2.0.0 - using tf.keras instead of Keras
ModuleNotFoundError
You must install keras-unet before importing as follows
!pip install keras-unet
from keras_unet import utils
Let us know if the issue still persists. Thanks!

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'
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.

Cannot import BertModel from transformers

I am trying to import BertModel from transformers, but it fails. This is code I am using
from transformers import BertModel, BertForMaskedLM
This is the error I get
ImportError: cannot import name 'BertModel' from 'transformers'
Can anyone help me fix this?
Fixed the error. This is the code
from transformers.modeling_bert import BertModel, BertForMaskedLM
lastest version of transformers have fix this issue
pip install upgrade transformers
You can use your code too from transformers import BertModel, BertForMaskedLM; just make sure your transformers is updated.
It's very simple, Uninstall transformer and reinstall it along with spacy.
It worked for me.

Python module Gensim error "cannot import name utils"

Hi I am using Gensim Word2Vec for word embedding in python.
from gensim.models import Word2Vec, KeyedVectors
But i am getting error like:
from gensim import utils
# cannot import whole gensim.corpora, because that imports wikicorpus...
from gensim.corpora.dictionary import Dictionary
ImportError: cannot import name utils. Thank you
I had a similar issue - In my case it was a dependency issue on scipy. I did the following to reinstall scipy.
pip uninstall scipy
conda install scipy
Hope this helps!

ImportError: No module named datasets

from datasets import dataset_utils ImportError: No module named datasets.
when i am writing this in python sript.
import tensorflow as tf
from datasets import dataset_utils
slim = tf.contrib.slim
But i am getting error.
from datasets import dataset_utils
ImportError: No module named datasets
I found this solution
How can jupyter access a new tensorflow module installed in the right path?
I did the same and i have dataset packages at path anaconda/lib/python2.7/site-packages/. Still i am getting same error.
pip install datasets
I solved it this way.
You can find the folder address on your device and append it to system path.
import sys
sys.path.append(r"D:\Python35\models\slim\datasets"); import dataset_utils
You'll need to do the same with 'nets' and 'preprocessing'
sys.path.append(r"D:\Python35\models\slim\nets"); import vgg
sys.path.append(r"D:\Python35\models\slim\preprocessing"); import vgg_preprocessing
Datasets is present in https://github.com/tensorflow/models/tree/master/slim/datasets
Since 'models' are not installable from pip (at the time of writing), they are not available in python load paths by default. So either we copy them or manually add to the path.
Here is how I setup env before running the code:
# git clone or wget
wget https://github.com/tensorflow/models/archive/master.zip -O models.zip
unzip models.zip
# add it to Python PATH
export PYTHONPATH=$PYTHONPATH:$PWD/models-master/slim
# now we are good to call `python mytensorflow.py`
It's using the datasets package in the TF-slim image models library, which is in:
git clone https://github.com/tensorflow/models/
Having done that though, in order to import the module as shown in the example on the slim image page, empty init.py have to be added to the models and models/slim directories.
go to https://github.com/nschaetti/EchoTorch/releases and download the latest release
install the latest release from the downloaded file (202006291 is the latest version at the moment):
$pip install ./EchoTorch-202006291.zip
test it out using narma10_esn.py (other examples may have some issues)
you may still need to install some more python packages not listed in the requirements file but it works once you do this.

Categories

Resources