"Mutating arrays is not supported" error when running FluxML/vae_mnist.jl

Hi,

I’m trying to run the file found here: model-zoo/vae_mnist.jl at master · FluxML/model-zoo · GitHub
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.

Looks like the problem happens inside Flux.params. Adding

Flux.Zygote.@nograd Flux.params

before the definition of model_loss fixes it for me.

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

Flux.params 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.

1 Like