# Untracking a Flux Model

**URL:** <https://discourse.julialang.org/t/untracking-a-flux-model/24811>\
**Category:** Machine Learning\
**Created:** [May 31, 2019, 7:55pm UTC](https://discourse.julialang.org/t/untracking-a-flux-model/24811 "2019-05-31T19:55:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mboutros](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mboutros](https://discourse.julialang.org/u/mboutros)\
**Post date:** [May 31, 2019, 7:55pm UTC](https://discourse.julialang.org/t/untracking-a-flux-model/24811/1 "2019-05-31T19:55:06Z")

</div>

I’m using Flux to train a model and it’s working wonderfully. After the model has been trained, it’s parameters are still tracked, which means the output of calling the model is tracked. Is there a way to “untrack” each of the model’s parameters, so that I don’t need to append `#data` each time I want to use the model? I thought this would be trivial but I can’t find a way. The workaround I have in mind is re-creating the model using the untracked version of the trained parameters, but I’m hoping there’s an easier way.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 31, 2019, 8:36pm UTC](https://discourse.julialang.org/t/untracking-a-flux-model/24811/2 "2019-05-31T20:36:45Z")

</div>

I wrote my own method to do that here: [https://github.com/rdeits/FluxExtensions.jl/blob/7d6d67eb6d4da66f0fddc24bf8ec2d04c60db254/src/FluxExtensions.jl#L16-L18](https://github.com/rdeits/FluxExtensions.jl/blob/7d6d67eb6d4da66f0fddc24bf8ec2d04c60db254/src/FluxExtensions.jl#L16-L18)

This was some slightly hack-y research code, but it seems to work and even has [tests](https://github.com/rdeits/FluxExtensions.jl/blob/7d6d67eb6d4da66f0fddc24bf8ec2d04c60db254/test/runtests.jl#L28).

---

<div class="post-metadata">

**Author:** ![dellison](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@dellison](https://discourse.julialang.org/u/dellison)\
**Post date:** [May 31, 2019, 10:42pm UTC](https://discourse.julialang.org/t/untracking-a-flux-model/24811/3 "2019-05-31T22:42:34Z")

</div>

Here’s one easy way to do it:

```julia
model = Flux.mapchildren(Flux.data, model)

```

Edit: As pointed out below, this should be `mapleaves`:

```julia
model = Flux.mapleaves(Flux.data, model)

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [June 1, 2019, 8:56am UTC](https://discourse.julialang.org/t/untracking-a-flux-model/24811/4 "2019-06-01T08:56:59Z")

</div>

What’s the difference of `mapchildren` compared to `mapleaves` as used [here](https://github.com/FluxML/Zygote.jl/blob/99a69737ce6a030383f43e1671ba658ac20be042/examples/mnist_mlp.jl#L36)? See def of `mapleaves` [here](https://github.com/FluxML/Flux.jl/blob/ffe037c485e50d53aeea0df97a81cd61fcc6ee81/src/treelike.jl#L27)

---

<div class="post-metadata">

**Author:** ![dellison](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@dellison](https://discourse.julialang.org/u/dellison)\
**Post date:** [June 1, 2019, 3:51pm UTC](https://discourse.julialang.org/t/untracking-a-flux-model/24811/5 "2019-06-01T15:51:07Z")

</div>

Oh, good catch- actually that line was exactly what I had in mind! 🙂 `mapleaves` is the way to do it. There’s a note in the docs [here](https://fluxml.ai/Flux.jl/stable/gpu/):

> Flux provides `mapleaves` , which allows you to alter all parameters of a model at once.

---

<div class="post-metadata">

**Author:** ![mboutros](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@mboutros](https://discourse.julialang.org/u/mboutros)\
**Post date:** [June 3, 2019, 1:53pm UTC](https://discourse.julialang.org/t/untracking-a-flux-model/24811/6 "2019-06-03T13:53:04Z")

</div>

Awesome, thank you!

---

<div class="post-metadata">

**Author:** ![CraftyVisage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/craftyvisage/32/8826_2.png) [@CraftyVisage](https://discourse.julialang.org/u/CraftyVisage)\
**Post date:** [June 8, 2019, 2:40am UTC](https://discourse.julialang.org/t/untracking-a-flux-model/24811/7 "2019-06-08T02:40:52Z")

</div>

After we do this how do we turn tracking back on?

---

<div class="post-metadata">

**Author:** ![dellison](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@dellison](https://discourse.julialang.org/u/dellison)\
**Post date:** [June 13, 2019, 5:55pm UTC](https://discourse.julialang.org/t/untracking-a-flux-model/24811/8 "2019-06-13T17:55:58Z")

</div>

I don’t have a Julia installation in front of me right now, but I this should work for re-tracking a model:

```julia
model = Flux.mapleaves(Flux.param, model)

```

---

<div class="post-metadata">

**Author:** ![Rana\_Ian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rana_ian/32/13554_2.png) [@Rana\_Ian](https://discourse.julialang.org/u/Rana_Ian)\
**Post date:** [December 12, 2019, 5:00am UTC](https://discourse.julialang.org/t/untracking-a-flux-model/24811/9 "2019-12-12T05:00:47Z")

</div>

Since the use of Zygote, Flux.data is deprecated and the proposed untracking solution no longer works.

Is there a way to untrack a model with Flux+Zygote?

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [December 12, 2019, 7:46am UTC](https://discourse.julialang.org/t/untracking-a-flux-model/24811/10 "2019-12-12T07:46:11Z")

</div>

Should not be a need to do so as Zygote works on normal arrays.

To prevent Zygite from taking gradients of a function you can use `@nograd`, but I guess that is not what the original question was about.

---

<div class="post-metadata">

**Author:** ![Rana\_Ian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rana_ian/32/13554_2.png) [@Rana\_Ian](https://discourse.julialang.org/u/Rana_Ian)\
**Post date:** [December 13, 2019, 4:17am UTC](https://discourse.julialang.org/t/untracking-a-flux-model/24811/11 "2019-12-13T04:17:18Z")

</div>

Thanks for pointing me in the right direction.
