BertConfig.to_json_file('config.json') saves  "num_labels"  as  "_num_labels" on Google Colab
Author: gontcharovd
Pull request: no

# 🐛 Bug

There is a problem with config.to_json_file() on Google Colab.

## Information

The "num_labels" key pair is saved as "_num_labels" key in the config.json file produced by BertConfig.to_json_file('config.json').
When this file is subsequently read with BertConfig.from_json_file('config.json') the "_num_labels" is loaded along with a  "num_labels" key with default value 2. 
This results in an improper configuration of a model TFBertForSequenceClassification.from_pretrained().

Model I am using (Bert, XLNet ...): TFBertForSequenceClassification

Language I am using the model on (English, Chinese ...): bert-base-german-cased

The problem arises when using:
* [ ] the official example scripts: (give details below)
* [ x] my own modified scripts: (give details below)

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [x ] my own task or dataset: (give details below)

## To reproduce

1.  modify the number of labels of the default bert-base-german-cased config and save to file

``` python
from transformers import BertConfig, TFBertForSequenceClassification

NUM_LABELS = 3
config = BertConfig.from_pretrained('bert-base-german-cased')
config.num_labels = NUM_LABELS
config.to_json_file('config.json')
```
This is what config.json looks like, notice the "_num_labels" key:

```json
{
  "_num_labels": 3,
  "architectures": [
    "BertForMaskedLM"
  ],
  "attention_probs_dropout_prob": 0.1,
  "bad_words_ids": null,
  "bos_token_id": null,
  "decoder_start_token_id": null,
  "do_sample": false,
  "early_stopping": false,
  "eos_token_id": null,
  "finetuning_task": null,
  "hidden_act": "gelu",
  "hidden_dropout_prob": 0.1,
  "hidden_size": 768,
  "id2label": {
    "0": "LABEL_0",
    "1": "LABEL_1",
    "2": "LABEL_2"
  },
  "initializer_range": 0.02,
  "intermediate_size": 3072,
  "is_decoder": false,
  "is_encoder_decoder": false,
  "label2id": {
    "LABEL_0": 0,
    "LABEL_1": 1,
    "LABEL_2": 2
  },
  "layer_norm_eps": 1e-12,
  "length_penalty": 1.0,
  "max_length": 20,
  "max_position_embeddings": 512,
  "min_length": 0,
  "model_type": "bert",
  "no_repeat_ngram_size": 0,
  "num_attention_heads": 12,
  "num_beams": 1,
  "num_hidden_layers": 12,
  "num_return_sequences": 1,
  "output_attentions": false,
  "output_hidden_states": false,
  "output_past": true,
  "pad_token_id": 0,
  "prefix": null,
  "pruned_heads": {},
  "repetition_penalty": 1.0,
  "task_specific_params": null,
  "temperature": 1.0,
  "top_k": 50,
  "top_p": 1.0,
  "torchscript": false,
  "type_vocab_size": 2,
  "use_bfloat16": false,
  "vocab_size": 30000
}
```
2. read the saved config

``` python
config = BertConfig.from_json_file('config.json')
```
this is what the loaded config.json looks like:

```json
{
  "_num_labels": 3,
  "architectures": [
    "BertForMaskedLM"
  ],
  "attention_probs_dropout_prob": 0.1,
  "bad_words_ids": null,
  "bos_token_id": null,
  "decoder_start_token_id": null,
  "do_sample": false,
  "early_stopping": false,
  "eos_token_id": null,
  "finetuning_task": null,
  "hidden_act": "gelu",
  "hidden_dropout_prob": 0.1,
  "hidden_size": 768,
  "id2label": {
    "0": "LABEL_0",
    "1": "LABEL_1",
    "2": "LABEL_2"
  },
  "initializer_range": 0.02,
  "intermediate_size": 3072,
  "is_decoder": false,
  "is_encoder_decoder": false,
  "label2id": {
    "LABEL_0": 0,
    "LABEL_1": 1,
    "LABEL_2": 2
  },
  "layer_norm_eps": 1e-12,
  "length_penalty": 1.0,
  "max_length": 20,
  "max_position_embeddings": 512,
  "min_length": 0,
  "model_type": "bert",
  "no_repeat_ngram_size": 0,
  "num_attention_heads": 12,
  "num_beams": 1,
  "num_hidden_layers": 12,
  "num_labels": 2,
  "num_return_sequences": 1,
  "output_attentions": false,
  "output_hidden_states": false,
  "output_past": true,
  "pad_token_id": 0,
  "prefix": null,
  "pruned_heads": {},
  "repetition_penalty": 1.0,
  "task_specific_params": null,
  "temperature": 1.0,
  "top_k": 50,
  "top_p": 1.0,
  "torchscript": false,
  "type_vocab_size": 2,
  "use_bfloat16": false,
  "vocab_size": 30000
}
```

