#126The classifier head — FC + softmaxMedium

The classifier head — FC + softmax

Background

After the conv stack, a CNN flattens the features and runs a fully-connected layer followed by softmax to produce class probabilities, then predicts the top class:

z=fW+b,p=softmax(z),y^=argmaxkpk\mathbf{z} = \mathbf{f}^\top W + \mathbf{b}, \qquad \mathbf{p} = \text{softmax}(\mathbf{z}), \qquad \hat{y} = \arg\max_k p_k

Problem statement

Implement classify_head(features, W, b) returning (label, probs).

Input

  • features — array-like of length D, the flattened feature vector.
  • W — array-like of shape (D, K), the FC weight matrix (K classes).
  • b — array-like of length K, the bias.

Output

Returns (label, probs): label is the int predicted class; probs is an np.ndarray of K probabilities summing to 1.

Examples

Example 1

Input:  features = [1, 0], W = [[2, 0], [0, 2]], b = [0, 0]
Output: label = 0, probs ≈ [0.8808, 0.1192]

Explanation: logits =[2,0]= [2, 0]; softmax [0.88,0.12]\approx [0.88, 0.12]; argmax is class 0.

Constraints

  • Compute logits features @ W + b, softmax (numerically stable), then argmax.
  • Return (int label, np.ndarray probs) with probs summing to 1.

Notes

  • Subtract the max logit before exponentiating to avoid overflow — the standard stable softmax.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example: two-class head probabilities
  • Reference: predicted label when other class wins
  • Sample: three-class probabilities