YouTip LogoYouTip

Pytorch Torch Chain_Matmul

# Creating Multiple Matrices A = torch.randn(10,20) B = torch.randn(20,30) C = torch.randn(30,40) D = torch.randn(40,50) # Chain Matrix Multiplication result = torch.chain_matmul(A, B, C, D) print("Matrix A Shape:", A.shape) print("Matrix B Shape:", B.shape) print("Matrix C Shape:", C.shape) print("Matrix D Shape:", D.shape) print("Result Shape:", result.shape)

Output result:

Matrix A Shape: torch.Size([10, 20])Matrix B Shape: torch.Size([20, 30])Matrix C Shape: torch.Size([30, 40])Matrix D Shape: torch.Size([40, 50])Result Shape: torch.Size([10, 50])

Example - Using a List

import torch

# Matrix List

 matrices =[torch.randn(10,20),

 torch.randn(20,30),

 torch.randn(30,40)]

# Using Lists as Arguments

 result = torch.chain_matmul(*matrices)

print("Result Shape:", result.shape)

Pytorch torch Reference Pytorch torch Reference

← Pytorch Torch Cholesky_InversePytorch Torch Cdist β†’