Skip to content

Commit

Permalink
Deprecate kernel field in StdDPP and kDPP (#41)
Browse files Browse the repository at this point in the history
* Deprecation release

* Preadapt tests

* Small docstring fix

* Breaking changes and version bump

* Update src/offline/kdpp.jl

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update src/offline/stddpp.jl

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Fix docs

* Fix tests

* oh no

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
theogf and github-actions[bot] committed Jan 18, 2023
1 parent 66801f5 commit d8bf86d
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "InducingPoints"
uuid = "b4bd816d-b975-4295-ac05-5f2992945579"
authors = ["Théo Galy-Fajou <theo.galyfajou@gmail.com> and JuliaGaussianProcesses"]
version = "0.3.5"
version = "0.4.0"

[deps]
AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918"
Expand Down
8 changes: 4 additions & 4 deletions docs/src/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ Sample from a k-Determinantal Point Process to select `k` points. `Z` will be a

```@example base
kernel = SqExponentialKernel()
alg = kDPP(M, kernel)
Z = inducingpoints(alg, x)
alg = kDPP(M)
Z = inducingpoints(alg, x; kernel)
fig = plot_inducing_points(x, Z) # hide
save("kdpp.svg", fig); nothing # hide
```
Expand All @@ -123,8 +123,8 @@ Samples from a standard Determinantal Point Process. The number of inducing poin

```@example base
kernel = with_lengthscale(SqExponentialKernel(), 0.2)
alg = StdDPP(kernel)
Z = inducingpoints(alg, x)
alg = StdDPP()
Z = inducingpoints(alg, x; kernel)
fig = plot_inducing_points(x, Z) # hide
save("StdDPP.svg", fig); nothing # hide
```
Expand Down
2 changes: 1 addition & 1 deletion src/offline/greedy_var_selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ end
See `GreedyVarSelection` for more info. `rng` isn't actually used here.
"""
function inducingpoints(
rng::AbstractRNG, alg::GreedyVarSelection, x::AbstractVector; kernel::Kernel
::AbstractRNG, alg::GreedyVarSelection, x::AbstractVector; kernel::Kernel
)
# Don't try to handle this case.
alg.M > length(x) && throw(ArgumentError("M > length(x). Requires M <= length(x)."))
Expand Down
12 changes: 0 additions & 12 deletions src/offline/greedyip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,3 @@ function greedy_ip(
end
return X[collect(ℐᵢₚ)]
end

# function elbo(Z::AbstractVector, X::AbstractVector, y::AbstractVector, kernel::Kernel, σ²::Real)
# Knm = kernelmatrix(kernel, X, Z)
# Kmm = kernelmatrix(kernel, Z) + T(jitt) * I
# Qff = Knm * (Kmm \ Knm')
# Kt = kerneldiagmatrix(kernel, X) .+ T(jitt) - diag(Qff)
# Σ = inv(Kmm) + Knm' * Knm / σ²
# invQnn = 1/σ² * I - 1/ (σ²)^2 * Knm * inv(Σ) * Knm'
# logdetQnn = logdet(Σ) + logdet(Kmm)
# return -0.5 * dot(y, invQnn * y) - 0.5 * logdetQnn -
# 1.0 / (2 * σ²) * sum(Kt)
# end
20 changes: 11 additions & 9 deletions src/offline/kdpp.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
"""
kDPP(m::Int, kernel::Kernel)
kDPP(m::Int)
k-DPP (Determinantal Point Process) will return a subset of `X` of size `m`,
according to DPP probability
according to DPP probability.
The kernel is passed as a keyword argument to [`inducingpoints`](@ref).
"""
struct kDPP{K<:Kernel} <: OffIPSA
struct kDPP <: OffIPSA
m::Int
kernel::K
function kDPP(m::Int, kernel::K) where {K<:Kernel}
function kDPP(m::Int)
m > 0 || throw(ArgumentError("The number of inducing points m should be positive"))
return new{K}(m, kernel)
return new(m)
end
end

function inducingpoints(rng::AbstractRNG, alg::kDPP, X::AbstractVector; kwargs...)
function inducingpoints(
rng::AbstractRNG, alg::kDPP, X::AbstractVector; kernel::Kernel, kwargs...
)
if alg.m >= length(X)
return edge_case(alg.m, length(X), X)
end
return kdpp_ip(rng, X, alg.m, alg.kernel)
return kdpp_ip(rng, X, alg.m, kernel)
end

Base.show(io::IO, ::kDPP) = print(io, "k-DPP selection of inducing points")
Base.show(io::IO, alg::kDPP) = print(io, "k-DPP selection of $(alg.m) inducing points")

function kdpp_ip(rng::AbstractRNG, X::AbstractVector, m::Int, kernel::Kernel)
return X[rand(rng, DPP(kernel, X)(m))] # Sample m indices from a DPP
Expand Down
18 changes: 8 additions & 10 deletions src/offline/stddpp.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
@doc raw"""
StdDPP(kernel::Kernel)
StdDPP()
Standard DPP (Determinantal Point Process) sampling given `kernel`.
Standard DPP (Determinantal Point Process) sampling.
The size of the returned `Z` is not fixed (but is not allowed to be empty unlike in a classical DPP).
The kernel is passed as a keyword argument to [`inducingpoints`](@ref).
"""
struct StdDPP{K<:Kernel} <: OffIPSA
kernel::K
function StdDPP(kernel::K) where {K<:Kernel}
return new{K}(kernel)
end
end
struct StdDPP <: OffIPSA end

function inducingpoints(rng::AbstractRNG, alg::StdDPP, X::AbstractVector; kwargs...)
dpp = DPP(alg.kernel, X)
function inducingpoints(
rng::AbstractRNG, alg::StdDPP, X::AbstractVector; kernel::Kernel, kwargs...
)
dpp = DPP(kernel, X)
samp = rand(rng, dpp)
while isempty(samp) # Sample from the DPP until there is a non-empty set
samp = rand(rng, dpp)
Expand Down
6 changes: 4 additions & 2 deletions src/online/seqdpp.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""
SeqDPP()
Sequential sampling via Determinantal Point Processes. Requires passing the `kernel` as keyword argument to `inducingpoints`.
Sequential sampling via Determinantal Point Processes.
Requires passing a `kernel::Kernel` as a keyword argument to [`inducingpoints`](@ref).
"""
struct SeqDPP <: OnIPSA end

Expand All @@ -11,7 +12,8 @@ Base.show(io::IO, ::SeqDPP) = print(io, "Sequential DPP")
inducingpoints([rng::AbstractRNG], alg::SeqDPP, X::AbstractVector; kernel::Kernel)
inducingpoints([rng::AbstractRNG], alg::SeqDPP, X::AbstractMatrix; obsdim=1, kernel::Kernel)
Select inducing points according using Sequential Determinantal Point Processes. Requires as additional keyword argument the `kernel`.
Select inducing points according using Sequential Determinantal Point Processes.
Requires passing a `kernel::Kernel` as a keyword argument.
"""
function inducingpoints(
rng::AbstractRNG,
Expand Down
12 changes: 6 additions & 6 deletions test/offline/kdpp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
nInd = 10
kernel = SqExponentialKernel()
X = ColVecs(rand(D, N))
alg = kDPP(nInd, kernel)
@test repr(alg) == "k-DPP selection of inducing points"
Z = inducingpoints(alg, X)
alg = kDPP(nInd)
@test repr(alg) == "k-DPP selection of $(nInd) inducing points"
Z = inducingpoints(alg, X; kernel)
@test length(Z) == nInd
@test_throws ArgumentError kDPP(-1, kernel)
@test_throws ErrorException inducingpoints(kDPP(100, kernel), X)
test_Zalg(kDPP(nInd, kernel), N)
@test_throws ArgumentError kDPP(-1)
@test_throws ErrorException inducingpoints(kDPP(100), X; kernel)
test_Zalg(kDPP(nInd), N; kernel)
end
2 changes: 1 addition & 1 deletion test/offline/stddpp.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@testset "stddpp.jl" begin
test_Zalg(StdDPP(SqExponentialKernel()))
test_Zalg(StdDPP(); kernel=SqExponentialKernel())
end

2 comments on commit d8bf86d

@theogf
Copy link
Member Author

@theogf theogf commented on d8bf86d Jan 18, 2023

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

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.4.0 -m "<description of version>" d8bf86de8f39eef98b5d42f0a1f7fbe56e0cb0cc
git push origin v0.4.0

Please sign in to comment.