Bug? NaN loss after training for a while using for BERT Encoded sentences.
Author: codeninja
Pull request: no
Labels: name: wontfix

# ❓ Questions & Help

<!-- The GitHub issue tracker is primarly intended for bugs, feature requests,
     new models and benchmarks, and migration questions. For all other questions,
     we direct you to Stack Overflow (SO) where a whole community of PyTorch and
     Tensorflow enthusiast can help you out. Make sure to tag your question with the
     right deep learning framework as well as the huggingface-transformers tag: 
     https://stackoverflow.com/questions/tagged/huggingface-transformers 
     
     If your question wasn't answered after a period of time on Stack Overflow, you
     can always open a question on GitHub. You should then link to the SO question 
     that you posted.
     -->

## Details
<!-- Description of your issue -->
I have a model which is taken from the HF Examples and slightly modified.

```
from transformers import TFBertModel, TFBertForSequenceClassification, BertTokenizer
# configuration = BertConfig()
def build_bert(batch_size=1, use_logits=True):
    num_labels = max(max(train_label), max(test_label))
    print(f"Number of labels: {num_labels}")

    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-5, epsilon=1e-06, clipnorm=1.0)

    if num_labels == 1:
        loss = tf.keras.losses.MeanSquaredError()
    else:
        loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=use_logits)

    print(f"loss used: {loss}")
    macc = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
    vacc = tf.keras.metrics.SparseCategoricalAccuracy('val_accuracy')

    config = BertConfig.from_pretrained("bert-base-cased", num_labels=num_labels, batch_size=batch_size)
    bert_model = TFBertForSequenceClassification.from_pretrained("bert-base-cased", config=config)
    bert_model.compile(optimizer=optimizer, loss=loss, metrics=[macc, vacc])
    return bert_model
```

I am training on data which is encoded by BERT into 48 token arrays and encoded with the HF Bert Encoder.

```bert_model = build_bert(1000, False)
bert_model.fit([encodings[30000:40000], train_attn_mask[30000:40000]], classes[30000:40000], 
               epochs=1, validation_split=.1, shuffle=False)
```

![image](https://user-images.githubusercontent.com/14914/77113444-d683f700-69f8-11ea-8f4e-b9991e777b16.png)

My model will train for a while (Please pardon the output... I have no idea why jupyter lab does this.)
![image](https://user-images.githubusercontent.com/14914/77113488-ee5b7b00-69f8-11ea-9029-fdda4ec26a31.png)

Then at some point (different every run) the loss drops to NaN.
![image](https://user-images.githubusercontent.com/14914/77113529-016e4b00-69f9-11ea-94d4-010388ff0f35.png)



<!-- You should first ask your question on SO, and only if
     you didn't get an answer ask it here on GitHub. -->
**A link to original question on Stack Overflow**: 
The solution for this problem on SO is varied. I tried changing the optimizer learning rates as well as altering the epsilon. I have validated that my data does not contain nan values, negative classifications,  or invalid encodings. I have removed all non unicode characters.


My concern is that this has uncovered a bug with in the framework. 


Comments:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.


Do you solve the problem?