Error in TFBertForSequenceClassification
Author: emillykkejensen
Pull request: no

## 🐛 Bug

<!-- Important information -->

Model I am using (Bert, XLNet....): Bert

Language I am using the model on (English, Chinese....): Multi-lingual

The problem arise when using:
* [x] the official example scripts: (give details)
* [ ] my own modified scripts: (give details)

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [x] my own task or dataset: (give details)

<!-- If you have a code sample, error messages, stack traces, please provide it here as well. -->

## Expected behavior

I have fine-tuned a language model using `run_lm_finetuning.py`.

When trying to load it with TFBertForSequenceClassification however, it fails.

```
config = transformers.BertConfig.from_json_file('./bertlm_model/config.json')
model = transformers.TFBertForSequenceClassification.from_pretrained('./bertlm_model/', from_pt = True)
```

Showing the following error:

```
>>> model = transformers.TFBertForSequenceClassification.from_pretrained('./bertlm_model/', from_pt = True, config = config)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
    return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
    return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 145, in load_pytorch_weights_in_tf2_model
    assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
AssertionError: classifier.weight not found in PyTorch model
```


If I try to run either `transformers.BertForSequenceClassification.from_pretrained('bertlm_model')` or `transformers.TFBertModel.from_pretrained('bertlm_model', from_pt = True)` all is fine!



<!-- A clear and concise description of what you expected to happen. -->

## Environment

* OS: Ubuntu 18.04
* Python version: 3.7.5
* PyTorch version: 1.3.1
* Transformers version (or branch): Git repo master comit 0cb163865a4c761c226b151283309eedb2b1ca4d
* Using GPU: Yes
* Distributed of parallel setup ?
* Any other relevant information:



Comments:
The code line that loads the BERT configuration is surely correct:
```
> config = transformers.BertConfig.from_json_file('./bertlm_model/config.json')
```
But, for what concern the loading of a fine-tuned BERT model on a custom dataset, I think it's not correct the line you've used. Can you try with the following line suggested by me?
```
> from transformers import TFBertForSequenceClassification
> model = TFBertForSequenceClassification.from_pretrained('bertlm_model', from_pt = True)
```

I suspect that it doesn't work however. **It's a PyTorch->TF 2.0 conversion problem**. It would be useful to understand that this bug occurs with _only_ BERT model or with _other_ models.

