ForwardDiff.jl icon indicating copy to clipboard operation
ForwardDiff.jl copied to clipboard

Incorrect gradient with cascaded reshape, linear equation solver operations

Open semihakbayrak opened this issue 4 years ago • 2 comments

For the following function A(η::Array) = η[1:2]'*(reshape(η[2+1:end],(2,2))\η[1:2]), I observed a weird behaviour with ForwardDiff. Suppose that we want to evaluate the gradient at x = [2.0, 2.0, 1.0, 0.0, 0.0, 1.0]. Then ForwardDiff.gradient(A,x) returns the output [4.0, 4.0, -4.0, 0.0, -4.0, -4.0]. However, I would expect 4th element of the gradient to be -4.0. If I change the vector x slightly as [2.0, 2.0, 1.0, 0.00001, 0.00001, 1.0], then the gradient turns out to be correct and I get the output [3.999960000399996, 3.999960000399996, -3.9999200011999845, -3.9999200011999845, -3.9999200011999845, -3.9999200011999845].

semihakbayrak avatar Feb 17 '21 12:02 semihakbayrak

I think this should be fixed by #481, can you confirm?

mcabbott avatar Mar 01 '21 13:03 mcabbott

Yes, indeed it resolves the issue. Thanks!

semihakbayrak avatar Mar 17 '21 12:03 semihakbayrak