JuliaDiff / JuliaDiff/ForwardDiff.jl

Perturbation confusion

Open
#238 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
1k
Forks
160
PR merge metrics
No merged PRs in 30d

Description

I have two examples in real use cases that can trigger perturbation confusion error in ForwardDiff.jl.

The first example (Lagrangian):

using ForwardDiff
m,g = 1, 9.8
t = 1
q = [1,2]
q̇ = [3,4]
L(t,q,q̇) = m/2 * dot(q̇,q̇) - m*g*q[2]

∂L∂q̇(L, t, q, q̇) = ForwardDiff.gradient(a->L(t,q,a), q̇)
Dqq̇(L, t, q, q̇) = ForwardDiff.jacobian(a->∂L∂q̇(L,t,a,q̇), q)
Dqq̇(L, t, q, q̇)  # ERROR: potential perturbation confusion...

The second one (Hamiltonian):

using ForwardDiff
m,g = 1, 9.8
t = 1
p = [5,6]
q = [1,2]

function Legendre_transformation(F, w)
    wv = a->ForwardDiff.gradient(F, a)
    z = zeros(w)
    M = ForwardDiff.jacobian(wv, z)
    b = wv(z)
    v = Symmetric(M)\(w-b)
    w'v - F(v)
end

function Lagrangian2Hamiltonian(Lagrangian, t, q, p)
    L = q̇ -> Lagrangian(t, q, q̇)
    Legendre_transformation(L, p)
end
Lagrangian2Hamiltonian(L, t, q, p)  # This is OK
ForwardDiff.gradient(a->Lagrangian2Hamiltonian(L, t, a, p), q)
# ERROR: potential perturbation confusion...

P.S.: I am on ForwardDiff master branch

julia> Pkg.status("ForwardDiff")
 - ForwardDiff                   0.5.0+             master

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the two Julia reproductions in the issue with ForwardDiff.gradient and ForwardDiff.jacobian, focusing on the nested differentiation that triggers perturbation confusion. Trace how the perturbation-confusion error arises in these examples; done means both reported use cases differentiate without that error and retain their expected results.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.