← 返回 apple 的题目列表Implement Self-Attention (Single-Head)
类型:online_judge
Problem: Implement Self-Attention (Single-Head)
Implement scaled dot-product self-attention (single head) in a Transformer.
Given:
sequence length n
dimension d
real matrices Q, K, V of shape n x d
optional attention mask M of shape n x n:
M[i][j] = 0 means allowed
M[i][j] = 1 means disallowed (set the corresponding logit to -inf before softmax)
Compute:
S = (Q * K^T) / sqrt(d)
If mask is provided, set S[i][j] = -inf wherever M[i][j] = 1
A = softmax(S) applied row-wise
Output O = A * V with shape n x d
Input (stdin)
First line: two integers n d Next n lines: matrix Q Next n lines: matrix K Next n lines: matrix V Next line: integer has_mask (0 or 1)
If has_mask = 1, next n lines: matrix M (0/1)
Output (stdout)
Print O as n lines, each with d floats rounded to 6 decimals.
Constraints
1 <= n <= 50
1 <= d <= 50
absolute value of inputs <= 10
Example
Input:
2 2
1 0
0 1
1 0
0 1
1 2
3 4
0
Output:
1.660476 2.660476
2.339524 3.339524
Example
Input
1 1
1
1
2
0
Output
2.000000