Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zygote producing wrong result for ndim >1 & batch > 0 #37

Closed
agerlach opened this issue Sep 1, 2020 · 1 comment · Fixed by #39
Closed

Zygote producing wrong result for ndim >1 & batch > 0 #37

agerlach opened this issue Sep 1, 2020 · 1 comment · Fixed by #39

Comments

@agerlach
Copy link
Collaborator

agerlach commented Sep 1, 2020

Only 1st element in the gradient is wrong and it is scaled exactly by the batchsize. NOTE: that the batchsize is determined by the internal algorithm and batch=10 is just a hint. In this following example the batchsize is 17. 136/17=8, where 8 is the correct answer

using Quadrature, Cuba, Cubature, Zygote, FiniteDiff, ForwardDiff, Test

f(x,p) = x[1,:]*p[1].+p[2]*p[3]

lb =[1.0,1.0]
ub = [3.0,3.0]
p = [2.0, 3.0, 4.0]
prob = QuadratureProblem(f,lb,ub,p)

function testf3(lb,ub,p; f=f)
    prob = QuadratureProblem(f,lb,ub,p, batch = 10, nout=1)
    solve(prob, CubatureJLh(); reltol=1e-3,abstol=1e-3)[1]
end

dp1 = ForwardDiff.gradient(p->testf3(lb,ub,p),p)
dp2 = Zygote.gradient(p->testf3(lb,ub,p),p)[1]
dp3 = FiniteDiff.finite_difference_gradient(p->testf3(lb,ub,p),p)

@test dp1  dp3 # passes
@test_broken dp2  dp3 # Fail  [136,16,12] ≈ [9,16,12]
@agerlach
Copy link
Collaborator Author

agerlach commented Sep 3, 2020

Most likely due to y not being the correct size for batch. Here it is size nout instead of nout x batch
https://github.com/SciML/Quadrature.jl/blob/11feb791e9423c4dafe615c224526409986f9dcf/src/Quadrature.jl#L491

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant