Flux gradient not defined

Hi, I’m attempting to build a basic perceptron using Flux, and starting with the tutorial code in the documentation here [Gradients and Layers · Flux].

Entering this code from the tutorial

using Flux
f(x) = 3x^2 + 2x + 1;
df(x) = gradient(f, x)[1];

results in this error:

UndefVarError: gradient not defined

Stacktrace:
[1] df(::Int64) at ./In[10]:3
[2] top-level scope at In[10]:4

How do I get the tutorial code to run correctly?

Many thanks!

What does using Pkg; Pkg.status("Flux") show?

1 Like
Status `~/.julia/environments/v1.4/Project.toml`
  [587475ba] Flux v0.9.0

I installed this morning with

] add Flux

try ]up The newest version of flux is 0.11.0 and 0.10.0 was a relatively breaking release

Thank you, that worked. I’d assumed installing would install latest version, sigh.

Adding a package adds the most recent version compatible with your current packages (to try to prevent breakage). I’m not 100% sure if that’s the best solution, but I kind of like it.

Wait I’m confused - doesn’t ]up also restrict me to the “most recent version compatible with current packages”, i.e. run the same resolver? Are you saying ]add and ]up will pick different package versions for the same environment?

The difference is that pkg"up" will also upgrade all packages. If you have old versions of other packages that depend upon Flux, they might not be compatible with the newest release (and indeed likely weren’t).

3 Likes