# "Mutating arrays is not supported" error when running FluxML/vae\_mnist.jl

**URL:** <https://discourse.julialang.org/t/mutating-arrays-is-not-supported-error-when-running-fluxml-vae-mnist-jl/59890>\
**Category:** New to Julia\
**Created:** [April 23, 2021, 6:17pm UTC](https://discourse.julialang.org/t/mutating-arrays-is-not-supported-error-when-running-fluxml-vae-mnist-jl/59890 "2021-04-23T18:17:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![conditionality](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@conditionality](https://discourse.julialang.org/u/conditionality)\
**Post date:** [April 23, 2021, 6:17pm UTC](https://discourse.julialang.org/t/mutating-arrays-is-not-supported-error-when-running-fluxml-vae-mnist-jl/59890/1 "2021-04-23T18:17:18Z")

</div>

Hi,

I’m trying to run the file found here: [https://github.com/FluxML/model-zoo/blob/master/vision/vae\_mnist/vae\_mnist.jl](https://github.com/FluxML/model-zoo/blob/master/vision/vae_mnist/vae_mnist.jl)  
But when I do, I get the error “LoadError: Mutating arrays is not supported” and it complains about row vae\_mnist.jl:145. I think I read (on this forum) that those examples are old, and that Flux has been updated since then, but I don’t know how to fix this so that it works with the latest version of Flux. I’m using Flux v0.12.1.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [April 23, 2021, 11:40pm UTC](https://discourse.julialang.org/t/mutating-arrays-is-not-supported-error-when-running-fluxml-vae-mnist-jl/59890/2 "2021-04-23T23:40:58Z")

</div>

Looks like the problem happens inside `Flux.params`. Adding

```
Flux.Zygote.@nograd Flux.params

```

before the definition of `model_loss` fixes it for me.

---

<div class="post-metadata">

**Author:** ![conditionality](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@conditionality](https://discourse.julialang.org/u/conditionality)\
**Post date:** [April 24, 2021, 11:10am UTC](https://discourse.julialang.org/t/mutating-arrays-is-not-supported-error-when-running-fluxml-vae-mnist-jl/59890/3 "2021-04-24T11:10:01Z")

</div>

That does the trick! But what does it do? It looks completely alien to me.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [April 24, 2021, 5:07pm UTC](https://discourse.julialang.org/t/mutating-arrays-is-not-supported-error-when-running-fluxml-vae-mnist-jl/59890/4 "2021-04-24T17:07:52Z")

</div>

[`Flux.params`](https://github.com/FluxML/Flux.jl/blob/8a5b9774f0d6b254f60c88ca07a984997a016e75/src/functor.jl#L51) retrieves the trainable parameters of a model. It does this by allocating a `Params` struct and then storing the parameters in that struct. This results in the values in an array being updated, but the autodiff tool being used (Zygote) can’t differentiate array mutation.

This statement uses the `@nograd` macro to disable differentiation through `params` and always assume it has no effect on the gradient. In this case that is the correct interpretation, the weights are changed in the optimizer and this algorithm does not want to include that.

I can’t find the docs for `@nograd`, I learned about it reading this forum but I’m no longer sure it is the official way to solve this problem.
