Using Symbolics and Flux

Let’s say I have something along the lines of

using Symbolics, Flux
@variables x,y
function f()
[x^2y^3 ; x+y^2]
grad_f(x,y) = Flux.gradient(transpose(f())*f(), x,y)

The following error comes:

ERROR: Sym x^(4(y^3)) + (x + y^2)^2 is not callable. Use @syms x^(4(y^3)) + (x + y^2)^2(var1, var2,…) to create it as a callable.

However it isn’t clear how I use “Use @syms x^(4(y^3)) + (x + y^2)^2(var1, var2,…)”. That is I am not sure where I include this or the syntactical formatting necessary.

Additionally if I use

grad_f(x,y) = Symbolics.gradient(transpose(f())*f(), [x,y])

method I always get a zero vector returned. Namely, it returns

2-element Vector{Num}:

I am afraid Symbolics and Flux are not compatible.

This is false given I just managed to get the following to work:

using Symbolics, Flux
@variables x,y,z
function ff(x,y,z)
grad_ff(x,y,z) = Flux.gradient(ff, x,y,z)

the output is:

(2x, 2y*z, y^2)

I found a fix, consider the following MWE

using Symbolics, Flux

@variables x, y
function f(x,y)
A = transpose([x;y])*[x;y]
return A[1]

This returns

x^2 + y^2

Then if I add


it returns

(2x, 2y)

Which is correct. Seems I was using incorrect syntax for what I wanted.

Note that Flux reexports gradient from Zygote.jl, so unless you need the NN functionality I’d recommend using that instead.

1 Like