Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autodiff doesn't work for * for AdjointTensorMap #157

Closed
Confusio opened this issue Oct 16, 2024 · 3 comments · Fixed by #161
Closed

Autodiff doesn't work for * for AdjointTensorMap #157

Confusio opened this issue Oct 16, 2024 · 3 comments · Fixed by #161

Comments

@Confusio
Copy link

Here is the example:

using TensorKit, Zygote
g(x)=real(scalar(x'*x))
V=Z3Space(0=>2,1=>2,2=>2);
A=randn(ComplexF64,V*V*V',one(V));
gradient(g,A)

Output:

ERROR: MethodError: no method matching *(::ChainRulesCore.Tangent{…}, ::TensorKit.AdjointTensorMap{…})
The function `*` exists, but no method is defined for this combination of argument types.
@Gertian
Copy link
Contributor

Gertian commented Oct 16, 2024

Firstly, are you running the latest version of TensorKit ?
I tried to run your code on the latest release and it already fails on ''A=randn(ComplexF64,VVV',one(V));'' which makes me doubt this.

So, assuming the latest release. And assuming you're trying to make a vector i.e. Tensor rather than a TensorMap we get :

A=Tensor(randnormal, ComplexF64,V*V*V');

To then answer your question. As far as I know AD methods are implemented for the @tensor macro. So let's rewrite your g(x) function using that :

using TensorKit, Zygote
g(x)=real(scalar(x'*x))
function g_patched(x) 
    return real(@tensor conj(x[1,2,3])*x[1,2,3])
end

which yields the same output.

But works nicely with AD :

V=Z3Space(0=>2,1=>2,2=>2);
A=Tensor(randnormal, ComplexF64,V*V*V');
gradient(g_patched, A)

Admittedly it might be useful to implement Chainrules that make your usecase work as well. Especially since this would be more flexible concerning inputs (I guess this also makes it harder, I haven't looked into it.).
This should get you going for now ?

@Confusio
Copy link
Author

I use the TensorKit version of [07d1fe3e] TensorKit v1.0.0-DEV ~/.julia/dev/TensorKit.
AD works for the @tensor macro; however, it fails with *. Sometimes, we just composite the linear maps. So I think AD for * is also essential.

@lkdvos
Copy link
Collaborator

lkdvos commented Jan 2, 2025

Will close this since it is now fixed in the latest releases.

@lkdvos lkdvos closed this as completed Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants