In a Transformer decoder with causal (masked) self-attention, the model is prevented from attending to future positions. This means that for position 2, it can only attend to positions 0, 1, and 2, but not to position 3. The causal mask sets the attention scores for future positions (in this case, position 3) to (-\infty).
Given the attention scores for position 2:
[0.3, 0.7, 0.9, 0.5],
the causal mask will set the score for position 3 to (-\infty). Thus, the scores become:
[0.3, 0.7, 0.9, -\infty].
After applying the softmax function, the weight for position 3 will be 0 (since (-\infty) becomes 0 after softmax), and the remaining weights will be normalized based on the scores for positions 0, 1, and 2.
Let's compute the softmax for the valid scores:
[
\text{Softmax}(x_i) = \frac{e^{x_i}}{\sum_{j=0}^{2} e^{x_j}}
]
where (x = [0.3, 0.7, 0.9]).
First, compute the exponentials:
[
e^{0.3} \approx 1.3499, \quad e^{0.7} \approx 2.0138, \quad e^{0.9} \approx 2.4596
]
Next, compute the sum:
[
\sum_{j=0}^{2} e^{x_j} = 1.3499 + 2.0138 + 2.4596 \approx 5.8233
]
Now, compute the softmax values:
[
\text{Softmax}(0.3) = \frac{1.3499}{5.8233} \approx 0.2318
]
[
\text{Softmax}(0.7) = \frac{2.0138}{5.8233} \approx 0.3458
]
[
\text{Softmax}(0.9) = \frac{2.4596}{5.8233} \approx 0.4224
]
Thus, the attention weights after applying the causal mask and softmax are:
[0.2318, 0.3458, 0.4224, 0].
The correct answer is:
[0.2318, 0.3458, 0.4224, 0].