Skip to content

Instantly share code, notes, and snippets.

View iantimmis's full-sized avatar
🧑‍🍳
Cookin

Ian Timmis iantimmis

🧑‍🍳
Cookin
  • Michigan
View GitHub Profile
@iantimmis
iantimmis / softmax_cross_entropy.py
Created October 12, 2020 03:54
Numerically stable softmax with cross entropy in numpy
import numpy as np
def naive_softmax(logits):
'''
Failure modes:
* If any entry is very large, exp overflows
* if all entries are very negative, all exps underflow
'''
exp_logits = np.exp(logits)
return exp_logits / np.sum(exp_logits)