Loading...
Use torch.einsum to perform matrix multiplication.
Einstein summation (einsum) is a compact notation for tensor operations. The key idea is that repeated indices are summed over, while indices that appear in the output are preserved.
The starter code creates matrices A (2x3) and B (3x2). Use torch.einsum to compute the matrix product of A and B.
The verification against torch.matmul is pre-filled.
Returns a dictionary with "result", "shape", and "matches_matmul".
None
{'result': [[58.0, 64.0], [139.0, 154.0]], 'shape': [2, 2], 'matches_matmul': True}[[1, 2, 3], [4, 5, 6]] and matrix B = [[7, 8], [9, 10], [11, 12]].torch.einsum('ij,jk->ik', A, B), which performs matrix multiplication by summing over the shared index j: Cik=∑jAijBjk.torch.matmul(A, B), which yields the same result, so "matches_matmul" is True.