Skip to content

Introduce JuliaFormatter with style=blue #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style = "blue"
18 changes: 12 additions & 6 deletions src/KernelFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if !isfile(joinpath(@__DIR__, "update_v0.8.0"))
This kernel now divides the squared distance by 2 to align with standard practice.
This warning will be removed in 0.9.0.
""";
color = Base.info_color(),
color=Base.info_color(),
)
touch(joinpath(@__DIR__, "update_v0.8.0"))
end
Expand All @@ -37,8 +37,15 @@ export KernelSum, KernelProduct
export TransformedKernel, ScaledKernel
export TensorProduct

export Transform, SelectTransform, ChainTransform, ScaleTransform, LinearTransform,
ARDTransform, IdentityTransform, FunctionTransform, PeriodicTransform
export Transform,
SelectTransform,
ChainTransform,
ScaleTransform,
LinearTransform,
ARDTransform,
IdentityTransform,
FunctionTransform,
PeriodicTransform

export NystromFact, nystrom

Expand All @@ -59,7 +66,6 @@ using StatsFuns: logtwo
using InteractiveUtils: subtypes
using StatsBase


abstract type Kernel end
abstract type SimpleKernel <: Kernel end

Expand Down Expand Up @@ -113,10 +119,10 @@ include("zygote_adjoints.jl")
include("test_utils.jl")

function __init__()
@require Kronecker="2c470bb0-bcc8-11e8-3dad-c9649493f05e" begin
@require Kronecker = "2c470bb0-bcc8-11e8-3dad-c9649493f05e" begin
Copy link
Member

Choose a reason for hiding this comment

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

It seems a bit inconsistent to add whitespace here, compared with the conventions for keyword arguments - is this according to BlueStyle or a JuliaFormatter thing?

Choose a reason for hiding this comment

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

I agree. I suspect this is a JuliaFormatter (or CSTParser) issue

include(joinpath("matrix", "kernelkroneckermat.jl"))
end
@require PDMats="90014a1f-27ba-587c-ab20-58faa44d9150" begin
@require PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150" begin
include(joinpath("matrix", "kernelpdmat.jl"))
end
end
Expand Down
19 changes: 11 additions & 8 deletions src/approximations/nystrom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,40 @@
function sampleindex(X::AbstractMatrix, r::Real; obsdim::Integer=defaultobs)
0 < r <= 1 || throw(ArgumentError("Sample rate `r` must be in range (0,1]"))
n = size(X, obsdim)
m = ceil(Int, n*r)
m = ceil(Int, n * r)
S = StatsBase.sample(1:n, m; replace=false, ordered=true)
return S
end

function nystrom_sample(k::Kernel, X::AbstractMatrix, S::Vector{<:Integer}; obsdim::Integer=defaultobs)
obsdim ∈ [1, 2] || throw(ArgumentError("`obsdim` should be 1 or 2 (see docs of kernelmatrix))"))
function nystrom_sample(
k::Kernel, X::AbstractMatrix, S::Vector{<:Integer}; obsdim::Integer=defaultobs
)
obsdim ∈ [1, 2] ||
throw(ArgumentError("`obsdim` should be 1 or 2 (see docs of kernelmatrix))"))
Xₘ = obsdim == 1 ? X[S, :] : X[:, S]
C = kernelmatrix(k, Xₘ, X; obsdim=obsdim)
Cs = C[:, S]
return (C, Cs)
end

function nystrom_pinv!(Cs::Matrix{T}, tol::T=eps(T)*size(Cs,1)) where {T<:Real}
function nystrom_pinv!(Cs::Matrix{T}, tol::T=eps(T) * size(Cs, 1)) where {T<:Real}
# Compute eigendecomposition of sampled component of K
QΛQᵀ = LinearAlgebra.eigen!(LinearAlgebra.Symmetric(Cs))

# Solve for D = Λ^(-1/2) (pseudo inverse - use tolerance from before factorization)
D = QΛQᵀ.values
λ_tol = maximum(D)*tol
λ_tol = maximum(D) * tol

for i in eachindex(D)
@inbounds D[i] = abs(D[i]) <= λ_tol ? zero(T) : one(T)/sqrt(D[i])
@inbounds D[i] = abs(D[i]) <= λ_tol ? zero(T) : one(T) / sqrt(D[i])
end

# Scale eigenvectors by D
Q = QΛQᵀ.vectors
QD = LinearAlgebra.rmul!(Q, LinearAlgebra.Diagonal(D)) # Scales column i of Q by D[i]

# W := (QD)(QD)ᵀ = (QΛQᵀ)^(-1) (pseudo inverse)
W = QD*QD'
W = QD * QD'

# Symmetrize W
return LinearAlgebra.copytri!(W, 'U')
Expand Down Expand Up @@ -98,5 +101,5 @@ Compute the approximate kernel matrix based on the Nystrom factorization.
function kernelmatrix(CᵀWC::NystromFact{<:Real})
W = CᵀWC.W
C = CᵀWC.C
return C'*W*C
return C' * W * C
end
7 changes: 3 additions & 4 deletions src/basekernels/constant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ metric(::WhiteKernel) = Delta()

Base.show(io::IO, ::WhiteKernel) = print(io, "White Kernel")


"""
ConstantKernel(; c=1.0)

