Flux.jl: Error while running sample from docs.

Created on 9 Feb 2019  Â·  6Comments  Â·  Source: FluxML/Flux.jl

Hello, I am new to Julia and Flux. I was running the sample code. I got the following error. I am using Julia 1.1.

julia> using Flux.Tracker


julia> f(x) = 3x^2 + 2x + 1
f (generic function with 1 method)

julia> df(x) = Tracker.gradient(f, x; nest = true)[1]
df (generic function with 1 method)

julia> df(2)
14.0 (tracked)

julia> d2f(x) = Tracker.gradient(df, x; nest = true)[1]
d2f (generic function with 1 method)

julia> d2f(2)
6.0 (tracked)

julia> f(W, b, x) = W * x + b
f (generic function with 2 methods)

julia> Tracker.gradient(f, 2, 3, 4)
(4.0 (tracked), 1.0 (tracked), 2.0 (tracked))

julia> W = param(2) 
2.0 (tracked)

julia> b = param(3) 
3.0 (tracked)

julia> f(x) = W * x + b
f (generic function with 2 methods)

julia> grads = Tracker.gradient(() -> f(4), params(W, b))
ERROR: UndefVarError: params not defined
Stacktrace:
 [1] top-level scope at none:0

Most helpful comment

Hello, I'm facing same issue when running a model from the docs (simple linear model)

using Flux


W = rand(2, 5)
b = rand(2)

predict(x) = W*x .+ b

function loss(x, y)
    y′ = predict(x)
    sum((y.-y′).^2)
end

x, y = rand(5), rand(2)
loss(x, y)

gs = gradient(()->loss(x, y), params(W, b))

it keep give me "params not defined"

All 6 comments

julia> using Flux

julia> grads = Tracker.gradient(() -> f(4), params(W, b))
Grads(...)

This makes it run. But is it intended to be like this?

You need to do using Flux, yeah. We can perhaps clarify the docs here.

Hello, I'm facing same issue when running a model from the docs (simple linear model)

using Flux


W = rand(2, 5)
b = rand(2)

predict(x) = W*x .+ b

function loss(x, y)
    y′ = predict(x)
    sum((y.-y′).^2)
end

x, y = rand(5), rand(2)
loss(x, y)

gs = gradient(()->loss(x, y), params(W, b))

it keep give me "params not defined"

Seems like replacing params by Flux.params works.

gs = gradient(() -> loss(x, y), Flux.params(W, b))

We could address it in the docs and qualify params appropriately.

I think params has been qualified in the docs

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aminya picture aminya  Â·  4Comments

KristofferC picture KristofferC  Â·  4Comments

tejank10 picture tejank10  Â·  6Comments

juliohm picture juliohm  Â·  5Comments

ExpandingMan picture ExpandingMan  Â·  6Comments