-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from JuliaGaussianProcesses/tgf/rework_categor…
…ical Rework categorical to allow multiple variants
- Loading branch information
Showing
6 changed files
with
59 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,41 @@ | ||
""" | ||
CategoricalLikelihood(l=softmax) | ||
CategoricalLikelihood(l=BijectiveSimplexLink(softmax)) | ||
Categorical likelihood is to be used if we assume that the | ||
uncertainity associated with the data follows a Categorical distribution. | ||
uncertainty associated with the data follows a [Categorical distribution](https://en.wikipedia.org/wiki/Categorical_distribution). | ||
Assuming a distribution with `n` categories: | ||
## `n-1` inputs (bijective link) | ||
One can work with a bijective transformation by wrapping a link (like `softmax`) | ||
into a [`BijectiveSimplexLink`](@ref) and only needs `n-1` inputs: | ||
```math | ||
p(y|f_1, f_2, \\dots, f_{n-1}) = \\operatorname{Categorical}(y | l(f_1, f_2, \\dots, f_{n-1}, 0)) | ||
``` | ||
Given an `AbstractVector` ``[f_1, f_2, ..., f_{n-1}]``, returns a `Categorical` distribution, | ||
with probabilities given by ``l(f_1, f_2, ..., f_{n-1}, 0)``. | ||
The default constructor is a bijective link around `softmax`. | ||
## `n` inputs (non-bijective link) | ||
One can also pass directly the inputs without concatenating a `0`: | ||
```math | ||
p(y|f_1, f_2, \\dots, f_n) = \\operatorname{Categorical}(y | l(f_1, f_2, \\dots, f_n)) | ||
``` | ||
This variant is over-parametrized, as there are `n-1` independent parameters | ||
embedded in a `n` dimensional parameter space. | ||
For more details, see the end of the section of this [Wikipedia link](https://en.wikipedia.org/wiki/Exponential_family#Table_of_distributions) | ||
where it corresponds to Variant 1 and 2. | ||
""" | ||
struct CategoricalLikelihood{Tl<:AbstractLink} <: AbstractLikelihood | ||
invlink::Tl | ||
end | ||
|
||
CategoricalLikelihood(l=softmax) = CategoricalLikelihood(link(l)) | ||
CategoricalLikelihood(l=BijectiveSimplexLink(softmax)) = CategoricalLikelihood(link(l)) | ||
|
||
(l::CategoricalLikelihood)(f::AbstractVector{<:Real}) = Categorical(l.invlink(vcat(f, 0))) | ||
function (l::CategoricalLikelihood)(f::AbstractVector{<:Real}) | ||
return Categorical(l.invlink(f)) | ||
end | ||
|
||
function (l::CategoricalLikelihood)(fs::AbstractVector) | ||
return Product(Categorical.(l.invlink.(vcat.(fs, 0)))) | ||
return Product(Categorical.(l.invlink.(fs))) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
@testset "CategoricalLikelihood" begin | ||
for args in ((), (softmax,), (SoftMaxLink(),)) | ||
@test CategoricalLikelihood(args...) isa CategoricalLikelihood{SoftMaxLink} | ||
end | ||
@test CategoricalLikelihood() isa | ||
CategoricalLikelihood{<:GPLikelihoods.BijectiveSimplexLink} | ||
|
||
@test CategoricalLikelihood(softmax) isa CategoricalLikelihood{SoftMaxLink} | ||
@test CategoricalLikelihood(SoftMaxLink()) isa CategoricalLikelihood{SoftMaxLink} | ||
|
||
lik = CategoricalLikelihood() | ||
OUT_DIM = 4 | ||
test_interface(lik, Categorical, OUT_DIM) | ||
lik_bijective = CategoricalLikelihood() | ||
test_interface(lik_bijective, Categorical, OUT_DIM) | ||
lik_nonbijective = CategoricalLikelihood(softmax) | ||
test_interface(lik_nonbijective, Categorical, OUT_DIM) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters