Convert the encoder classification-token state into class logits. Select sequence position zero, apply LayerNorm over its embedding coordinates, and multiply by the supplied classification matrix.
μb=D1d=1∑Dhb,d. σb2=D1d=1∑D(hb,d−μb)2. hb,d=σb2+εhb,d−μb. logits=hWhead.Here, h is the token at position zero, B is batch size, D is embedding width, Whead is W_head, and ε=10−6. Return the logits as a float64 NumPy array with shape (B,C), where C is the second dimension of W_head.
Examples
Input: encoder_output = [[[4,2],[1,1],[0,0]]], W_head = [[1,0,1],[0,1,1]]
Output: [[1,-1,0]]
Explanation: LayerNorm is applied to token zero before W_head produces class logits.
Input: encoder_output = [[[2,2],[9,-9]]], W_head = [[1,0],[0,1]]
Output: [[0,0]]
Input: encoder_output = [[[6,2],[100,100]],[[1,3],[-100,-100]]], W_head = [[1,2],[-1,1]]
Output: [[2,1],[-1.999999,-1]]
Hint 1
Select encoder_output[:, 0, :] before normalizing.
Hint 2
Compute the mean and population variance along the last axis.
Hint 3
The number of output classes is W_head.shape[1].
Requirements
- Use NumPy.
- Select token position zero.
- Apply LayerNorm with population variance and epsilon 1e-6.
- Multiply the normalized state by W_head without an output activation.
- Return a float64 NumPy array with shape (B, C).
Constraints
- encoder_output has shape (B, N, D) and dtype float64.
- W_head has shape (D, C) and dtype float64.
- B, N, D, and C are positive.
All steps in ViT
Accepts: array
Accepts: array
Classification Head
Convert the encoder classification-token state into class logits. Select sequence position zero, apply LayerNorm over its embedding coordinates, and multiply by the supplied classification matrix.
μb=D1d=1∑Dhb,d. σb2=D1d=1∑D(hb,d−μb)2. hb,d=σb2+εhb,d−μb. logits=hWhead.Here, h is the token at position zero, B is batch size, D is embedding width, Whead is W_head, and ε=10−6. Return the logits as a float64 NumPy array with shape (B,C), where C is the second dimension of W_head.
Examples
Input: encoder_output = [[[4,2],[1,1],[0,0]]], W_head = [[1,0,1],[0,1,1]]
Output: [[1,-1,0]]
Explanation: LayerNorm is applied to token zero before W_head produces class logits.
Input: encoder_output = [[[2,2],[9,-9]]], W_head = [[1,0],[0,1]]
Output: [[0,0]]
Input: encoder_output = [[[6,2],[100,100]],[[1,3],[-100,-100]]], W_head = [[1,2],[-1,1]]
Output: [[2,1],[-1.999999,-1]]
Hint 1
Select encoder_output[:, 0, :] before normalizing.
Hint 2
Compute the mean and population variance along the last axis.
Hint 3
The number of output classes is W_head.shape[1].
Requirements
- Use NumPy.
- Select token position zero.
- Apply LayerNorm with population variance and epsilon 1e-6.
- Multiply the normalized state by W_head without an output activation.
- Return a float64 NumPy array with shape (B, C).
Constraints
- encoder_output has shape (B, N, D) and dtype float64.
- W_head has shape (D, C) and dtype float64.
- B, N, D, and C are positive.
All steps in ViT
Accepts: array
Accepts: array