Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/KernelFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export spectral_mixture_kernel, spectral_mixture_product_kernel
using Compat
using Requires
using Distances, LinearAlgebra
using SpecialFunctions: logabsgamma, besselk, polygamma
using SpecialFunctions: loggamma, besselk, polygamma
using ZygoteRules: @adjoint, pullback
using StatsFuns: logtwo
using InteractiveUtils: subtypes
Expand Down
7 changes: 4 additions & 3 deletions src/basekernels/matern.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ struct MaternKernel{Tν<:Real} <: SimpleKernel
end

@inline function kappa(κ::MaternKernel, d::Real)
ν = first(κ.ν)
iszero(d) ? one(d) : _matern(ν, d)
result = _matern(first(κ.ν), d)
return ifelse(iszero(d), one(result), result)
end

function _matern(ν::Real, d::Real)
exp((one(d) - ν) * logtwo - loggamma(ν) + ν * log(sqrt(2ν) * d) + log(besselk(ν, sqrt(2ν) * d)))
y = sqrt(2ν) * d
return exp((one(d) - ν) * logtwo - loggamma(ν) + ν * log(y) + log(besselk(ν, y)))
end

metric(::MaternKernel) = Euclidean()
Expand Down
2 changes: 0 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
hadamard(x, y) = x .* y

loggamma(x) = first(logabsgamma(x))

# Macro for checking arguments
macro check_args(K, param, cond, desc=string(cond))
quote
Expand Down
14 changes: 0 additions & 14 deletions src/zygote_adjoints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,6 @@ end
end
end

@adjoint function loggamma(x)
first(logabsgamma(x)) , Δ -> (Δ .* polygamma(0, x), )
end

@adjoint function kappa(κ::MaternKernel, d::Real)
ν = first(κ.ν)
val, grad = pullback(_matern, ν, d)
return ((iszero(d) ? one(d) : val),
Δ -> begin
∇ = grad(Δ)
return ((ν = [∇[1]],), iszero(d) ? zero(d) : ∇[2])
end)
end

@adjoint function ColVecs(X::AbstractMatrix)
back(Δ::NamedTuple) = (Δ.X,)
back(Δ::AbstractMatrix) = (Δ,)
Expand Down