Problem loading a finetuned model.
Hi!

There is a problem with the way model are saved and loaded. The following code should crash and doesn't:

```python
import torch
from pytorch_pretrained_bert import BertForSequenceClassification

model_fn = 'model.bin'
bert_model = 'bert-base-multilingual-cased'
model = BertForSequenceClassification.from_pretrained(bert_model, num_labels = 16)

model_to_save = model.module if hasattr(model, 'module') else model
torch.save(model_to_save.state_dict(), model_fn)
print(model_to_save.num_labels)

model_state_dict = torch.load(model_fn)
loaded_model = BertForSequenceClassification.from_pretrained(bert_model, state_dict = model_state_dict)
print(loaded_model.num_labels)
```

This code prints:

```
16
2
```

The code should raise an exception when trying to load the weights of the task specific linear layer. I'm guessing that the problem comes from `PreTrainedBertModel.from_pretrained`.

I would be happy to submit a PR fixing this problem but I'm not used to work with the PyTorch loading mechanisms. @thomwolf could you give me some guidance?

Cheers!
Ok I managed to find the problem. It comes from:

https://github.com/huggingface/pytorch-pretrained-BERT/blob/7fb94ab934b2ad1041613fc93c61d13105faf98a/pytorch_pretrained_bert/modeling.py#L534-L540

When trying to load `classifier.weight` and `classifier.bias`, the following line gets added to `error_msgs`:  
```
size mismatch for classifier.weight: copying a param with shape torch.Size([16, 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([16]) from checkpoint, the shape in current model is torch.Size([2]).
```
First, I think that we should add a check of `error_msgs` to `from_pretrained`. I don't really know if there is any other way than printing an error message and existing the program since the default behavior (keeping the classifier layer randomly initialized) can be frustrating for the user (I speak from experience ^^).

To fix this, we should probably fetch the number of labels of the saved model and use it to instanciate the model being created before loading the saved weights. Unfortunately I don't really know how to do that, any idea?

Another possible "fix" would be to force the user to give a `num_labels` argument when loading a pretrained classification model with the following code in `BertForSequenceClassification`:
```python
    @classmethod
    def from_pretrained(cls, *args, **kwargs):
        if 'num_labels' not in kwargs:
            raise ValueError('num_labels should be given when loading a pre-trained classification model')
        return super().from_pretrained(*args, **kwargs)
```
And even with this code, we are not able to check that the `num_labels` value is the same as the saved model. I don't really like the idea of forcing the user to give an information that the checkpoint already contains.<|||||>Just use the num_labels when you load your model
```python
model_state_dict = torch.load(model_fn)
loaded_model = BertForSequenceClassification.from_pretrained(bert_model, state_dict = model_state_dict, num_labels = 16)
print(loaded_model.num_labels)```
<|||||>As mentioned in my previous posts, I think that the library should either fetch the number of labels from the save file or force the user to provide a `num_labels` argument. 

While what you are proposing fixes my problem I would like to prevent this problem for other users in the future by patching the library code.<|||||>I see thanks @rodgzilla. Indeed not using the `error_msg` is bad practice, let's raise these errors.

Regarding fetching the number of labels, I understand your point but it will probably add too much custom logic in the library for the moment so let's go for your simple solution of setting the number of labels as mandatory for now (should have done that since the beginning).<|||||>Hi everyone!
I had to come here to know that I had to include `num_labels` when loading the model because the error was misleading.
Also, I didn't know how many labels there were so I had to guess.
The model I was trying to load:
[biobert-base-cased-v1.1-mnli](https://huggingface.co/dmis-lab/biobert-base-cased-v1.1-mnli#)<|||||>I'm also facing a similar problem using the same model as @ugm2 - [biobert-base-cased-v1.1-mnli](https://huggingface.co/dmis-lab/biobert-base-cased-v1.1-mnli#) 

In my example I know the exact `num_labels` and provide it as an argument while loading the model. 
How can I solve this?

```
RuntimeError: 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([10, 768]).
	size mismatch for classifier.bias: copying a param with shape torch.Size([3]) from checkpoint, the shape in current model is torch.Size([10]).
```<|||||>With the latest transformers versions, you can use the recently introduced (https://github.com/huggingface/transformers/pull/12664) `ignore_mismatched_sizes=True` parameter for `from_pretrained` method in order to specify that you'd rather drop the layers that have incompatible shapes rather than raise a `RuntimeError`. 