Open slwu89 opened 1 year ago
I encountered a similar error. The following equivalent (basically replacing with for loop to mimic what .@
macro is "supposed to do") and it works, (I shortened the above MWE)
function mwe(a,c)
# d = @. a/(a + c) # this doesn't work
# d = @. 1.0/(a + c) # even this doesn't work
# d = @. a + c # but this works
# d = @. 1.0/a # this also works
# but below works
d = similar(a)
@simd for i in eachindex(a)
@inbounds d[i] = a[i]/(a[i] + c)
end
e = map(a,d) do aa, dd
aa*dd
end
return sum(e)
end
a = rand(Normal(),5)
c = 50.0
mwe(a,c)
ReverseDiff.hessian(x -> mwe(x, c), a)
# works! (haven't checked the correctness, but structure of the matrix is alright.)
More interestingly the below works!!
function mwe(a,c)
# d = @. a/(a + c) # doesn't work
# works
d = similar(a)
@. d = a/(a + c)
e = map(a,d) do aa, dd
aa*dd
end
return sum(e)
end
a = rand(Normal(),5)
c = 50.0
mwe(a,c)
ReverseDiff.hessian(x -> mwe(x, c), a)
Hi ReverseDiff team,
I've found an error that comes up when an object used in a function being differentiated is the result of a tracked array and another argument (not the one we are finding the derivative with respect to) when the other argument is a scalar. I understand my explanation is a bit imprecise, a minimum reproducible example is below. In the line labeled "doesn't work" the arg
c
is treated as a scalar. In the line labeled "works" it is treated as a length one vector. The objectd
is used in further computation; whenc
is treated as a scalar this leads to a method error when it (an element of the resulting objectd
) is used in the lineaa*sin(dd)
.[EDIT]: I'm on Julia 1.8.3 and the version of ReverseDiff.jl on the Julia general repo (v1.14.4).