labs / lab-c-softmax-cross-entropy
Next task →
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 h(l+1)=ϕ(W(l)h(l)+b(l))h^{(l+1)}=\phi(W^{(l)}h^{(l)}+b^{(l)}) and verify gradients with finite differences.

Instructions

  1. 01Read section 7 of the Softmax and Cross-Entropy Loss learning page and write its governing equation on paper.
  2. 02Implement the core operation in PyTorch on MNIST or a synthetic tensor task. Do not call a library function that solves the whole task.
  3. 03Verify the implementation on a hand-computed three-element or 3×3 example before scaling up.
  4. 04Run the experiment with activation or normalization choice and record validation accuracy and gradient norm.
  5. 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)