# Is it possible to selectively update the params in Flux.jl?

**URL:** https://discourse.julialang.org/t/is-it-possible-to-selectively-update-the-params-in-flux-jl/23198
**Category:** Machine Learning
**Tags:** flux
**Created:** [April 16, 2019, 11:51am UTC](https://discourse.julialang.org/t/is-it-possible-to-selectively-update-the-params-in-flux-jl/23198 "2019-04-16T11:51:25Z")
**Posts on this page:** 10
**Page:** 1

<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: [April 16, 2019, 11:51am UTC](https://discourse.julialang.org/t/is-it-possible-to-selectively-update-the-params-in-flux-jl/23198/1 "2019-04-16T11:51:25Z")

</div>

I can’t seem to update the weights selectively. See below example where `x` is a set of 3 weights and I want to only update, say the first component. But can’t seem to do so. Is there a way to do that?

```julia
x = param(rand(3))
loss() = sum((x .- float.(1:3)).^2)
gs = Tracker.gradient(() -> loss(), params(x))

println(x)
update!(x, -0.01gs[x]) # works
println(x)

println(x[1])
update!(x[1], -0.01gs[x][1]) #doesn't work
println(x[1])

```

I am trying to use Flux and it’s AD to do this [Same Stats, Different Graphs: Generating Datasets with Varied Appearance and Identical Statistics through Simulated Annealing](https://www.autodeskresearch.com/publications/samestats)

---

<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: [April 16, 2019, 11:59am UTC](https://discourse.julialang.org/t/is-it-possible-to-selectively-update-the-params-in-flux-jl/23198/2 "2019-04-16T11:59:26Z")

</div>

I found that this works but I am almost sure that it’s not the best solution

```julia
println(x)
update!(x, -0.01gs[x].*[1,0,0]) # works
println(x)

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 16, 2019, 12:31pm UTC](https://discourse.julialang.org/t/is-it-possible-to-selectively-update-the-params-in-flux-jl/23198/3 "2019-04-16T12:31:32Z")

</div>

> [@xiaodai](#):
>
> update!(x[1], -0.01gs[1]) #doesn’t work

That can’t work because you’re trying to mutate a scalar.

---

<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: [April 16, 2019, 12:43pm UTC](https://discourse.julialang.org/t/is-it-possible-to-selectively-update-the-params-in-flux-jl/23198/4 "2019-04-16T12:43:44Z")

</div>

> [@ChrisRackauckas](#):
>
> That can’t work because you’re trying to mutate a scalar.

Don’t get it… I just selectively update the weights… Clearly it can be done with my method in the 2nd post. Wondering if there’s a better way.

---

<div class="post-metadata">

### Author: ![afternone](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@afternone](https://discourse.julialang.org/u/afternone)
#### Post date: [April 17, 2019, 12:36am UTC](https://discourse.julialang.org/t/is-it-possible-to-selectively-update-the-params-in-flux-jl/23198/5 "2019-04-17T00:36:02Z")

</div>

You can do as follows:

```julia
println(x[1])
x.data[1] += -0.01gs[x].data[1]
Tracker.tracker(x).grad .= 0
println(x[1])

```

---

<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: [April 17, 2019, 2:57am UTC](https://discourse.julialang.org/t/is-it-possible-to-selectively-update-the-params-in-flux-jl/23198/6 "2019-04-17T02:57:30Z")

</div>

> [@afternone](#):
>
> Tracker.tracker(x).grad .= 0

I am trying to understand this. What’s the purpose of setting the `.grad` to 0?

---

<div class="post-metadata">

### Author: ![afternone](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@afternone](https://discourse.julialang.org/u/afternone)
#### Post date: [April 17, 2019, 4:34am UTC](https://discourse.julialang.org/t/is-it-possible-to-selectively-update-the-params-in-flux-jl/23198/7 "2019-04-17T04:34:50Z")

</div>

Without this step, the gradient will accumulate during each backward propagation, which is not what we want.

---

<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: [April 17, 2019, 6:29am UTC](https://discourse.julialang.org/t/is-it-possible-to-selectively-update-the-params-in-flux-jl/23198/9 "2019-04-17T06:29:23Z")

</div>

Are you meant to set the head to 0 for for all grads? For just the ones you are not updating?

---

<div class="post-metadata">

### Author: ![afternone](https://avatars.discourse-cdn.com/v4/letter/a/97f17d/32.png) [@afternone](https://discourse.julialang.org/u/afternone)
#### Post date: [April 17, 2019, 10:00am UTC](https://discourse.julialang.org/t/is-it-possible-to-selectively-update-the-params-in-flux-jl/23198/10 "2019-04-17T10:00:01Z")

</div>

`Tracker.tracker(x).grad .= 0` will reset all grads to 0.  
`Tracker.tracker(x).grad[1] = 0` will only reset the first grad to 0.

---

<div class="post-metadata">

### Author: ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)
#### Post date: [April 17, 2019, 10:54am UTC](https://discourse.julialang.org/t/is-it-possible-to-selectively-update-the-params-in-flux-jl/23198/11 "2019-04-17T10:54:49Z")

</div>

I think this should be a standard feature. Is it worthy opening a new issue in Flux.jl?
