Cannot import BertModel from transformers - python

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.

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!

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

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

ModuleNotFoundError: No module named 'allennlp.data.iterators'

In Google colab I am to trying import BucketIterator using:
from allennlp.data.iterators import BucketIterator
But it is raising the same error again and again-
ModuleNotFoundError: No module named 'allennlp.data.iterators
After installing allennlp with the imports:
from allennlp.data.token_indexers import TokenIndexer, SingleIdTokenIndexer
from allennlp.data.tokenizers.character_tokenizer import CharacterTokenizer
from allennlp.data.vocabulary import Vocabulary
from allennlp.modules.seq2vec_encoders import PytorchSeq2VecWrapper
are working fine. Is there a way to solve this issue?
I was facing the same issue.
It won't work, since Iterators are removed from allennlp.
Install a legacy version of allennlp. allennlp is depended upon the PyTorch(torchtext) library. And since torchtext removed iterators form their newer versions allennlp did it too.
You can directly use torchtext.data.BucketIterator(). Use:
pip install torchtext==0.5.0 --user

Keras Import Error when loading a pre trained model

I am trying to use a pre-trained model in Tensorflow. I am using the following code:
import tensorflow as tf
from tensorflow import keras
from keras.applications import mobilenet_v2
I get the following error:
ModuleNotFoundError: No module named 'keras'
However, the following codes do work:
from tensorflow.keras.applications import mobilenet_v2
OR
from keras_applications import mobilenet_v2
The 2 methods mentioned above work but the 1st one doesn't. Why does this happen?
I've solved this problem by downgrading tensorflow to version 2.0 using this command:
pip install tensorflow==2.0
I hope it helps you.

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!

Categories

Resources