Expand All @@ -49,14 +48,14 @@ Kernel function always returning a constant value `c`
"""
struct ConstantKernel{Tc<:Real} <: SimpleKernel
c::Vector{Tc}
function ConstantKernel(;c::T=1.0) where {T<:Real}
new{T}([c])
function ConstantKernel(; c::T=1.0) where {T<:Real}
return new{T}([c])
end
end

@functor ConstantKernel

kappa(κ::ConstantKernel,x::Real) = first(κ.c)*one(x)
kappa(κ::ConstantKernel, x::Real) = first(κ.c) * one(x)

metric(::ConstantKernel) = Delta()

Expand Down
6 changes: 2 additions & 4 deletions src/basekernels/exponential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ metric(::SqExponentialKernel) = SqEuclidean()

iskroncompatible(::SqExponentialKernel) = true

Base.show(io::IO,::SqExponentialKernel) = print(io,"Squared Exponential Kernel")
Base.show(io::IO, ::SqExponentialKernel) = print(io, "Squared Exponential Kernel")

## Aliases ##

Expand All @@ -41,7 +41,6 @@ See [`SqExponentialKernel`](@ref)
"""
const SEKernel = SqExponentialKernel


"""
ExponentialKernel()

Expand Down Expand Up @@ -78,7 +77,6 @@ See [`ExponentialKernel`](@ref)
"""
const Matern12Kernel = ExponentialKernel


"""
GammaExponentialKernel(; γ = 2.0)

Expand Down Expand Up @@ -109,5 +107,5 @@ metric(::GammaExponentialKernel) = Euclidean()
iskroncompatible(::GammaExponentialKernel) = true

function Base.show(io::IO, κ::GammaExponentialKernel)
print(io, "Gamma Exponential Kernel (γ = ", first(κ.γ), ")")
return print(io, "Gamma Exponential Kernel (γ = ", first(κ.γ), ")")
end
17 changes: 9 additions & 8 deletions src/basekernels/fbm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ function (κ::FBMKernel)(x::AbstractVector{<:Real}, y::AbstractVector{<:Real})
modY = sum(abs2, y)
modXY = sqeuclidean(x, y)
h = first(κ.h)
return (modX^h + modY^h - modXY^h)/2
return (modX^h + modY^h - modXY^h) / 2
end

(κ::FBMKernel)(x::Real, y::Real) = (abs2(x)^first(κ.h) + abs2(y)^first(κ.h) - abs2(x - y)^first(κ.h)) / 2
function (κ::FBMKernel)(x::Real, y::Real)
return (abs2(x)^first(κ.h) + abs2(y)^first(κ.h) - abs2(x - y)^first(κ.h)) / 2
end

Base.show(io::IO, κ::FBMKernel) = print(io, "Fractional Brownian Motion Kernel (h = ", first(κ.h), ")")
function Base.show(io::IO, κ::FBMKernel)
return print(io, "Fractional Brownian Motion Kernel (h = ", first(κ.h), ")")
end

_fbm(modX, modY, modXY, h) = (modX^h + modY^h - modXY^h)/2
_fbm(modX, modY, modXY, h) = (modX^h + modY^h - modXY^h) / 2

_mod(x::AbstractVector{<:Real}) = abs2.(x)
_mod(x::ColVecs) = vec(sum(abs2, x.X; dims=1))
Expand All @@ -56,10 +60,7 @@ function kernelmatrix(κ::FBMKernel, x::AbstractVector, y::AbstractVector)
end

function kernelmatrix!(
K::AbstractMatrix,
κ::FBMKernel,
x::AbstractVector,
y::AbstractVector,
K::AbstractMatrix, κ::FBMKernel, x::AbstractVector, y::AbstractVector
)
pairwise!(K, SqEuclidean(), x, y)
K .= _fbm.(_mod(x), _mod(y)', K, κ.h)
Expand Down
25 changes: 14 additions & 11 deletions src/basekernels/gabor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ Gabor kernel with lengthscale `ell` and period `p`. Given by
"""
struct GaborKernel{K<:Kernel} <: Kernel
kernel::K
function GaborKernel(;ell=nothing, p=nothing)
k = _gabor(ell=ell, p=p)
function GaborKernel(; ell=nothing, p=nothing)
k = _gabor(; ell=ell, p=p)
return new{typeof(k)}(k)
end
end

