I’m trying to simplify some code that does a lot of dropdims
and mapslices
, so I’m looking at some packages that do Einstein notation. I tried out Einsum.jl which seems pretty intuitive, but I also wanted to try out TensorOperations.jl, which seems to have higher performance and some bells and whistles like GPU support.
So I figured I’d try out TensorOperations but I’m getting an error that I don’t really understand.
Here’s a MWE for the operation I’m trying to do:
a = randn(3,4,5)
b = randn(5,3)
@tensor c[k, n] := a[k, n, c] * b[c,k]
I expected this to translate to something like:
c = [sum(a[k, n, :] .* b[:, k]) for k in 1:size(a,1), n in 1:size(a,2)]
Which is what @einsum
does. But this gives the error:
IndexError{String}("non-matching indices between left and right hand side: \$(Expr(:(:=), :(var\"##835\"[k, n]), :(var\"##833\"[k, n, c] * var\"##834\"[c, k])))")
Can anyone help me understand why this is valid for @einsum
but not @tensor
? My mental model (based on the Einsum
docs) is that variables introduced in the right-hand-side of an expression are summed over, but TensorOperations
seems to have more restrictions on what’s a valid expression.