Is this code correct to convert for loop matrix multiplication into einsum?

53 Views Asked by At

I have written a for loop based and einsum based code for matrix multiplication that i want to perform. Can you help me check its correctness ??

`

w = torch.randn((10,32,32))
x = torch.randn((3,32,32))
x_c = x.clone()
z = torch.zeros(x.shape)
for i in range(x.shape[0]):
  dummy_x = torch.zeros((x.shape[1],w.shape[2]))
  for j in range(w.shape[0]):
    dummy_x += torch.matmul(x[i],w[j])
  z[i]=dummy_x

result = torch.einsum("ijk,lkm->ijm",x_c,w)
# result = torch.einsum("iljm->ijm",result)
torch.eq(result,z)

I tried the above code and checked for equality using torch.eq but the answer was false

0

There are 0 best solutions below