Repository: huggingface/transformers

Issue:
Error in TFBertForSequenceClassification
## 🐛 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:




Discussion:
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)
```


Resolving patch:
diff --git a/transformers/modeling_tf_pytorch_utils.py b/transformers/modeling_tf_pytorch_utils.py
--- a/transformers/modeling_tf_pytorch_utils.py
+++ b/transformers/modeling_tf_pytorch_utils.py
@@ -143,7 +143,11 @@ def load_pytorch_weights_in_tf2_model(tf_model, pt_state_dict, tf_inputs=None, a
         name, transpose = convert_tf_weight_name_to_pt_weight_name(sw_name, start_prefix_to_remove=start_prefix_to_remove)
 
         # Find associated numpy array in pytorch model state dict
-        assert name in pt_state_dict, "{} not found in PyTorch model".format(name)
+        if name not in pt_state_dict:
+            if allow_missing_keys:
+                continue
+            raise AttributeError("{} not found in PyTorch model".format(name))
+
         array = pt_state_dict[name].numpy()
 
         if transpose:
@@ -250,6 +254,7 @@ def load_tf2_weights_in_pytorch_model(pt_model, tf_weights, allow_missing_keys=F
 
     all_tf_weights = set(list(tf_weights_map.keys()))
     loaded_pt_weights_data_ptr = {}
+    missing_keys_pt = []
     for pt_weight_name, pt_weight in current_pt_params_dict.items():
         # Handle PyTorch shared weight ()not duplicated in TF 2.0
         if pt_weight.data_ptr() in loaded_pt_weights_data_ptr:
@@ -258,7 +263,10 @@ def load_tf2_weights_in_pytorch_model(pt_model, tf_weights, allow_missing_keys=F
 
         # Find associated numpy array in pytorch model state dict
         if pt_weight_name not in tf_weights_map:
-            raise ValueError("{} not found in TF 2.0 model".format(pt_weight_name))
+            if allow_missing_keys:
+                missing_keys_pt.append(pt_weight_name)
+                continue
+            raise AttributeError("{} not found in TF 2.0 model".format(pt_weight_name))
 
         array, transpose = tf_weights_map[pt_weight_name]
 
@@ -283,6 +291,7 @@ def load_tf2_weights_in_pytorch_model(pt_model, tf_weights, allow_missing_keys=F
         all_tf_weights.discard(pt_weight_name)
 
     missing_keys, unexpected_keys = pt_model.load_state_dict(new_pt_params_dict, strict=False)
+    missing_keys += missing_keys_pt
 
     if len(missing_keys) > 0:
         logger.info("Weights of {} not initialized from TF 2.0 model: {}".format(
diff --git a/transformers/modeling_tf_utils.py b/transformers/modeling_tf_utils.py
--- a/transformers/modeling_tf_utils.py
+++ b/transformers/modeling_tf_utils.py
@@ -297,7 +297,7 @@ def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
 
         if from_pt:
             # Load from a PyTorch checkpoint
-            return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file)
+            return load_pytorch_checkpoint_in_tf2_model(model, resolved_archive_file, allow_missing_keys=True)
 
         ret = model(model.dummy_inputs, training=False)  # build the network with dummy inputs
 


Test patch: