-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add rrule frule; adjust how parameters are passed (#326)
* add rrule frule; adjust how parameters are passed * update docs * relax versioning * version proof
- Loading branch information
Showing
9 changed files
with
192 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# View find_zero as solving `f(x, p) = 0` for `xᵅ(p)`. | ||
# This is implicitly defined. By the implicit function theorem, we have: | ||
# ∇f = 0 => ∂/∂ₓ f(xᵅ, p) ⋅ ∂xᵅ/∂ₚ + ∂/∂ₚf(x\^α, p) ⋅ I = 0 | ||
# or ∂xᵅ/∂ₚ = ∂/∂ₚf(xᵅ, p) / ∂/∂ₓ f(xᵅ, p) | ||
|
||
# does this work? | ||
# It doesn't pass a few of the tests of ChainRulesTestUtils | ||
function ChainRulesCore.frule( | ||
config::ChainRulesCore.RuleConfig, | ||
(Δself, Δp), | ||
::typeof(solve), | ||
ZP::ZeroProblem, | ||
M::AbstractUnivariateZeroMethod, | ||
p; | ||
kwargs...) | ||
|
||
|
||
xᵅ = solve(ZP, M, p; kwargs...) | ||
|
||
F = p -> Callable_Function(M, ZP.F, p) | ||
fₓ(x) = first(F(p)(x)) | ||
fₚ(p) = - first(F(p)(xᵅ)) | ||
|
||
function pushforward_find_zero(fₓ, fₚ, xᵅ, p, Δp) | ||
# is scalar? | ||
o = typeof(p) == eltype(p) ? one(p) : ones(eltype(p), size(p)) | ||
fx = ChainRulesCore.frule_via_ad(config, | ||
(ChainRulesCore.NoTangent(), o), | ||
fₓ, xᵅ)[2] | ||
fp = ChainRulesCore.frule_via_ad(config, | ||
(ChainRulesCore.NoTangent(), o), | ||
fₚ, p)[2] | ||
|
||
dp = ChainRulesCore.unthunk(Δp) | ||
δ = - (fp * dp) / fx | ||
δ | ||
end | ||
|
||
xᵅ, pushforward_find_zero(fₓ, fₚ, xᵅ, p, Δp) | ||
|
||
end | ||
|
||
## modified from | ||
## https://github.com/gdalle/ImplicitDifferentiation.jl/blob/main/src/implicit_function.jl | ||
function ChainRulesCore.rrule( | ||
rc::ChainRulesCore.RuleConfig, | ||
::typeof(solve), | ||
ZP::ZeroProblem, | ||
M::AbstractUnivariateZeroMethod, | ||
p; | ||
kwargs...) | ||
|
||
xᵅ = solve(ZP, M, p; kwargs...) | ||
F = p -> Callable_Function(M, ZP.F, p) | ||
fₓ(x) = first(F(p)(x)) | ||
fₚ(p) = - first(F(p)(xᵅ)) | ||
|
||
pullback_Aᵀ = last ∘ ChainRulesCore.rrule_via_ad(rc, fₓ, xᵅ)[2] | ||
pullback_Bᵀ = last ∘ ChainRulesCore.rrule_via_ad(rc, fₚ, p)[2] | ||
|
||
function pullback_find_zero(dy) | ||
dy = ChainRulesCore.unthunk(dy) | ||
u = inv(pullback_Aᵀ(1/dy)) | ||
dx = pullback_Bᵀ(u) | ||
return (ChainRulesCore.NoTangent(), ChainRulesCore.NoTangent(), | ||
ChainRulesCore.NoTangent(), dx) | ||
end | ||
|
||
return xᵅ, pullback_find_zero | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Roots | ||
using Zygote | ||
using Test | ||
|
||
# issue #325 add frule, rrule | ||
|
||
@testset "Test rrule" begin | ||
|
||
# single function | ||
f(x, p) = log(x) - p | ||
F(p) = find_zero(f, 1, Order1(), p) | ||
@test first(Zygote.gradient(F, 1)) ≈ exp(1) | ||
|
||
g(x, p) = x^2 - p[1]*x - p[2] | ||
G(p) = find_zero(g, 1, Order1(), p) | ||
@test first(Zygote.gradient(G, [0,4])) ≈ [1/2, 1/4] | ||
|
||
fx(x,p) = 1/x | ||
F2(p) = find_zero((f,fx), 1, Roots.Newton(), p) | ||
@test first(Zygote.gradient(F2, 1)) ≈ exp(1) | ||
|
||
gp(x, p) = 2x - p[1] | ||
G2(p) = find_zero((g, gp), 1, Roots.Newton(), p) | ||
@test first(Zygote.gradient(G2, [0,4])) ≈ [1/2, 1/4] | ||
|
||
end |
1afc30c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
1afc30c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/69167
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: