# Using Symbolics and Flux

**URL:** https://discourse.julialang.org/t/using-symbolics-and-flux/89498
**Category:** Modelling & Simulations
**Created:** [October 29, 2022, 5:20pm UTC](https://discourse.julialang.org/t/using-symbolics-and-flux/89498 "2022-10-29T17:20:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)
#### Post date: [October 29, 2022, 5:20pm UTC](https://discourse.julialang.org/t/using-symbolics-and-flux/89498/1 "2022-10-29T17:20:23Z")

</div>

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](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

---

<div class="post-metadata">

### Author: ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)
#### Post date: [October 29, 2022, 6:01pm UTC](https://discourse.julialang.org/t/using-symbolics-and-flux/89498/2 "2022-10-29T18:01:53Z")

</div>

I am afraid Symbolics and Flux are not compatible.

---

<div class="post-metadata">

### Author: ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)
#### Post date: [October 29, 2022, 6:33pm UTC](https://discourse.julialang.org/t/using-symbolics-and-flux/89498/3 "2022-10-29T18:33:04Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)
#### Post date: [October 29, 2022, 6:48pm UTC](https://discourse.julialang.org/t/using-symbolics-and-flux/89498/4 "2022-10-29T18:48:21Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [October 29, 2022, 7:02pm UTC](https://discourse.julialang.org/t/using-symbolics-and-flux/89498/5 "2022-10-29T19:02:42Z")

</div>

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