5 hidden checks
The three functions at the heart of every classifier, written so they never produce nan or inf.
sigmoid(z): element-wise on an array. It must not overflow for large negativez(trynp.exp(1000)). One approach:np.where(z >= 0, 1 / (1 + np.exp(-z)), np.exp(z) / (1 + np.exp(z))), but beware thatnp.whereevaluates both branches; clippingzto[-500, 500]first is a simple fix.bce(p, y): mean binary cross-entropy for probability arraypand 0/1 labelsy. Clippto[1e-12, 1 - 1e-12].softmax(Z): row-wise softmax of a 2-D array of logits. Subtract each row's max.
- sigmoid has the right valuesnot run
- sigmoid survives extreme inputsnot run
- bce matches the formula and stays finitenot run
- softmax rows sum to 1 and match the formulanot run
- softmax survives huge logitsnot run