Skip to content

Commit

Permalink
Merge pull request #141 from devmotion/dw/logabsgamma
Browse files Browse the repository at this point in the history
Add derivative of `logabsgamma`
  • Loading branch information
mcabbott committed May 13, 2023
2 parents 5248679 + 32ba995 commit 0e99755
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Tracker"
uuid = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
version = "0.2.24"
version = "0.2.25"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
10 changes: 10 additions & 0 deletions src/lib/real.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,13 @@ end

collectmemaybe(xs::AbstractArray{>:TrackedReal}) = collect(xs)
collectmemaybe(xs::AbstractArray{<:TrackedReal}) = collect(xs)

# `logabsgamma` returns a tuple and hence its derivative is not defined in DiffRules
SpecialFunctions.logabsgamma(x::TrackedReal) = track(SpecialFunctions.logabsgamma, x)
@grad function SpecialFunctions.logabsgamma(x::Real)
data_x = data(x)
function logabsgamma_pullback(Δ)
return (SpecialFunctions.digamma(data_x) * first(Δ),)
end
return SpecialFunctions.logabsgamma(data_x), logabsgamma_pullback
end
5 changes: 5 additions & 0 deletions test/tracker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Tracker, Test, NNlib
using Tracker: TrackedReal, gradient, gradcheck, grad, checkpoint, forwarddiff
using NNlib: conv, ∇conv_data, depthwiseconv
using PDMats
using SpecialFunctions: logabsgamma
using Printf: @sprintf
using LinearAlgebra: diagm, dot, LowerTriangular, norm, det, logdet, logabsdet, I, Diagonal
using Statistics: mean, std, var
Expand Down Expand Up @@ -504,3 +505,7 @@ end
@test size(y) == (5, 3)
end

@testset "logabsgamma" begin
@test gradcheck(x -> logabsgamma(only(x))[1], rand(1))
@test gradcheck(x -> logabsgamma(only(x))[2], rand(1))
end

2 comments on commit 0e99755

@mcabbott
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

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/83517

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:

git tag -a v0.2.25 -m "<description of version>" 0e997556f78b6b810f81d380106c4454380d249e
git push origin v0.2.25

Please sign in to comment.