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]
end
grad_f(x,y) = Flux.gradient(transpose(f())*f(), x,y)
display(grad_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.

https://docs.juliahub.com/Symbolics/eABRO/3.4.0/tutorials/symbolic_functions/#Syms-and-callable-Syms-1

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}:
0
0

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)
x^2+y^2*z
end
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]
end
display(f(x,y))

This returns

x^2 + y^2

Then if I add

Flux.gradient(f,x,y)

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