Cannot import BertModel from transformers

Question:

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?

Answers:

Fixed the error. This is the code

from transformers.modeling_bert import BertModel, BertForMaskedLM

You can use your code too from transformers import BertModel, BertForMaskedLM; just make sure your transformers is updated.

Answered By: user12769533

lastest version of transformers have fix this issue

pip install upgrade transformers
Answered By: Fan Yang

It’s very simple, Uninstall transformer and reinstall it along with spacy.
It worked for me.

Answered By: Pagadala Sumanth