Practical Labs/Lab: Softmax and Cross-Entropy Loss
Level 2 · deep-learning
Lab: Softmax and Cross-Entropy Loss
Build a minimal, verifiable implementation that demonstrates Softmax and Cross-Entropy Loss. Connect the code to the governing equation, inspect its output numerically, and explain one failure case.
Mathematical basis
For Softmax and Cross-Entropy Loss, compute the forward pass and verify gradients with finite differences.
Instructions
- 01Read section 7 of the Softmax and Cross-Entropy Loss learning page and write its governing equation on paper.
- 02Implement the core operation in PyTorch on MNIST or a synthetic tensor task. Do not call a library function that solves the whole task.
- 03Verify the implementation on a hand-computed three-element or 3×3 example before scaling up.
- 04Run the experiment with activation or normalization choice and record validation accuracy and gradient norm.
- 05Inspect the output, identify one discrepancy with the theory, and write a one-paragraph explanation.
Starter code
import numpy as np
def relu(x): return np.maximum(0, x)
x = np.array([-2., -1., 0., 1., 2.])
y = relu(x)
assert np.array_equal(y, [0,0,0,1,2])
print(y)