@functor GaborKernel

(κ::GaborKernel)(x, y) = κ.kernel(x ,y)
(κ::GaborKernel)(x, y) = κ.kernel(x, y)

function _gabor(; ell = nothing, p = nothing)
function _gabor(; ell=nothing, p=nothing)
if ell === nothing
if p === nothing
return SqExponentialKernel() * CosineKernel()
Expand All @@ -29,7 +29,8 @@ function _gabor(; ell = nothing, p = nothing)
elseif p === nothing
return transform(SqExponentialKernel(), 1 ./ ell) * CosineKernel()
else
return transform(SqExponentialKernel(), 1 ./ ell) * transform(CosineKernel(), 1 ./ p)
return transform(SqExponentialKernel(), 1 ./ ell) *
transform(CosineKernel(), 1 ./ p)
end
end

Expand All @@ -38,11 +39,11 @@ function Base.getproperty(k::GaborKernel, v::Symbol)
return getfield(k, v)
elseif v == :ell
kernel1 = k.kernel.kernels[1]
if kernel1 isa TransformedKernel
return 1 ./ kernel1.transform.s[1]
else
return 1.0
end
if kernel1 isa TransformedKernel
return 1 ./ kernel1.transform.s[1]
else
return 1.0
end
elseif v == :p
kernel2 = k.kernel.kernels[2]
if kernel2 isa TransformedKernel
Expand All @@ -55,7 +56,9 @@ function Base.getproperty(k::GaborKernel, v::Symbol)
end
end

Base.show(io::IO, κ::GaborKernel) = print(io, "Gabor Kernel (ell = ", κ.ell, ", p = ", κ.p, ")")
function Base.show(io::IO, κ::GaborKernel)
return print(io, "Gabor Kernel (ell = ", κ.ell, ", p = ", κ.p, ")")
end

kernelmatrix(κ::GaborKernel, x::AbstractVector) = kernelmatrix(κ.kernel, x)

Expand Down
8 changes: 5 additions & 3 deletions src/basekernels/maha.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Mahalanobis distance-based kernel given by
where the matrix P is the metric.

"""
struct MahalanobisKernel{T<:Real, A<:AbstractMatrix{T}} <: SimpleKernel
struct MahalanobisKernel{T<:Real,A<:AbstractMatrix{T}} <: SimpleKernel
P::A
function MahalanobisKernel(; P::AbstractMatrix{T}) where {T<:Real}
LinearAlgebra.checksquare(P)
new{T,typeof(P)}(P)
return new{T,typeof(P)}(P)
end
end

Expand All @@ -22,4 +22,6 @@ kappa(κ::MahalanobisKernel, d::T) where {T<:Real} = exp(-d)

metric(κ::MahalanobisKernel) = SqMahalanobis(κ.P)

Base.show(io::IO, κ::MahalanobisKernel) = print(io, "Mahalanobis Kernel (size(P) = ", size(κ.P), ")")
function Base.show(io::IO, κ::MahalanobisKernel)
return print(io, "Mahalanobis Kernel (size(P) = ", size(κ.P), ")")
end
2 changes: 1 addition & 1 deletion src/basekernels/matern.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ For `ν=n+1/2, n=0,1,2,...` it can be simplified and you should instead use
"""
struct MaternKernel{Tν<:Real} <: SimpleKernel
ν::Vector{Tν}
function MaternKernel(;nu::T=1.5, ν::T=nu) where {T<:Real}
function MaternKernel(; nu::T=1.5, ν::T=nu) where {T<:Real}
@check_args(MaternKernel, ν, ν > zero(T), "ν > 0")
return new{T}([ν])
end
Expand Down
10 changes: 5 additions & 5 deletions src/basekernels/periodic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ Periodic Kernel as described in http://www.inference.org.uk/mackay/gpB.pdf eq. 4
"""
struct PeriodicKernel{T} <: SimpleKernel
r::Vector{T}
function PeriodicKernel(; r::AbstractVector{T} = ones(Float64, 1)) where {T<:Real}
function PeriodicKernel(; r::AbstractVector{T}=ones(Float64, 1)) where {T<:Real}
@assert all(r .> 0)
new{T}(r)
return new{T}(r)
end
end

PeriodicKernel(dims::Int) = PeriodicKernel(Float64, dims)

PeriodicKernel(T::DataType, dims::Int = 1) = PeriodicKernel(r = ones(T, dims))
PeriodicKernel(T::DataType, dims::Int=1) = PeriodicKernel(; r=ones(T, dims))

@functor PeriodicKernel

metric(κ::PeriodicKernel) = Sinus(κ.r)

kappa(κ::PeriodicKernel, d::Real) = exp(- 0.5d)
kappa(κ::PeriodicKernel, d::Real) = exp(-0.5d)

function Base.show(io::IO, κ::PeriodicKernel)
print(io, "Periodic Kernel, length(r) = $(length(κ.r))")
return print(io, "Periodic Kernel, length(r) = $(length(κ.r))")
end
28 changes: 18 additions & 10 deletions src/basekernels/piecewisepolynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,48 @@ processes are hence V times mean-square differentiable. The kernel function is:
```
where `r` is the Mahalanobis distance mahalanobis(x,y) with `maha` as the metric.
"""
struct PiecewisePolynomialKernel{V, A<:AbstractMatrix{<:Real}} <: SimpleKernel
struct PiecewisePolynomialKernel{V,A<:AbstractMatrix{<:Real}} <: SimpleKernel
maha::A
j::Int
function PiecewisePolynomialKernel{V}(maha::AbstractMatrix{<:Real}) where V
function PiecewisePolynomialKernel{V}(maha::AbstractMatrix{<:Real}) where {V}
V in (0, 1, 2, 3) || error("Invalid parameter V=$(V). Should be 0, 1, 2 or 3.")
LinearAlgebra.checksquare(maha)
j = div(size(maha, 1), 2) + V + 1
return new{V,typeof(maha)}(maha, j)
end
end

