# Zygote: Mutating arrays is not supported (small example)

**URL:** <https://discourse.julialang.org/t/zygote-mutating-arrays-is-not-supported-small-example/64595>\
**Category:** Machine Learning\
**Created:** [July 13, 2021, 5:41pm UTC](https://discourse.julialang.org/t/zygote-mutating-arrays-is-not-supported-small-example/64595 "2021-07-13T17:41:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gianmariomanca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gianmariomanca/32/25597_2.png) [@gianmariomanca](https://discourse.julialang.org/u/gianmariomanca)\
**Post date:** [July 13, 2021, 5:41pm UTC](https://discourse.julialang.org/t/zygote-mutating-arrays-is-not-supported-small-example/64595/1 "2021-07-13T17:41:41Z")

</div>

A small educational example:

```julia
using Zygote
using ForwardDiff

```

Let’s define a simple gaussian (sampled at integer coordinates)

```julia
x = -20:1.0:20
f2(xf) = 1000.0 .* exp.(-1.25 .* (x .- xf).^2)

```

And a simple center of mass computation to estimate the location of the peak from the samples

```julia
function cm(xc, α)
	y = f2(xc)
	
	# The next two lines are the issue
	y = y .- 15.0;
	y[y.<0] .= 0.0;
		
	y = y.^α #just a random example, could be some other function
	i = argmax(y)
	idx = i-2:1:i+2
	return sum(y[idx] .* x[idx]) / sum(y[idx])
end

```

And let’s say that I want to minimize the error that I make as function of alpha

```julia
function cost_cm(α)
	r = cm.(-0.5:0.01:0.5, α) .- (-0.5:0.01:0.5)
	return maximum(r)
end

```

Forward diff is fine

```julia
d_cost_cm(x) = ForwardDiff.derivative(cost_cm, x)
d_cost_cm(1.0)
-0.02776240571095623

```

Zygote is not happy

```julia
d_cost_cm_Zygote(x) = gradient(cost_cm, x)[1]
d_cost_cm_Zygote(1.0)
Mutating arrays is not supported ...

```

The issue are the lines:

```julia
	y = y .- 15.0;
	y[y.<0] .= 0.0;

```

Is there a way to replace them with something equivalent that would make Zygote happy?  
(PS do we expect this issue to be solved by Diffractor.jl?)

Somehow related, if you use filter to create a new array instead of y[y.\<0] .= 0.0 can you get the list of indexes to be used in sum(y\_new\_filtered\_array .\* x[idx]) / sum(y\_new\_filtered\_array) ?

---

<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 13, 2021, 6:26pm UTC](https://discourse.julialang.org/t/zygote-mutating-arrays-is-not-supported-small-example/64595/2 "2021-07-13T18:26:50Z")

</div>

You could replace `y[y.<0] .= 0.0` by `y = clamp.(y, 0.0, Inf)`.  
See [here](https://juliadiff.org/ChainRulesCore.jl/stable/writing_good_rules.html#Patterns-that-need-rules-in-%5BZygote.jl%5D(https://github.com/FluxML/Zygote.jl)) for workarounds when it is more difficult to write code without mutating arrays.

---

<div class="post-metadata">

**Author:** ![gianmariomanca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gianmariomanca/32/25597_2.png) [@gianmariomanca](https://discourse.julialang.org/u/gianmariomanca)\
**Post date:** [July 13, 2021, 6:55pm UTC](https://discourse.julialang.org/t/zygote-mutating-arrays-is-not-supported-small-example/64595/3 "2021-07-13T18:55:40Z")

</div>

Thank you. For fun I also created a couple of alternatives that seem to work:

```julia
function cm(xc, α)
	y0 = f2(xc)
	y0 = y0 .- 15.0;
	
	idx_m = argmax(y0)
	idx = idx_m-2:1:idx_m+2
	
	y = y0[idx];
	y = clamp.(y, 0.0, Inf)
	y = y.^α
	
	return sum(y .* x[idx]) / sum(y)
end

```

or

```julia
function cm(xc, α)
	y0 = f2(xc)
	y0 = y0 .- 15.0;
	
	idx = findall(x -> x>=0.0,y0)
	y = y0[idx];
	y = y.^α
	
	return sum(y .* x[idx]) / sum(y)
end

```

---

<div class="post-metadata">

**Author:** ![gianmariomanca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gianmariomanca/32/25597_2.png) [@gianmariomanca](https://discourse.julialang.org/u/gianmariomanca)\
**Post date:** [July 13, 2021, 6:57pm UTC](https://discourse.julialang.org/t/zygote-mutating-arrays-is-not-supported-small-example/64595/4 "2021-07-13T18:57:33Z")

</div>

I suspect I have to get a bit more familiar with what “mutating” an array means here.
