Moaz Mohammed Husain
Moaz Mohammed Husain

Reputation: 115

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?

Upvotes: 4

Views: 22458

Answers (4)

Terence Yang
Terence Yang

Reputation: 658

lastest version of transformers have fix this issue. you can use the below command

pip install --upgrade transformers

Upvotes: 3

Pagadala Sumanth
Pagadala Sumanth

Reputation: 11

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

Upvotes: 1

user12769533
user12769533

Reputation: 278

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

Upvotes: 0

Moaz Mohammed Husain
Moaz Mohammed Husain

Reputation: 115

Fixed the error. This is the code

from transformers.modeling_bert import BertModel, BertForMaskedLM

Upvotes: 6

Related Questions