Problems
Loading...
Step 6 of 6
Fine-tuning Architecture
Hard

Implement one gradient-descent step for a BERT sequence-classification head. Treat the supplied hidden states as the output of a pre-trained encoder and update only the classifier parameters. Use the hidden state at sequence position zero for every sample.

Z=H:,0,:W+bZ = H_{:,0,:}W + b Pi,c=exp⁡(Zi,c)∑k=1Cexp⁡(Zi,k)P_{i,c} = \frac{\exp(Z_{i,c})}{\sum_{k=1}^{C}\exp(Z_{i,k})} L=−1B∑i=1Blog⁡Pi,yiL = -\frac{1}{B}\sum_{i=1}^{B}\log P_{i,y_i}

For the softmax-cross-entropy gradient, subtract one from the probability of each true class and divide by BB. Here, BB is batch size, CC is class count, and yiy_i is the integer label for sample ii. Return a Python dictionary with exactly new_classifier_W, new_classifier_b, and loss. The updated parameters must be float64 NumPy arrays, and loss must be a Python float measured before the update.

Loading visualization...

Examples

Input: hidden_states = [[[1,0],[0.2,0.3]],[[0,1],[-0.1,0.4]]], labels = [0,1], classifier_W = [[0.2,-0.1],[-0.3,0.4]], classifier_b = [0.05,-0.05], learning_rate = 0.1

Output: {"new_classifier_W":[[0.220066,-0.120066],[-0.317717,0.417717]],"new_classifier_b":[0.052348,-0.052348],"loss":0.475252}

Explanation: Only the classification-token states contribute to the softmax loss and the classifier update.

Input: hidden_states = [[[0.5,-0.5,1],[9,9,9]],[[-1,0.25,0.5],[8,8,8]],[[0.2,0.1,-0.3],[7,7,7]]], labels = [2,0,1], classifier_W = [[0.1,-0.2,0.3],[0.4,0.1,-0.1],[-0.3,0.2,0.5]], classifier_b = [0,0.1,-0.1], learning_rate = 0.05

Output: {"new_classifier_W":[[0.085013,-0.192573,0.30756],[0.403876,0.101804,-0.10568],[-0.294879,0.187395,0.507484]],"new_classifier_b":[0.002816,0.097798,-0.100613],"loss":1.031599}

Input: hidden_states = [[[1,-1],[4,4]]], labels = [1], classifier_W = [[0,0],[0,0]], classifier_b = [0,0], learning_rate = 0.2

Output: {"new_classifier_W":[[-0.1,0.1],[0.1,-0.1]],"new_classifier_b":[-0.1,0.1],"loss":0.693147}

Hint 1

Subtract each row maximum before exponentiating logits.

Hint 2

Set grad_logits[np.arange(batch_size), labels] -= 1.

Hint 3

Use cls_states.T @ grad_logits for the weight gradient.

Requirements

  • Use NumPy.
  • Select the position-zero state from each sequence.
  • Compute numerically stable softmax probabilities and mean cross-entropy.
  • Compute analytical gradients for the classifier weights and bias.
  • Apply exactly one gradient-descent update.
  • Return exactly the documented arrays and pre-update loss.

Constraints

  • hidden_states has shape (B, S, D) and dtype float64.
  • labels has shape (B), dtype int64, and values from zero through C minus one.
  • classifier_W has shape (D, C) and dtype float64.
  • classifier_b has shape (C) and dtype float64.
  • learning_rate is a positive float.
  • B, S, D, and C are positive.