Hey,
the following MWE:
function g(x)
out = fill(0.0, size(x))
for i = 1:size(x)[1]
out[i, :] .= x[i, :]
end
return out
end
ReverseDiff.gradient(x -> sum(g(x)), randn((5, 5)))
which returns
5×5 Array{Float64,2}:
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
Using out = zeros(eltype(x), size(x))
instead of the fill
, works as expected:
function g(x)
out = zeros(eltype(x), size(x))
for i = 1:size(x)[1]
out[i, :] .= x[i, :]
end
return out
end
ReverseDiff.gradient(x -> sum(g(x)), randn((5, 5)))
5×5 Array{Float64,2}:
1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0 1.0
Is that something I’m not aware of in ReverseDiff or a bug?
Thanks,
Felix