sometimes loss starts with nan when running "Quick tour TF 2.0 training and PyTorch interoperability" script
Author: catqaq
Pull request: no
Labels: name: Ex: Sequence Classification

name: TensorFlow

# 🐛 Bug

## Information
The problem arises when using:
* [x] the official example scripts: (give details below)
"Quick tour TF 2.0 training and PyTorch interoperability"

## To reproduce

Steps to reproduce the behavior:

1.run the code:
```
import tensorflow as tf
import tensorflow_datasets
from transformers import *

# Load dataset, tokenizer, model from pretrained model/vocabulary
tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
model = TFBertForSequenceClassification.from_pretrained('bert-base-cased')
data = tensorflow_datasets.load('glue/mrpc')

# Prepare dataset for GLUE as a tf.data.Dataset instance
train_dataset = glue_convert_examples_to_features(data['train'], tokenizer, max_length=128, task='mrpc')
valid_dataset = glue_convert_examples_to_features(data['validation'], tokenizer, max_length=128, task='mrpc')
train_dataset = train_dataset.shuffle(100).batch(32).repeat(10)
valid_dataset = valid_dataset.batch(64)

# Prepare training: Compile tf.keras model with optimizer, loss and learning rate schedule
optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5, epsilon=1e-08, clipnorm=1.0)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
model.compile(optimizer=optimizer, loss=loss, metrics=[metric])

# Train and evaluate using tf.keras.Model.fit()
history = model.fit(train_dataset, epochs=10, steps_per_epoch=115,
                    validation_data=valid_dataset, validation_steps=7)
```
2. sometimes the loss is nan and sometimes it works well:
### nan case:
Train for 115 steps, validate for 7 steps
Epoch 1/10
115/115 [==============================] - 863s 8s/step - loss: nan - accuracy: 0.3255 - val_loss: nan - val_accuracy: 0.3162
Epoch 2/10
115/115 [==============================] - 854s 7s/step - loss: nan - accuracy: 0.3255 - val_loss: nan - val_accuracy: 0.3162

### normal case:
Train for 115 steps, validate for 7 steps
Epoch 1/10
 27/115 [======>.......................] - ETA: 11:37 - loss: 0.6249 - accuracy: 0.6609

## Environment info    
- `transformers` version: 2.9.1
- Platform:ubuntu 16.04
- Python version:3.6
- PyTorch version (GPU?):1.2.0 GPU
- Tensorflow version (GPU?):2.0.0 gpu
- Using GPU in script?:yes
- Using distributed or parallel set-up in script?:no


Comments:
I ran your script five times and I cannot reproduce this so it's very hard to debug. Can you clear the cache created by convert_examples and try again? And if that does not work, try updating tensorflow and tensorflow_datasets to the latest versions.

Thanks for your attention. As i said "sometimes", the script works well now. I did nothing except reboot my Spyder several times. I don’t know why, but it ’s back to normal

That's good to hear! It might have been a caching issue somewhere with Spyder. (Personally I can recommend PyCharm.)

Yes, I guess so. Thanks for your recommendation. I have been hesitating whether to abandon Spyder completely. PyCharm is powerful but a little too heavy...