NaN Gradients during Mixed Precision Training in Keras
0 reputation · 05 Nov 2023, 09:55 UTC
Resource Optimization via Mixed Precision
To reduce VRAM overhead and lower compute costs for low-traffic workloads, the Keras Mixed Precision API is used to execute compute-heavy operations in float16. This approach aims to minimize the memory footprint of activations, potentially allowing the use of smaller GPU instances.
Numerical Stability Constraints
While mixed precision increases throughput on compatible hardware, it introduces risks regarding numerical underflow. Keras utilizes a loss scaling mechanism to mitigate this, but certain architectural configurations can still lead to instability during gradient updates.
Specifically, there is a requirement to maintain a float32 output layer for final activations like softmax or sigmoid to ensure stability. However, the interaction between the loss scaler and specific custom loss functions can still result in NaN gradients.
- How does Keras handle loss scaling when integrating custom loss functions with the mixed_precision policy?
- What is the recommended verification method to determine if NaN gradients are caused by the loss scaler or an improperly typed output layer?