← 返回 meta 的题目列表Implement Scaled Dot-Product Attention
类型:online_judge
Question: Implement Scaled Dot-Product Attention
Implement single-head scaled dot-product attention.
You are given three matrices:
Query matrix Q with shape n × d
Key matrix K with shape n × d
Value matrix V with shape n × d
Compute:
[ Attention(Q,K,V)=softmax\left(\frac{QK^T}{\sqrt d}\right)V ]
The softmax is applied independently to each row.
Input Format
n d
n rows of Q, each containing d floating-point numbers
n rows of K, each containing d floating-point numbers
n rows of V, each containing d floating-point numbers
Output Format
Output the attention result matrix with shape n × d.
Print d floating-point numbers per row, rounded to 6 decimal places.
Constraints
1 <= n <= 100
1 <= d <= 64
Input values are in [-10, 10]
Requirements
Correctly implement scaled dot-product attention.
Use a numerically stable softmax.
Explain the time and space complexity.
Example
Input:
2 2
1 0
0 1
1 0
0 1
1 2
3 4
Output:
1.660477 2.660477
2.339523 3.339523
Example
Input
1 1
2
3
4
Output
4.000000