Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: Error(s) in loading state_dict for BertForSequenceClassification #57

Closed
Lunde89 opened this issue Aug 21, 2020 · 7 comments
Assignees

Comments

@Lunde89
Copy link

Lunde89 commented Aug 21, 2020

Full error message:
Error(s) in loading state_dict for BertForSequenceClassification:
size mismatch for classifier.weight: copying a param with shape torch.Size([3, 768]) from checkpoint, the shape in current model is torch.Size([2, 768]).
size mismatch for classifier.bias: copying a param with shape torch.Size([3]) from checkpoint, the shape in current model is torch.Size([2]).

Trying to run the following code:
from danlp.models import load_bert_tone_model
classifier = load_bert_tone_model()

Using:
Torch 1.6+cpu
Python 3.7.4
daNLP 0.0.4

@AmaliePauli
Copy link
Contributor

Hi Lunde89,
The DaNLP version 0.0.4 do not support the BertTone model, please update the package to version 0.0.9 .
I hope this will fix your issue.
Amalie

@AmaliePauli AmaliePauli self-assigned this Aug 24, 2020
@Lunde89
Copy link
Author

Lunde89 commented Aug 24, 2020

Hi Amalie,

I might have mis-typed in the original comment. Sorry about that. At least, i'm using 0.0.9 now - and it produces the same error.

@AmaliePauli
Copy link
Contributor

Alright, what about the transformes version? You also need this package for it to run. It is tested with transformers==2.3.0. ?

@Lunde89
Copy link
Author

Lunde89 commented Aug 26, 2020

Hi Amalie,

I figured out the issue The problem is that the version you get from PyPi isn't the same as the one on this repository. After i installed from source, everything works like a charm! 👍

@KennethEnevoldsen
Copy link
Contributor

KennethEnevoldsen commented Oct 9, 2020

Just to +1 this comment I have had the exact same problem. Seems like an update of the PyPi is needed.

A simple fix:

pip install tqdm==4.42.1
pip install transformers==2.3.0
pip install danlp==0.0.9

Notice that: flair 0.6.1 requires transformers>=3.0.0, but you'll have transformers 2.3.0 which is incompatible.

(update)
It does however seem that the PyPi isn't up to date with the repo as the following give me this error:

from danlp.models import load_bert_tone_model
classifier = load_bert_tone_model()
classifier.predict('Jeg tror alligvel, det bliver godt')  # works
classifier.predict_proba('Analysen viser, at økonomien bliver forfærdelig dårlig')
AttributeError: 'BertTone' object has no attribute 'predict_proba'

Looking into the class using dir(classifier), we get:

['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'model_pol',
 'model_sub',
 'predict',
 'tokenizer_pol',
 'tokenizer_sub']

notably not way to extract probability.

@kirajen
Copy link

kirajen commented Oct 23, 2020

Hi all,

I am now facing the same issue. I have ensured all my versions are as in the comment above.

@KennethEnevoldsen
Copy link
Contributor

KennethEnevoldsen commented Oct 23, 2020

So @kirajen please try updating to the latest version. You can do this using:

pip install git+https://github.com/alexandrainst/danlp.git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants