PIXELBANKv9.1.0
Menu

Implement Bilinear Form with Einsum

Problem Statement

Use einsum to compute a batched bilinear form x^T A y.

Background

A bilinear form computes x^T A y where x and y are vectors and A is a matrix. With einsum, you can express this contraction over multiple indices in a single call.

Your Task

The starter code creates x (3x4), A (4x4), and y (3x4). Use einsum to compute the batched bilinear form x^T A y, producing one scalar per batch element.

Output Format

Returns a dictionary with "results" (one scalar per batch), "shape", and "first_result".

Example:

Input:
None
Output:
{'results': [1.0, 6.0, 136.0], 'shape': [3], 'first_result': 1.0}
Reasoning:
  • We start with the given input values for x, A, and y: x = [[1,0,0,0],[0,1,0,0],[1,1,1,1]], A = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], and y = [[1,0,0,0],[0,1,0,0],[1,1,1,1]].
  • We compute the batched bilinear form using torch.einsum('bi,ij,bj->b', x, A, y), which calculates xbTAybx_b^T A y_b for each batch bb. This results in three separate calculations: x0TAy0=[1,0,0,0]â‹…[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]â‹…[1,0,0,0]=1.0x_0^T A y_0 = [1,0,0,0] \cdot [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] \cdot [1,0,0,0] = 1.0, x1TAy1=[0,1,0,0]â‹…[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]â‹…[0,1,0,0]=6.0x_1^T A y_1 = [0,1,0,0] \cdot [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] \cdot [0,1,0,0] = 6.0, and x2TAy2=[1,1,1,1]â‹…[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]â‹…[1,1,1,1]=136.0x_2^T A y_2 = [1,1,1,1] \cdot [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] \cdot [1,1,1,1] = 136.0.
  • The results of these calculations are collected into a list: [1.0, 6.0, 136.0].
  • The final output is a dictionary containing the results, the shape of the results, and the first result: {'results': [1.0, 6.0, 136.0], 'shape': [3], 'first_result': 1.0}.

Constraints:

  • Use 'bi,ij,bj->b' einsum notation
  • Batched computation
  • Fixed input values for determinism
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Implement Bilinear Form with Einsum - Hard | PixelBank