function PiecewisePolynomialKernel(;v::Integer=0, maha::AbstractMatrix{<:Real})
function PiecewisePolynomialKernel(; v::Integer=0, maha::AbstractMatrix{<:Real})
return PiecewisePolynomialKernel{v}(maha)
end

# Have to reconstruct the type parameter
# See also https://github.com/FluxML/Functors.jl/issues/3#issuecomment-626747663
function Functors.functor(::Type{<:PiecewisePolynomialKernel{V}}, x) where V
function Functors.functor(::Type{<:PiecewisePolynomialKernel{V}}, x) where {V}
function reconstruct_kernel(xs)
return PiecewisePolynomialKernel{V}(xs.maha)
end
return (maha = x.maha,), reconstruct_kernel
return (maha=x.maha,), reconstruct_kernel
end

_f(κ::PiecewisePolynomialKernel{0}, r, j) = 1
_f(κ::PiecewisePolynomialKernel{1}, r, j) = 1 + (j + 1) * r
_f(κ::PiecewisePolynomialKernel{2}, r, j) = 1 + (j + 2) * r + (j^2 + 4 * j + 3) / 3 * r.^2
_f(κ::PiecewisePolynomialKernel{3}, r, j) = 1 + (j + 3) * r +
(6 * j^2 + 36j + 45) / 15 * r.^2 + (j^3 + 9 * j^2 + 23j + 15) / 15 * r.^3
_f(κ::PiecewisePolynomialKernel{2}, r, j) = 1 + (j + 2) * r + (j^2 + 4 * j + 3) / 3 * r .^ 2
function _f(κ::PiecewisePolynomialKernel{3}, r, j)
return 1 +
(j + 3) * r +
(6 * j^2 + 36j + 45) / 15 * r .^ 2 +
(j^3 + 9 * j^2 + 23j + 15) / 15 * r .^ 3
end

kappa(κ::PiecewisePolynomialKernel{V}, r) where V = max(1 - r, 0)^(κ.j + V) * _f(κ, r, κ.j)
function kappa(κ::PiecewisePolynomialKernel{V}, r) where {V}
return max(1 - r, 0)^(κ.j + V) * _f(κ, r, κ.j)
end

metric(κ::PiecewisePolynomialKernel) = Mahalanobis(κ.maha)

function Base.show(io::IO, κ::PiecewisePolynomialKernel{V}) where {V}
print(io, "Piecewise Polynomial Kernel (v = ", V, ", size(maha) = ", size(κ.maha), ")")
return print(
io, "Piecewise Polynomial Kernel (v = ", V, ", size(maha) = ", size(κ.maha), ")"
)
end
Loading