Jutho / TensorOperations.jl

Julia package for tensor contractions and related operations
https://jutho.github.io/TensorOperations.jl/stable/
Other
443 stars 56 forks source link

How to perform batch matmul `aij,ajk->aik`? #99

Closed Neutron3529 closed 3 years ago

Neutron3529 commented 3 years ago
#import Pkg;Pkg.add("TensorOperations");Pkg.add("Einsum");
using TensorOperations
using Einsum
A=rand(3,4,5);
B=rand(3,5,6);
@einsum C[a,i,k]:=A[a,i,j]*B[a,j,k] # OK
@tensor C[a,i,k]:=A[a,i,j]*B[a,j,k] # ERROR: TensorOperations.IndexError{String}("non-matching indices between left and right hand side: \$(Expr(:(:=), :(var\"##257\"[a, i, k]), :(var\"##255\"[a, i, j] * var\"##256\"[a, j, k])))")

what I could do to fix that error? (the reason that I use TensorOperations rather than Einsum is that Einsum is slower than TensorOperations(at least in some demos))

Jutho commented 3 years ago

You seem to be interested in batched matrix multiplication. TensorOperations.jl is currently restricted to tensor contractions that can be mapped to normal matrix multiplication, corresponding to strict Einstein summation convention, i.e. indices that appear twice in the right hand side are contracted over and cannot appear in the left hand side.

Maybe Tullio.jl can be of help to you.

Neutron3529 commented 3 years ago

You seem to be interested in batched matrix multiplication. TensorOperations.jl is currently restricted to tensor contractions that can be mapped to normal matrix multiplication, corresponding to strict Einstein summation convention, i.e. indices that appear twice in the right hand side are contracted over and cannot appear in the left hand side.

Maybe Tullio.jl can be of help to you.

Thank you for your reply. Previously, I was using R and meet some performance issues. I thought using a single instruction may provide better performance since it is true in R. But for Julia, maybe using loop is also an acceptable choice.

what I want to do could be: a=[rand(10,10) for i in 1:5] b=[rand(10,5) for b in 1:5] a.*b

which is faster enough.