# Problem with Mutating array not supported in constructing a Wasserstein GAN in Flux

**URL:** https://discourse.julialang.org/t/problem-with-mutating-array-not-supported-in-constructing-a-wasserstein-gan-in-flux/64203
**Category:** Machine Learning
**Tags:** flux
**Created:** [July 7, 2021, 9:43am UTC](https://discourse.julialang.org/t/problem-with-mutating-array-not-supported-in-constructing-a-wasserstein-gan-in-flux/64203 "2021-07-07T09:43:27Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![gforchini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gforchini/32/25358_2.png) [@gforchini](https://discourse.julialang.org/u/gforchini)
#### Post date: [July 7, 2021, 9:43am UTC](https://discourse.julialang.org/t/problem-with-mutating-array-not-supported-in-constructing-a-wasserstein-gan-in-flux/64203/1 "2021-07-07T09:43:27Z")

</div>

I am trying to construct a Wasserstein GAN as in Gulrajani, et al, Improved Training of Wasserstein GANs. Part of the code is below. The discriminator loss function contains a function of the derivatives of the discriminator with respect to the inputs. This creates problems in differentiating the discriminator loss which I cannot solve.

using CUDA, Flux, Distributions, CSV, DataFrames, ForwardDiff, StatsBase, GLM, Random, Statistics, Base  
using Flux.Losses: logitbinarycrossentropy  
using Parameters: @with\_kw  
using Flux.Optimise: update!  
using Base.Iterators: partition  
using Flux: params  
using ReverseDiff  
using Distances

global const ε=Float32(1e-6)

function Discriminator()  
return Chain(  
Dense(14, 21, elu),  
Dense(21, 1,sigmoid))  
end

function Grad\_discriminator\_x(dscr,Z)  
z=zeros(14)  
f = z → dscr(z)[1]  
g = z → ReverseDiff.gradient(f,z)  
x = mapslices(g, Z; dims=1)  
return x  
end

function discriminator\_loss(dscr, real\_input, fake\_input)  
λ=10.  
real\_loss = mean(dscr(real\_input))  
fake\_loss = mean(dscr(fake\_input))  
mix=ε \* real\_input +(1-ε)_fake\_input  
x=Grad\_discriminator\_x(dscr,mix)  
x = mapslices(Z-\>Grad\_discriminator\_x(Discriminator(),Z), mix; dims=1)  
norms = colwise(Euclidean(), x, zeros(14))  
penalty = λ_ mean(norms.-1)^2  
return fake\_loss-real\_loss+penalty  
end

function Grad\_discriminator\_loss(dscr, real\_input, fake\_input)  
gradient(()-\>discriminator\_loss(dscr, real\_input, fake\_input),Flux.params(dscr))  
end

real\_input=randn(Float64,14, 5);  
fake\_input=randn(Float64,14, 5);

Grad\_discriminator\_x(Discriminator(),randn(Float64,14, 5))

discriminator\_loss(Discriminator(), real\_input, fake\_input)

Grad\_discriminator\_loss(Discriminator(), real\_input, fake\_input)

Calculating the gradient of the discriminator with respect to the inputs, or the discriminator loss is not problem. However, when I try to evaluate Grad\_discriminator\_loss I get the following error. Any help would be greatly appreciated.

Grad\_discriminator\_loss(Discriminator(), real\_input, fake\_input)  
ERROR: Mutating arrays is not supported  
Stacktrace:  
[1] error(s::String)  
@ Base ./error.jl:33  
[2] (::Zygote.var"#403#404")(#unused#::Nothing)  
@ Zygote ~/.julia/packages/Zygote/i1R8y/src/lib/array.jl:58  
[3] (::Zygote.var"#2259#back#405"{Zygote.var"#403#404"})(Δ::Nothing)  
@ Zygote ~/.julia/packages/ZygoteRules/OjfTt/src/adjoint.jl:59  
[4] Pullback  
@ ~/.julia/packages/Distances/gnt89/src/generic.jl:83 [inlined]  
[5] (::typeof(∂(colwise!)))(Δ::Vector{Float64})  
@ Zygote ~/.julia/packages/Zygote/i1R8y/src/compiler/interface2.jl:0  
[6] Pullback  
@ ~/.julia/packages/Distances/gnt89/src/generic.jl:163 [inlined]  
[7] (::typeof(∂(colwise)))(Δ::Vector{Float64})  
@ Zygote ~/.julia/packages/Zygote/i1R8y/src/compiler/interface2.jl:0  
[8] Pullback  
@ ./REPL[11]:8 [inlined]  
[9] (::typeof(∂(discriminator\_loss)))(Δ::Float64)  
@ Zygote ~/.julia/packages/Zygote/i1R8y/src/compiler/interface2.jl:0  
[10] Pullback  
@ ./REPL[12]:2 [inlined]  
[11] (::typeof(∂(λ)))(Δ::Float64)  
@ Zygote ~/.julia/packages/Zygote/i1R8y/src/compiler/interface2.jl:0  
[12] (::Zygote.var"#69#70"{Zygote.Params, typeof(∂(λ)), Zygote.Context})(Δ::Float64)  
@ Zygote ~/.julia/packages/Zygote/i1R8y/src/compiler/interface.jl:255  
[13] gradient(f::Function, args::Zygote.Params)  
@ Zygote ~/.julia/packages/Zygote/i1R8y/src/compiler/interface.jl:59  
[14] Grad\_discriminator\_loss(dscr::Chain{Tuple{Dense{typeof(elu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(σ), Matrix{Float32}, Vector{Float32}}}}, real\_input::Matrix{Float64}, fake\_input::Matrix{Float64})  
@ Main ./REPL[12]:2  
[15] top-level scope  
@ REPL[18]:1

---

<div class="post-metadata">

### Author: ![jbrea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbrea/32/3879_2.png) [@jbrea](https://discourse.julialang.org/u/jbrea)
#### Post date: [July 7, 2021, 10:44am UTC](https://discourse.julialang.org/t/problem-with-mutating-array-not-supported-in-constructing-a-wasserstein-gan-in-flux/64203/2 "2021-07-07T10:44:24Z")

</div>

Did you have already a look at [this](https://juliadiff.org/ChainRulesCore.jl/stable/writing_good_rules.html#Patterns-that-need-rules-in-%5BZygote.jl%5D(https://github.com/FluxML/Zygote.jl)) or [this](https://github.com/rakeshvar/Zygote-Mutating-Arrays-WorkAround.jl)?

Btw. [Quoting the code](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) would help the reader of your post 🙂

---

<div class="post-metadata">

### Author: ![gforchini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gforchini/32/25358_2.png) [@gforchini](https://discourse.julialang.org/u/gforchini)
#### Post date: [July 7, 2021, 12:02pm UTC](https://discourse.julialang.org/t/problem-with-mutating-array-not-supported-in-constructing-a-wasserstein-gan-in-flux/64203/3 "2021-07-07T12:02:20Z")

</div>

Thank you for the links. Although I have googled extensively, I haven’t seen them.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [July 7, 2021, 12:46pm UTC](https://discourse.julialang.org/t/problem-with-mutating-array-not-supported-in-constructing-a-wasserstein-gan-in-flux/64203/4 "2021-07-07T12:46:03Z")

</div>

This error usually means some array-level function isn’t supported, and internally it is making a new array & writing into it. Seeing “packages/Distances” in the stack trace is a clue, and if you look [here](https://github.com/FluxML/Zygote.jl/blob/master/src/lib/distances.jl) you can check what methods are supported:

```julia
julia> jacobian(x -> colwise(Euclidean(), x, zeros(14,1)), rand(14,1)) # makes a 1-element vector
([0.19770084950458103 0.03806289602200381 … 0.31432031223232176 0.03768309247814568],)

julia> jacobian(x -> colwise(Euclidean(), x, zeros(14)), rand(14))
ERROR: Mutating arrays is not supported
Stacktrace:
...
  [4] Pullback
    @ ~/.julia/packages/Distances/gnt89/src/generic.jl:63 [inlined]
  [5] (::typeof(∂(colwise!)))(Δ::Vector{Float64})
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface2.jl:0
  [6] Pullback
    @ ~/.julia/packages/Distances/gnt89/src/generic.jl:129 [inlined]
  [7] (::typeof(∂(colwise)))(Δ::Vector{Float64})
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface2.jl:0
...

julia> jacobian(x -> mapslices(identity, x, dims=1), [1 2; 3 4])
ERROR: Mutating arrays is not supported
Stacktrace:
  [2] (::Zygote.var"#440#441")(#unused#::Nothing)
    @ Zygote ~/.julia/dev/Zygote/src/lib/array.jl:76
...
  [7] (::typeof(∂(concatenate_setindex!)))(Δ::Nothing)
    @ Zygote ~/.julia/dev/Zygote/src/compiler/interface2.jl:0

julia> using SliceMap

julia> jacobian(x -> mapcols(identity, x), [1 2; 3 4])
([1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1],)

```

The next problem you will hit is `mapslices`. You can probably re-arrange things to use solid arrays not slices, but if you must, then [SliceMap.jl](https://github.com/mcabbott/SliceMap.jl) is one way around that. (How this will work with 2nd derivatives you’ll have to see, `MapCols` which uses ForwardDiff may behave better.)

---

<div class="post-metadata">

### Author: ![gforchini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gforchini/32/25358_2.png) [@gforchini](https://discourse.julialang.org/u/gforchini)
#### Post date: [July 7, 2021, 1:16pm UTC](https://discourse.julialang.org/t/problem-with-mutating-array-not-supported-in-constructing-a-wasserstein-gan-in-flux/64203/5 "2021-07-07T13:16:49Z")

</div>

Many thanks for your help. This is really helpful to understand what is going wrong.
