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+b Pi,c=∑k=1Cexp(Zi,k)exp(Zi,c) L=−B1i=1∑BlogPi,yiFor the softmax-cross-entropy gradient, subtract one from the probability of each true class and divide by B. Here, B is batch size, C is class count, and yi is the integer label for sample i. 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.
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.
All steps in BERT
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: number
Fine-tuning Architecture
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+b Pi,c=∑k=1Cexp(Zi,k)exp(Zi,c) L=−B1i=1∑BlogPi,yiFor the softmax-cross-entropy gradient, subtract one from the probability of each true class and divide by B. Here, B is batch size, C is class count, and yi is the integer label for sample i. 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.
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.
All steps in BERT
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: number