Skip to content
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

Adds nlatent and nclass to Categorical #68

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GPLikelihoods"
uuid = "6031954c-0455-49d7-b3b9-3e1c99afaf40"
authors = ["JuliaGaussianProcesses Team"]
version = "0.3.1"
version = "0.3.2"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
12 changes: 12 additions & 0 deletions src/GPLikelihoods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,24 @@ export Link,
ProbitLink,
NormalCDFLink,
SoftMaxLink
export nlatent

# Links
include("links.jl")

# Likelihoods
abstract type AbstractLikelihood end

"""
nlatent(::AbstractLikelihood)::Int

Returns the number of latent Gaussian processes needed to build the likelihood.
In other terms the input dimensionality passed to the likelihood from the GP perspective.
It is typically 1, but for some likelihoods like [`CategoricalLikelihood`](@ref) or
[`HeteroscedasticGaussianLikelihood`](@ref) multiple latent GPs are necessary.
"""
nlatent(::AbstractLikelihood) = 1 # Default number of latent GPs required is 1

include("likelihoods/bernoulli.jl")
include("likelihoods/categorical.jl")
include("likelihoods/gaussian.jl")
Expand Down
4 changes: 2 additions & 2 deletions src/TestInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Functors
using Random
using Test

function test_interface(rng::AbstractRNG, lik, out_dist, D_in=1; functor_args=())
function test_interface(rng::AbstractRNG, lik, out_dist, D_in=nlatent(lik); functor_args=())
N = 10
T = Float64 # TODO test Float32 as well
f, fs = if D_in == 1
Expand Down Expand Up @@ -53,7 +53,7 @@ samples is correct and if the functor works as intended.
- `functor_args=()`: a collection of symbols of arguments to match functor parameters with.
...
"""
function test_interface(lik, out_dist, D_in=1; kwargs...)
function test_interface(lik, out_dist, D_in=nlatent(lik); kwargs...)
return test_interface(Random.GLOBAL_RNG, lik, out_dist, D_in; kwargs...)
end

Expand Down
10 changes: 7 additions & 3 deletions src/likelihoods/categorical.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
CategoricalLikelihood(l=BijectiveSimplexLink(softmax))
CategoricalLikelihood(n::Int, l=BijectiveSimplexLink(softmax))

Categorical likelihood is to be used if we assume that the
Categorical likelihood with `n` categories is to be used if we assume that the
uncertainty associated with the data follows a [Categorical distribution](https://en.wikipedia.org/wiki/Categorical_distribution).

Assuming a distribution with `n` categories:
Expand All @@ -27,10 +27,14 @@ For more details, see the end of the section of this [Wikipedia link](https://en
where it corresponds to Variant 1 and 2.
"""
struct CategoricalLikelihood{Tl<:AbstractLink} <: AbstractLikelihood
n::Int # Number of categories
invlink::Tl
end

CategoricalLikelihood(l=BijectiveSimplexLink(softmax)) = CategoricalLikelihood(link(l))
CategoricalLikelihood(n=BijectiveSimplexLink(softmax)) = CategoricalLikelihood(n, link(l))

nlatent(l::CategoricalLikelihood) = l.n
nlatent(l::CategoricalLikelihood{<:BijectiveSimplexLink}) = l.n - 1

function (l::CategoricalLikelihood)(f::AbstractVector{<:Real})
return Categorical(l.invlink(f))
Expand Down
2 changes: 2 additions & 0 deletions src/likelihoods/gaussian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ end

HeteroscedasticGaussianLikelihood(l=exp) = HeteroscedasticGaussianLikelihood(link(l))

nlatent(::HeteroscedasticGaussianLikelihood) = 2

function (l::HeteroscedasticGaussianLikelihood)(f::AbstractVector{<:Real})
return Normal(f[1], sqrt(l.invlink(f[2])))
end
Expand Down
18 changes: 10 additions & 8 deletions test/likelihoods/categorical.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
@testset "CategoricalLikelihood" begin
@test CategoricalLikelihood() isa
nclass = 4
@test CategoricalLikelihood(nclass) isa
CategoricalLikelihood{<:GPLikelihoods.BijectiveSimplexLink}

theogf marked this conversation as resolved.
Show resolved Hide resolved
@test CategoricalLikelihood(nclass, softmax) isa CategoricalLikelihood{SoftMaxLink}
@test CategoricalLikelihood(nclass, SoftMaxLink()) isa CategoricalLikelihood{SoftMaxLink}
theogf marked this conversation as resolved.
Show resolved Hide resolved

@test CategoricalLikelihood(softmax) isa CategoricalLikelihood{SoftMaxLink}
@test CategoricalLikelihood(SoftMaxLink()) isa CategoricalLikelihood{SoftMaxLink}

OUT_DIM = 4
lik_bijective = CategoricalLikelihood()
test_interface(lik_bijective, Categorical, OUT_DIM)
lik_nonbijective = CategoricalLikelihood(softmax)
lik_bijective = CategoricalLikelihood(nclass)
test_interface(lik_bijective, Categorical)
@test nlatent(lik_bijective) == nclass - 1
lik_nonbijective = CategoricalLikelihood(nclass, softmax)
test_interface(lik_nonbijective, Categorical, OUT_DIM)
@test nlatent(lik_nonbijective) == nclass
end
5 changes: 2 additions & 3 deletions test/likelihoods/gaussian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ end
end

lik = HeteroscedasticGaussianLikelihood()
IN_DIM = 3
OUT_DIM = 2 # one for the mean the other for the log-standard deviation
N = 10
test_interface(lik, Normal, 2)
test_interface(lik, Normal)
@test nlatent(lik) == 2
end