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

Sign error on derivatives with certain type of functions. #33

Closed
KristofferC opened this issue Aug 8, 2015 · 4 comments
Closed

Sign error on derivatives with certain type of functions. #33

KristofferC opened this issue Aug 8, 2015 · 4 comments

Comments

@KristofferC
Copy link
Collaborator

Am I doing something forbidden here?

# Simple working example
f(x) = x[1]^2
g = forwarddiff_gradient(f, Float64, fadtype=:typed)
g([3.0])
#6.0, OK!

# But rewrite it in this way:
f(x) = [x[1]^2]' * [1.0]
g = forwarddiff_gradient(f, Float64, fadtype=:typed)
g([3.0])
 # -6.0, Wups!

Edit: Oh, this is almost exactly: #24 but it should have been fixed in #25?

Edit2: Looked into it a bit more, the problem is not with transpose but with conjugate:

A = ForwardDiff.GraDual{Float64,1}[GraDual(3.0, [1.0])]

julia> A'*[1.0]
1-element Array{ForwardDiff.GraDual{Float64,1},1}:
 GraDual(3.0,
[-1.0])

julia> A.'*[1.0]
1-element Array{ForwardDiff.GraDual{Float64,1},1}:
 GraDual(3.0,
[1.0])

Basically, why does conj on a Array{ForwardDiff.GraDual{Float64,1},1} invert the gradient part?

@mlubin
Copy link
Contributor

mlubin commented Aug 8, 2015

Could you give this a try with #27? That branch basically fixes everything.

@KristofferC
Copy link
Collaborator Author

Yeah, looks like this is correct: https://github.com/JuliaDiff/ForwardDiff.jl/pull/27/files#diff-85b7db0a1fe4b76c8f248e5b5d9ad774R71

Using the matrix syntax doesn't work but rewriting it with dot fixes it.

f(x) = [x[1]^2]' * [1.0]
ForwardDiff.gradient(f, [3.0])
# ERROR: MethodError: `load_gradient` has no method matching 
# load_gradient(::Array{ForwardDiff.GradientNum{1,Float64,Tuple{Float64}},1})
# in take_gradient at /home/kristoffer/.julia/v0.4/ForwardDiff/src/fad_api.jl:73
# in gradient at /home/kristoffer/.julia/v0.4/ForwardDiff/src/fad_api.jl:78

f(x) = dot([x[1]^2], [1.0])
ForwardDiff.gradient(f, [3.0])
# 1-element Array{Float64,1}:
# 6.0

@jrevels
Copy link
Member

jrevels commented Aug 10, 2015

Using the matrix syntax doesn't work but rewriting it with dot fixes it.

f(x) = [x[1]^2]' * [1.0]

Just a note on why this results in an error: f above is a function from a Vector to a Vector - ForwardDiff.gradient expects a function from a Vector to a Number.

@KristofferC
Copy link
Collaborator Author

Ah, of course, thank you for pointing that out.

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

No branches or pull requests

3 participants