3. the loaded config improperly configures the number of labels of a model:

```python
model = TFBertForSequenceClassification.from_pretrained(
    './model/bert_de/weights.h5', config=config
)
```
```
>>> model  = TFBertForSequenceClassification.from_pretrained(
...     './model/bert_de/weights.h5', config=config
... )
2020-04-24 16:20:44.590400: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/gontcharovd/anaconda3/envs/schwarz/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 274, in from_pretrained
    model.load_weights(resolved_archive_file, by_name=True)
  File "/home/gontcharovd/anaconda3/envs/schwarz/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 234, in load_weights
    return super(Model, self).load_weights(filepath, by_name, skip_mismatch)
  File "/home/gontcharovd/anaconda3/envs/schwarz/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py", line 1220, in load_weights
    f, self.layers, skip_mismatch=skip_mismatch)
  File "/home/gontcharovd/anaconda3/envs/schwarz/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 777, in load_weights_from_hdf5_group_by_name
    str(weight_values[i].shape) + '.')
ValueError: Layer #2 (named "classifier"), weight <tf.Variable 'tf_bert_for_sequence_classification/classifier/kernel:0' shape=(768, 2) dtype=float32, numpy=
array([[ 0.00399719,  0.01253725],
       [ 0.00453608, -0.00098394],
       [ 0.01605183, -0.02316079],
       ...,
       [-0.0174976 ,  0.00032987],
       [-0.01292989,  0.00867058],
       [-0.02766422, -0.00422863]], dtype=float32)> has shape (768, 2), but the saved weight has shape (768, 3).
```


I load the model with saved weights after transfer learning starting from bert-base-german-cased.
The weights were saved by ModelCheckpoint:

```python
mc = ModelCheckpoint('./model/bert_de/weights.h5', monitor='val_loss', mode='min',
                     verbose=1, save_best_only=True, save_weights_only=True)
```

## Expected behavior

The correct number of labels = 3 should be read from the config.json file and  not the default 2.

```python
model_de = TFBertForSequenceClassification.from_pretrained(
    './model/bert_de/weights.h5', config=config_de
)
```

A hack for this problem is to specify the num_labels again after reading config.json:

```python
config = BertConfig.from_json_file('config.json')
config.num_labels = NUM_LABELS
mode = TFBertForSequenceClassification.from_pretrained(
    './model/bert_de/weights.h5', config=config
)
```

## Environment info
This happens on Google Colab. On my local machine I don't get the key "_num_labels".

Comments:
Hello, I believe there's a mismatch between the versions on your computer and on Google colab. When running on master, saving the file as you did:

```py
from transformers import BertConfig

NUM_LABELS = 3
config = BertConfig.from_pretrained('bert-base-german-cased')
config.num_labels = NUM_LABELS
config.to_json_file('config.json')
```

when reloading it in the same environment and printing the number of labels:
```py
from transformers import BertConfig

config = BertConfig.from_pretrained('config.json')
print(config.num_labels)  # 3

config = BertConfig.from_json_file("config.json")
print(config.num_labels)  # 3
```

This happens since https://github.com/huggingface/transformers/pull/3147, which is necessary in order for the correct `id2label` to be instantiated. Please let me know if updating the version on your local machine does not solve the problem.

Hello, the transformers version on my computer was 2.1.1 and on Google Colab 2.8.0. Upgrading to the newest version solved the issue for me. Thanks!