Here's a minimal working example.
using Flux
myvector = []
for i in 1:3
push!(myvector, 0.1 * i) #Produces a vector [0.1, 0.2, 0.3]
end
Q = Chain(Dense(3, 64, relu), Dense(64, 32, relu), Dense(32, 16, relu),
Dense(16, 1, identity)) #Creates a neural network. Exact parameters aren't important.
θ, re = Flux.destructure(Q)
f(x) = -re(θ)(x)[1]
gs = gradient(f, myvector)[1] #Takes the gradient of f at the point u.
The code creates a vector myvector
and a neural network Q
. It then feeds the neural network myvector
and tries to take the gradient. There is a mutating arrays error:
Mutating arrays is not supported -- called setindex!(Vector{Any}, ...) This error occurs when you ask Zygote to differentiate operations that change the elements of arrays in place (e.g. setting values with x .= ...)
Why is there such an error? Nothing seems to be changing the elements of the array. How can I fix this error?
If instead of using !push
to define the vector I had simply defined myvector = [0.1, 0.2, 0.3]
, then there is no error. However, I don't see why - the !push
doesn't modify the elements, it just increases the length of myvector
. For my use case as well, the elements of the vector are complicated and need to be calculated individually, so I can't just define myvector
as in this MWE.
There is also no error if I define myvector = AbstractFloat[]
. However, this seems to cause issues elsewhere in my use case (which I will ask about separately if making myvector
an AbstractFloat is the best solution).
Your issue is related to your variable,
myvector
. You made aVector{Any}
, which is not supported by Zygote. You could make aVector{Float64}
like this:myvector=[0.1 * i for i in 1:3]
. In most cases,Float32
is needed for machine learning,myvector=[0.1 * i for i in 1:3].|>Float32
If you really want to use
push!()
, I would suggest usingmyvector = Float64[]
instead ofmyvector = AbstractFloat[]
.