# How can I go about updating scalar parameters in Flux.jl?

**URL:** https://discourse.julialang.org/t/how-can-i-go-about-updating-scalar-parameters-in-flux-jl/19146
**Category:** New to Julia
**Tags:** question, differentiation, flux
**Created:** [December 31, 2018, 12:08pm UTC](https://discourse.julialang.org/t/how-can-i-go-about-updating-scalar-parameters-in-flux-jl/19146 "2018-12-31T12:08:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)
#### Post date: [December 31, 2018, 12:08pm UTC](https://discourse.julialang.org/t/how-can-i-go-about-updating-scalar-parameters-in-flux-jl/19146/1 "2018-12-31T12:08:22Z")

</div>

Hey everyone and happy holidays,

I’ve been playing around with Flux.jl as I would like to base my new Bayesian deep learning package on this framework. When running the examples from the [documentation](https://fluxml.ai/Flux.jl/stable/models/basics.html) I noticed something I couldn’t wrap my head around.

The first example in the “Basic usage” documentation in Flux which uses the `update!` function looks something like this

```julia
using Flux
using Flux.Tracker
using Flux.Tracker: update!

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

predict(x) = W*x .+ b
loss(x, y) = sum((y .- predict(x)).^2)

x, y = rand(5), rand(2) # Dummy data
pars = Params([W, b])
grads = Tracker.gradient(() -> loss(x, y), pars)

update!(W, -0.1*grads[W])
loss(x, y)

```

which works as expected. So far so good. Now however, moving back to the example before in the same section I would like to try to update the parameters of that model and that’s where I fail. The following code shows the issue.

```julia
using Flux
using Flux.Tracker
using Flux.Tracker: update!

W, b = param(2), param(3)

predict(x) = W*x + b
loss(x, y) = sum((y - predict(x))^2)

x, y = 4, 15
pars = Params([W, b])
grads = Tracker.gradient(() -> loss(x, y), pars)

update!(W, -0.1*grads[W])
loss(x, y)

```

The error you get is

> ERROR: MethodError: no method matching copyto!(::Float64, ::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0},Tuple{},typeof(+),Tuple{Float64,Float64}})

so there’s some broadcasting error as far as I can see but there’s no broadcasting done in my functions in the latest code. So a simple answer could be that Flux doesn’t support updating of scalar type parameters. But it seems a bit counter intuitive since the manual shows an example of calculating gradients of a function parameterized by scalars. Did anyone run into this or did I make a mistake somewhere?

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [January 1, 2019, 10:04am UTC](https://discourse.julialang.org/t/how-can-i-go-about-updating-scalar-parameters-in-flux-jl/19146/2 "2019-01-01T10:04:51Z")

</div>

> [@DoktorMike](#):
>
> grads[W]

Does that return a vector or a scalar?

---

<div class="post-metadata">

### Author: ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)
#### Post date: [January 1, 2019, 5:29pm UTC](https://discourse.julialang.org/t/how-can-i-go-about-updating-scalar-parameters-in-flux-jl/19146/3 "2019-01-01T17:29:57Z")

</div>

`grads[W]` returns

> -32.0 (tracked)

i.e. a scalar value.

---

<div class="post-metadata">

### Author: ![MikeInnes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikeinnes/32/3656_2.png) [@MikeInnes](https://discourse.julialang.org/u/MikeInnes)
#### Post date: [January 10, 2019, 10:21am UTC](https://discourse.julialang.org/t/how-can-i-go-about-updating-scalar-parameters-in-flux-jl/19146/4 "2019-01-10T10:21:30Z")

</div>

I have a fix in [this PR](https://github.com/FluxML/Flux.jl/pull/548). Thanks for reporting.