> ##  Bug
> Model I am using (Bert, XLNet....): Bert
> 
> Language I am using the model on (English, Chinese....): Multi-lingual
> 
> The problem arise when using:
> 
> * [x]  the official example scripts: (give details)
> * [ ]  my own modified scripts: (give details)
> 
> The tasks I am working on is:
> 
> * [ ]  an official GLUE/SQUaD task: (give the name)
> * [x]  my own task or dataset: (give details)
> 
> ## Expected behavior
> I have fine-tuned a language model using `run_lm_finetuning.py`.
> 
> When trying to load it with TFBertForSequenceClassification however, it fails.
> 
> ```
> config = transformers.BertConfig.from_json_file('./bertlm_model/config.json')
> model = transformers.TFBertForSequenceClassification.from_pretrained('./bertlm_model/', from_pt = True)
> ```
> 
> Showing the following error:
> 
> ```
> >>> model = transformers.TFBertForSequenceClassification.from_pretrained('./bertlm_model/', from_pt = True, config = config)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
>     return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
>     return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 145, in load_pytorch_weights_in_tf2_model
>     assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
> AssertionError: classifier.weight not found in PyTorch model
> ```
> 
> If I try to run either `transformers.BertForSequenceClassification.from_pretrained('bertlm_model')` or `transformers.TFBertModel.from_pretrained('bertlm_model', from_pt = True)` all is fine!
> 
> ## Environment
> * OS: Ubuntu 18.04
> * Python version: 3.7.5
> * PyTorch version: 1.3.1
> * Transformers version (or branch): Git repo master comit [0cb1638](https://github.com/huggingface/transformers/commit/0cb163865a4c761c226b151283309eedb2b1ca4d)
> * Using GPU: Yes
> * Distributed of parallel setup ?
> * Any other relevant information:

Thanks for your answer - unfortunately it didn't work..

As I'm fine-tuning the LM on bert-multilingual, I can't try it out with other models. However I have tried to load all the different BERT huggingface-sub-models using my fine-tuned language model and it seems it is only TFBertModel and TFBertForMaskedLM it will load?

Hope that can lead you in a direction?


```
import transformers
model_dir = 'bertlm_model/'
config = transformers.BertConfig.from_json_file(model_dir + 'config.json')
```

### TFBertModel (works fine)
```
>>> model = transformers.TFBertModel.from_pretrained(model_dir, from_pt = True, config = config)
>>> 
```

### TFBertForPreTraining (won't load)
```
>>> model = transformers.TFBertForPreTraining.from_pretrained(model_dir, from_pt = True, config = config)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
    return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
    return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 145, in load_pytorch_weights_in_tf2_model
    assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
AssertionError: cls.seq_relationship.weight not found in PyTorch model
>>> 
```

### TFBertForMaskedLM (works fine)
```
>>> model = transformers.TFBertForMaskedLM.from_pretrained(model_dir, from_pt = True, config = config)
>>> 
```

### TFBertForNextSentencePrediction (won't load)
```
>>> model = transformers.TFBertForNextSentencePrediction.from_pretrained(model_dir, from_pt = True, config = config)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
    return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
    return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 145, in load_pytorch_weights_in_tf2_model
    assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
AssertionError: cls.seq_relationship.weight not found in PyTorch model
>>> 
```

### TFBertForSequenceClassification (won't load)
```
>>> model = transformers.TFBertForSequenceClassification.from_pretrained(model_dir, from_pt = True, config = config)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
    return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
    return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 145, in load_pytorch_weights_in_tf2_model
    assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
AssertionError: classifier.weight not found in PyTorch model
>>> 
```

### TFBertForMultipleChoice (won't load)
```
>>> model = transformers.TFBertForMultipleChoice.from_pretrained(model_dir, from_pt = True, config = config)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
    return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
    return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 109, in load_pytorch_weights_in_tf2_model
    tfo = tf_model(tf_inputs, training=False)  # Make sure model is built
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 822, in __call__
    outputs = self.call(cast_inputs, *args, **kwargs)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_bert.py", line 943, in call
    seq_length = shape_list(input_ids)[2]
IndexError: list index out of range
>>> 
```

### TFBertForTokenClassification (won't load)
```
>>> model = transformers.TFBertForTokenClassification.from_pretrained(model_dir, from_pt = True, config = config)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
    return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
    return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 145, in load_pytorch_weights_in_tf2_model
    assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
AssertionError: classifier.weight not found in PyTorch model
>>> 
```

### TFBertForQuestionAnswering (won't load)
```
>>> model = transformers.TFBertForQuestionAnswering.from_pretrained(model_dir, from_pt = True, config = config)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
    return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
    return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
  File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 145, in load_pytorch_weights_in_tf2_model
    assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
AssertionError: qa_outputs.weight not found in PyTorch model
>>> 
```

The same pattern of working (e.g. _TFBertForMaskedLM_) vs not working (e.g. _TFBertForQuestionAnswering_) appears also with the PyTorch version of these models? e.g. _BertForMaskedLM_

> Thanks for your answer - unfortunately it didn't work..
> 
> As I'm fine-tuning the LM on bert-multilingual, I can't try it out with other models. However I have tried to load all the different BERT huggingface-sub-models using my fine-tuned language model and it seems it is only TFBertModel and TFBertForMaskedLM it will load?
> 
> Hope that can lead you in a direction?
> 
> ```
> import transformers
> model_dir = 'bertlm_model/'
> config = transformers.BertConfig.from_json_file(model_dir + 'config.json')
> ```
> 
> ### TFBertModel (works fine)
> ```
> >>> model = transformers.TFBertModel.from_pretrained(model_dir, from_pt = True, config = config)
> >>> 
> ```
> 
> ### TFBertForPreTraining (won't load)
> ```
> >>> model = transformers.TFBertForPreTraining.from_pretrained(model_dir, from_pt = True, config = config)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
>     return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
>     return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 145, in load_pytorch_weights_in_tf2_model
>     assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
> AssertionError: cls.seq_relationship.weight not found in PyTorch model
> >>> 
> ```
> 
> ### TFBertForMaskedLM (works fine)
> ```
> >>> model = transformers.TFBertForMaskedLM.from_pretrained(model_dir, from_pt = True, config = config)
> >>> 
> ```
> 
> ### TFBertForNextSentencePrediction (won't load)
> ```
> >>> model = transformers.TFBertForNextSentencePrediction.from_pretrained(model_dir, from_pt = True, config = config)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
>     return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
>     return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 145, in load_pytorch_weights_in_tf2_model
>     assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
> AssertionError: cls.seq_relationship.weight not found in PyTorch model
> >>> 
> ```
> 
> ### TFBertForSequenceClassification (won't load)
> ```
> >>> model = transformers.TFBertForSequenceClassification.from_pretrained(model_dir, from_pt = True, config = config)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
>     return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
>     return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 145, in load_pytorch_weights_in_tf2_model
>     assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
> AssertionError: classifier.weight not found in PyTorch model
> >>> 
> ```
> 
> ### TFBertForMultipleChoice (won't load)
> ```
> >>> model = transformers.TFBertForMultipleChoice.from_pretrained(model_dir, from_pt = True, config = config)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
>     return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
>     return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 109, in load_pytorch_weights_in_tf2_model
>     tfo = tf_model(tf_inputs, training=False)  # Make sure model is built
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 822, in __call__
>     outputs = self.call(cast_inputs, *args, **kwargs)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_bert.py", line 943, in call
>     seq_length = shape_list(input_ids)[2]
> IndexError: list index out of range
> >>> 
> ```
> 
> ### TFBertForTokenClassification (won't load)
> ```
> >>> model = transformers.TFBertForTokenClassification.from_pretrained(model_dir, from_pt = True, config = config)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
>     return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
>     return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 145, in load_pytorch_weights_in_tf2_model
>     assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
> AssertionError: classifier.weight not found in PyTorch model
> >>> 
> ```
> 
> ### TFBertForQuestionAnswering (won't load)
> ```
> >>> model = transformers.TFBertForQuestionAnswering.from_pretrained(model_dir, from_pt = True, config = config)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 288, in from_pretrained
>     return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 82, in load_pytorch_checkpoint_in_tf2_model
>     return load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=tf_inputs, allow_missing_keys=allow_missing_keys)
>   File "/home/myuser/.local/share/virtualenvs/BERTlm-giwpujkO/lib/python3.7/site-packages/transformers/modeling_tf_pytorch_utils.py", line 145, in load_pytorch_weights_in_tf2_model
>     assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
> AssertionError: qa_outputs.weight not found in PyTorch model
> >>> 
> ```

All models load fine using the PyTorch version. So it is only some of the TF versions that are not working..


```
>>> model = transformers.BertModel.from_pretrained(model_dir, config = config)
>>> model = transformers.BertForPreTraining.from_pretrained(model_dir, config = config)
>>> model = transformers.BertForMaskedLM.from_pretrained(model_dir, config = config)
>>> model = transformers.BertForNextSentencePrediction.from_pretrained(model_dir, config = config)
>>> model = transformers.BertForSequenceClassification.from_pretrained(model_dir, config = config)
>>> model = transformers.BertForMultipleChoice.from_pretrained(model_dir, config = config)
>>> model = transformers.BertForTokenClassification.from_pretrained(model_dir, config = config)
>>> model = transformers.BertForQuestionAnswering.from_pretrained(model_dir, config = config)
>>> 
```

Hello! If I understand correctly, you fine-tuned a BERT model with a language modeling head (`BertForMaskedLM`), which was then saved and now you're trying to load it in TensorFlow.

You can load it with `TFBertModel` and `TFBertForMaskedLM` as the weights are there, but can't load it in other architectures as some weights are lacking. In PyTorch you can load them but it randomly initializes the lacking weights.

I believe we should have the same behavior between our TensorFlow models and our PyTorch models so I'll take a look at it. In the meantime, here's a workaround that will allow you to load the models in TensorFlow, for example from a `BertForMaskedLM` checkpoint to a `TFBertForSequenceClassification`:

- Save the `BertForMaskedLM` checkpoint
- Load it in `BertForSequenceClassification`
- Save the checkpoint from `BertForSequenceClassification`
- Load this checkpoint in `TFBertForSequenceClassification`

Here's an example that will allow you to do that, make sure the directories exist :

```py
from transformers import BertForMaskedLM, BertForSequenceClassification, TFBertForSequenceClassification

# This must have already been done by the script you used
model = BertForMaskedLM.from_pretrained("bert-base-cased")
model.save_pretrained("here")

# Load the saved checkpoint in a PyTorch BertForSequenceClassification model and save it
model = BertForSequenceClassification.from_pretrained("here")
model.save_pretrained("here-seq")

# Load the PyTorch model in the TF model of the same type
TFBertForSequenceClassification.from_pretrained("here-seq", from_pt=True)
```

Perfect - the workaround works - thanks a lot 👍 

And yes, that is sort of the procedure I've used. However I did't run the BertForMaskedLM directly but instead used the run_lm_finetuning.py script to generate my fine-tuned LM:

```
python run_lm_finetuning.py \
    --train_data_file=<pathToTrain.txt>\
    --output_dir=bertlm_model \
    --eval_data_file=<pathToTest.txt>\
    --model_type=bert \
    --model_name_or_path=bert-base-multilingual-cased \
    --mlm \
    --cache_dir=cache \
    --do_train \
    --do_eval \
    --per_gpu_train_batch_size=8\
    --per_gpu_eval_batch_size=8
```

And from there, I then try to load it with:
```
import transformers
model_dir = 'bertlm_model'

config = transformers.BertConfig.from_json_file(model_dir + '/config.json')
model = transformers.TFBertForSequenceClassification.from_pretrained(model_dir, from_pt = True, config = config)
```