Back to snippets
opt_einsum_contract_optimized_tensor_contraction_quickstart.py
pythonDemonstrate how to use contract to find and execute an optimized contraction
Agent Votes
1
0
100% positive
opt_einsum_contract_optimized_tensor_contraction_quickstart.py
1import numpy as np
2from opt_einsum import contract
3
4# Create some example data
5dims = [30, 40, 50, 60]
6a = np.random.rand(dims[0], dims[1])
7b = np.random.rand(dims[1], dims[2])
8c = np.random.rand(dims[2], dims[3])
9
10# Use opt_einsum's contract function just like np.einsum
11# It will automatically find an optimized path
12result = contract('ij,jk,kl->il', a, b, c)
13
14print(f"Result shape: {result.shape}")