JuliaDiff / JuliaDiff/ChainRulesCore.jl
Explicit examples on how to derive pushfowards and pullbacks
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 267
- Forks
- 66
- PR merge metrics
- No merged PRs in 30d
Description
It was mentioned on Slack that the docs could really use some explicit examples on how to derive a pushforward or pullback, so here is an example I cooked up that I'll try to polish up and turn into a docs PR.
Consider an R^2 -> R^2 function (2 real inputs and 2 real outputs)
f((x, y)::Vector) = [u(x, y)
v(x, y)]
This thing has a Jacobian matrix
J(f)((x, y)) = [∂_u_x(x, y) ∂_u_y(x, y)
∂_v_x(x, y) ∂_v_y(x, y)]
where ∂_u_x is meant to be the partial derivative of u with respect to x and so on.
The pushforward is then defined as
Pf(f)((x, y)) = ((Δx, Δy),) -> J(f)((x, y)) * [Δx, Δy]
where Δx, Δy should be interpreted as 'wiggles in the input space'. This simplifies to
Pf(f)((x, y)) = ((Δx, Δy),) -> = [∂_x_u(x, y) * Δx + ∂_y_u(x, y) * Δy
∂_x_v(x, y) * Δx + ∂_y_v(x, y) * Δy]
Similarly, the pullback is defined as
Pb(f)((x, y)) = ((Δu, Δv),) -> transpose(J(f)((x, y))) * [Δu, Δv]
where Δu, Δv are to be interpreted at 'wobbles in the output space'. This simplifies to
Pb(f)((x, y)) = ((Δu, Δv),) -> [∂_x_u(x, y) * Δu + ∂_x_v(x, y) * Δv
∂_y_u(x, y) * Δu + ∂_y_v(x, y) * Δv]
Okay, so suppose for concreteness that we have u(x, y) = sin(x) * y^2 and v(x, y) = exp(2x) + y then
J(f)((x, y)) = [cos(x)*y^2 2sin(x)*y
2exp(2x) + y exp(2x) + 1]
and the pushforward is
Pf(f)((x, y)) = ((Δx, Δy),) -> [cos(x)*y^2 * Δx + 2sin(x)*y * Δy
(2exp(2x)+y) * Δx + (exp(2x)+1) * Δy]
whereas the pullback is
Pb(f)((x, y)) = ((Δu, Δv),) -> [cos(x)*y^2 * Δu + (2exp(2x)+y) * Δv
2sin(x)*y * Δu + (exp(2x)+1) * Δv]
We are now ready to write an frule and rrule for our function f.
function frule((_, (Δx, Δy)::Vector{<:Real}), ::typeof(f), r::Vector{<:Real})
(x, y) = r
z = f(r)
sinx, cosx = sincos(x)
exp2x = exp(2x)
∂z = [cosx*y^2 * Δx + 2sinx*y * Δy
(2exp2x+y) * Δx + (exp2x+1) * Δy]
return z, ∂z
end
function rrule(::typeof(f), r::Vector{<:Real})
(x, y) = r
sinx, cosx = sincos(x)
exp2x = exp(2x)
z = f(r)
Pb((Δu, Δv)::Vector{<:Real}) = [cosx*y^2 * Δu + (2exp2x+y) * Δv
2sinx*y * Δu + (exp2x+1) * Δv]
return z, Pb
end
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the worked Julia example in this issue, including its Jacobian, pushforward, pullback, frule, and rrule sections. Locate the repository's documentation entry point and adapt the example there. Done means the docs contain explicit, polished derivations and corresponding rule examples that readers can follow.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100