Attention Context Vector

Easy
~10 min
code completion

Attention Context Vector

After converting raw scores to attention weights (via softmax), the final attention output is a weighted sum of the value vectors:

  • has shape — attention weights; each row sums to 1
  • has shape — value matrix
  • has shape — context vectors
  • Each row is a convex combination of the rows of , weighted by how strongly query attended to each key.

    For a single query, may be a 1D vector of length , and the output is a 1D context vector of length .

    Your task:

    Implement attention_context(weights, V) that returns .

    Example Tests

    Single query attending fully to first value row

    Input: {"V":[[1,2],[3,4],[5,6]],"weights":[1,0,0]}

    Expected: [1,2]

    Equal weights over two value rows: midpoint

    Input: {"V":[[2,4],[6,8]],"weights":[0.5,0.5]}

    Expected: [4,6]

    Uniform weights: output is mean of value rows

    Input: {"V":[[3,6],[6,3],[9,3]],"weights":[0.3333333333333333,0.3333333333333333,0.3333333333333333]}

    Expected: [6,4]

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.