← 返回 amazon 的题目列表Implement Scaled Dot-Product Attention
类型:online_judge
Problem: Implement Scaled Dot-Product Attention
Implement single-head, unmasked scaled dot-product attention:
[ \operatorname{Attention}(Q,K,V)=\operatorname{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V ]
Where:
Q is a query matrix of shape q × d
K is a key matrix of shape k × d
V is a value matrix of shape k × dv
Softmax is computed independently for each row of attention scores. Print every output value rounded to 6 decimal places.
Input Format
First line: four integers q k d dv
Next q lines: matrix Q, each with d floating-point values
Next k lines: matrix K, each with d floating-point values
Next k lines: matrix V, each with dv floating-point values
Output Format
Print q lines. Each line contains dv floating-point values separated by spaces, representing the attention output matrix.
Constraints
1 <= q, k <= 200
1 <= d, dv <= 128
Matrix values are in [-100, 100]
Example
Input
1 2 2 2
1 0
1 0
0 1
10 0
0 20
Output
6.697616 6.604767
Example
Input
1 2 2 2
1 0
1 0
0 1
10 0
0 20
Output
6.697616 6.604767