It is two in the morning. Your model has been training for six hours, and the loss has been sliding down all evening.
Then one batch of data produces a gradient hundreds of times bigger than anything seen so far. The optimizer does exactly what it was told to do: it takes a step proportional to that gradient. The parameters are flung clear across the landscape.
The next loss printed is NaN. Six hours of progress, wiped out by a single update.
This is a runaway truck on a mountain road. And the fix is the same kind of fix a truck gets: nobody redesigns the engine on the hillside. They fit a limiter so it cannot go faster than it is allowed to.

A runaway truck careering down a steep mountain road