-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement scalar rules for Zygote with ChainRules (#103)
* Implement scalar rules for Zygote with ChainRules * Fix adjoints of `uniformlogpdf` * Fix typo * Update src/chainrules.jl Co-authored-by: Seth Axen <[email protected]> * Remove one more `@thunk` * Add tests * Add missing import * Introduce more randomness in tests * Use `@scalar_rule` for `uniformlogpdf` * Set seed Co-authored-by: Seth Axen <[email protected]>
- Loading branch information
Showing
9 changed files
with
171 additions
and
115 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
## Uniform ## | ||
|
||
@scalar_rule( | ||
uniformlogpdf(a, b, x), | ||
@setup( | ||
insupport = a <= x <= b, | ||
diff = b - a, | ||
c = insupport ? inv(diff) : inv(one(diff)), | ||
z = insupport ? zero(x) : oftype(x, NaN), | ||
), | ||
(c, -c, z), | ||
) | ||
|
||
## Beta ## | ||
|
||
@scalar_rule( | ||
betalogpdf(α::Real, β::Real, x::Number), | ||
@setup(di = digamma(α + β)), | ||
( | ||
@thunk(log(x) - digamma(α) + di), | ||
@thunk(log(1 - x) - digamma(β) + di), | ||
@thunk((α - 1)/x + (1 - β)/(1 - x)), | ||
), | ||
) | ||
|
||
## Gamma ## | ||
|
||
@scalar_rule( | ||
gammalogpdf(k::Real, θ::Real, x::Number), | ||
( | ||
@thunk(-digamma(k) - log(θ) + log(x)), | ||
@thunk(-k/θ + x/θ^2), | ||
@thunk((k - 1)/x - 1/θ), | ||
), | ||
) | ||
|
||
## Chisq ## | ||
|
||
@scalar_rule( | ||
chisqlogpdf(k::Real, x::Number), | ||
@setup(ko2 = k / 2), | ||
(@thunk((-logtwo - digamma(ko2) + log(x)) / 2), @thunk((ko2 - 1)/x - one(ko2) / 2)), | ||
) | ||
|
||
## FDist ## | ||
|
||
@scalar_rule( | ||
fdistlogpdf(v1::Real, v2::Real, x::Number), | ||
@setup( | ||
temp1 = v1 * x + v2, | ||
temp2 = log(temp1), | ||
vsum = v1 + v2, | ||
temp3 = vsum / temp1, | ||
temp4 = digamma(vsum / 2), | ||
), | ||
( | ||
@thunk((log(v1 * x) + 1 - temp2 - x * temp3 - digamma(v1 / 2) + temp4) / 2), | ||
@thunk((log(v2) + 1 - temp2 - temp3 - digamma(v2 / 2) + temp4) / 2), | ||
@thunk(v1 / 2 * (1 / x - temp3) - 1 / x), | ||
), | ||
) | ||
|
||
## TDist ## | ||
|
||
@scalar_rule( | ||
tdistlogpdf(v::Real, x::Number), | ||
( | ||
@thunk((digamma((v + 1) / 2) - 1 / v - digamma(v / 2) - log(1 + x^2 / v) + x^2 * (v + 1) / v^2 / (1 + x^2 / v)) / 2), | ||
@thunk(-x * (v + 1) / (v + x^2)), | ||
) | ||
) | ||
|
||
## Binomial ## | ||
|
||
@scalar_rule( | ||
binomlogpdf(n::Int, p::Real, x::Int), | ||
(DoesNotExist(), x / p - (n - x) / (1 - p), DoesNotExist()), | ||
) | ||
|
||
## Poisson ## | ||
|
||
@scalar_rule( | ||
poislogpdf(v::Real, x::Int), | ||
(x / v - 1, DoesNotExist()), | ||
) |
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,51 @@ | ||
|
||
@testset "chainrules" begin | ||
x, Δx, x̄ = randn(3) | ||
y, Δy, ȳ = randn(3) | ||
z, Δz, z̄ = randn(3) | ||
Δu = randn() | ||
|
||
ỹ = x + exp(y) + exp(z) | ||
z̃ = x + exp(y) | ||
frule_test(DistributionsAD.uniformlogpdf, (x, Δx), (ỹ, Δy), (z̃, Δz)) | ||
rrule_test(DistributionsAD.uniformlogpdf, Δu, (x, x̄), (ỹ, ȳ), (z̃, z̄)) | ||
|
||
x̃ = exp(x) | ||
ỹ = exp(y) | ||
z̃ = logistic(z) | ||
frule_test(DistributionsAD.betalogpdf, (x̃, Δx), (ỹ, Δy), (z̃, Δz)) | ||
rrule_test(DistributionsAD.betalogpdf, Δu, (x̃, x̄), (ỹ, ȳ), (z̃, z̄)) | ||
|
||
x̃ = exp(x) | ||
ỹ = exp(y) | ||
z̃ = exp(z) | ||
frule_test(DistributionsAD.gammalogpdf, (x̃, Δx), (ỹ, Δy), (z̃, Δz)) | ||
rrule_test(DistributionsAD.gammalogpdf, Δu, (x̃, x̄), (ỹ, ȳ), (z̃, z̄)) | ||
|
||
x̃ = exp(x) | ||
ỹ = exp(y) | ||
z̃ = exp(z) | ||
frule_test(DistributionsAD.chisqlogpdf, (x̃, Δx), (ỹ, Δy)) | ||
rrule_test(DistributionsAD.chisqlogpdf, Δu, (x̃, x̄), (ỹ, ȳ)) | ||
|
||
x̃ = exp(x) | ||
ỹ = exp(y) | ||
z̃ = exp(z) | ||
frule_test(DistributionsAD.fdistlogpdf, (x̃, Δx), (ỹ, Δy), (z̃, Δz)) | ||
rrule_test(DistributionsAD.fdistlogpdf, Δu, (x̃, x̄), (ỹ, ȳ), (z̃, z̄)) | ||
|
||
x̃ = exp(x) | ||
frule_test(DistributionsAD.tdistlogpdf, (x̃, Δx), (y, Δy)) | ||
rrule_test(DistributionsAD.tdistlogpdf, Δu, (x̃, x̄), (y, ȳ)) | ||
|
||
x̃ = rand(1:100) | ||
ỹ = logistic(y) | ||
z̃ = rand(1:x̃) | ||
frule_test(DistributionsAD.binomlogpdf, (x̃, nothing), (ỹ, Δy), (z̃, nothing)) | ||
rrule_test(DistributionsAD.binomlogpdf, Δu, (x̃, nothing), (ỹ, ȳ), (z̃, nothing)) | ||
|
||
x̃ = exp(x) | ||
ỹ = rand(1:100) | ||
frule_test(DistributionsAD.poislogpdf, (x̃, Δx), (ỹ, nothing)) | ||
rrule_test(DistributionsAD.poislogpdf, Δu, (x̃, x̄), (ỹ, nothing)) | ||
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