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

General out indices #511

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions src/matrix/kernelkroneckermat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ efficiently inverted or decomposed. See also [`kernelmatrix`](@ref).
function kronecker_kernelmatrix(
k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, x::MOI, y::MOI
) where {MOI<:IsotopicMOInputsUnion}
x.out_dim == y.out_dim ||
throw(DimensionMismatch("`x` and `y` must have the same `out_dim`"))
x.outIndices == y.outIndices ||
FelixBenning marked this conversation as resolved.
Show resolved Hide resolved
throw(DimensionMismatch("`x` and `y` must have the same `outIndices`"))
Kfeatures = kernelmatrix(k.kernel, x.x, y.x)
Koutputs = _mo_output_covariance(k, x.out_dim)
Koutputs = _mo_output_covariance(k, length(x.outIndices))
return _kernelmatrix_kroneckerjl_helper(MOI, Kfeatures, Koutputs)
end

function kronecker_kernelmatrix(
k::Union{IndependentMOKernel,IntrinsicCoregionMOKernel}, x::MOI
) where {MOI<:IsotopicMOInputsUnion}
Kfeatures = kernelmatrix(k.kernel, x.x)
Koutputs = _mo_output_covariance(k, x.out_dim)
Koutputs = _mo_output_covariance(k, length(x.outIndices))
return _kernelmatrix_kroneckerjl_helper(MOI, Kfeatures, Koutputs)
end

Expand Down
14 changes: 7 additions & 7 deletions src/mokernels/independent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ function (κ::IndependentMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,I
return κ.kernel(x, y) * (px == py)
end

_mo_output_covariance(k::IndependentMOKernel, out_dim) = Eye{Bool}(out_dim)
_mo_output_covariance(k::IndependentMOKernel, out_dim::Integer) = Eye{Bool}(out_dim)

function kernelmatrix(
k::IndependentMOKernel, x::MOI, y::MOI
) where {MOI<:IsotopicMOInputsUnion}
x.out_dim == y.out_dim ||
throw(DimensionMismatch("`x` and `y` must have the same `out_dim`"))
x.outIndices == y.outIndices ||
throw(DimensionMismatch("`x` and `y` must have the same `outIndices`"))
Kfeatures = kernelmatrix(k.kernel, x.x, y.x)
Koutputs = _mo_output_covariance(k, x.out_dim)
Koutputs = _mo_output_covariance(k, length(x.outIndices))
return _kernelmatrix_kron_helper(MOI, Kfeatures, Koutputs)
end

if VERSION >= v"1.6"
function kernelmatrix!(
K::AbstractMatrix, k::IndependentMOKernel, x::MOI, y::MOI
) where {MOI<:IsotopicMOInputsUnion}
x.out_dim == y.out_dim ||
throw(DimensionMismatch("`x` and `y` must have the same `out_dim`"))
x.outIndices == y.outIndices ||
throw(DimensionMismatch("`x` and `y` must have the same `outIndices`"))
Kfeatures = kernelmatrix(k.kernel, x.x, y.x)
Koutputs = _mo_output_covariance(k, x.out_dim)
Koutputs = _mo_output_covariance(k, length(x.outIndices))
return _kernelmatrix_kron_helper!(K, MOI, Kfeatures, Koutputs)
end
end
Expand Down
14 changes: 7 additions & 7 deletions src/mokernels/intrinsiccoregion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,29 @@ function (k::IntrinsicCoregionMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{
return k.B[px, py] * k.kernel(x, y)
end

function _mo_output_covariance(k::IntrinsicCoregionMOKernel, out_dim)
function _mo_output_covariance(k::IntrinsicCoregionMOKernel, out_dim::Integer)
@assert size(k.B) == (out_dim, out_dim)
return k.B
end

function kernelmatrix(
k::IntrinsicCoregionMOKernel, x::MOI, y::MOI
) where {MOI<:IsotopicMOInputsUnion}
x.out_dim == y.out_dim ||
throw(DimensionMismatch("`x` and `y` must have the same `out_dim`"))
x.outIndices == y.outIndices ||
throw(DimensionMismatch("`x` and `y` must have the same `outIndices`"))
Kfeatures = kernelmatrix(k.kernel, x.x, y.x)
Koutputs = _mo_output_covariance(k, x.out_dim)
Koutputs = _mo_output_covariance(k, length(x.outIndices))
return _kernelmatrix_kron_helper(MOI, Kfeatures, Koutputs)
end

if VERSION >= v"1.6"
function kernelmatrix!(
K::AbstractMatrix, k::IntrinsicCoregionMOKernel, x::MOI, y::MOI
) where {MOI<:IsotopicMOInputsUnion}
x.out_dim == y.out_dim ||
throw(DimensionMismatch("`x` and `y` must have the same `out_dim`"))
x.outIndices == y.outIndices ||
throw(DimensionMismatch("`x` and `y` must have the same `outIndices`"))
Kfeatures = kernelmatrix(k.kernel, x.x, y.x)
Koutputs = _mo_output_covariance(k, x.out_dim)
Koutputs = _mo_output_covariance(k, length(x.outIndices))
return _kernelmatrix_kron_helper!(K, MOI, Kfeatures, Koutputs)
end
end
Expand Down
42 changes: 28 additions & 14 deletions src/mokernels/moinput.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ The first `out_dim` elements represent all outputs for the first input, the seco

See [Inputs for Multiple Outputs](@ref) in the docs for more info.
"""
struct MOInputIsotopicByFeatures{S,T<:AbstractVector{S},Tout_dim<:Integer} <:
AbstractVector{Tuple{S,Int}}
struct MOInputIsotopicByFeatures{
Copy link
Member

Choose a reason for hiding this comment

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

Changing the type parameters is a breaking change.

Copy link
Author

Choose a reason for hiding this comment

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

really? :/

S,T<:AbstractVector{S},IdxType,ToutIndices<:AbstractVector{IdxType}
FelixBenning marked this conversation as resolved.
Show resolved Hide resolved
} <: AbstractVector{Tuple{S,IdxType}}
x::T
out_dim::Tout_dim
outIndices::ToutIndices
end

function MOInputIsotopicByFeatures(
x::T, out_dim::Tout_dim
FelixBenning marked this conversation as resolved.
Show resolved Hide resolved
) where {S,T<:AbstractVector{S},Tout_dim<:Integer}
FelixBenning marked this conversation as resolved.
Show resolved Hide resolved
return MOInputIsotopicByFeatures{S,T,Int,Base.OneTo{Tout_dim}}(x, Base.OneTo(out_dim))
FelixBenning marked this conversation as resolved.
Show resolved Hide resolved
end

"""
Expand All @@ -54,10 +61,17 @@ As shown above, an `MOInputIsotopicByOutputs` represents a vector of tuples.
The first `length(x)` elements represent the inputs for the first output, the second
`length(x)` elements represent the inputs for the second output, etc.
"""
struct MOInputIsotopicByOutputs{S,T<:AbstractVector{S},Tout_dim<:Integer} <:
AbstractVector{Tuple{S,Int}}
struct MOInputIsotopicByOutputs{
S,T<:AbstractVector{S},IdxType,ToutIndices<:AbstractVector{IdxType}
FelixBenning marked this conversation as resolved.
Show resolved Hide resolved
} <: AbstractVector{Tuple{S,IdxType}}
x::T
out_dim::Tout_dim
outIndices::ToutIndices
end

function MOInputIsotopicByOutputs(
x::T, out_dim::Tout_dim
FelixBenning marked this conversation as resolved.
Show resolved Hide resolved
) where {S,T<:AbstractVector{S},Tout_dim<:Integer}
FelixBenning marked this conversation as resolved.
Show resolved Hide resolved
return MOInputIsotopicByOutputs{S,T,Int,Base.OneTo{Tout_dim}}(x, Base.OneTo(out_dim))
FelixBenning marked this conversation as resolved.
Show resolved Hide resolved
end

const IsotopicMOInputsUnion = Union{MOInputIsotopicByFeatures,MOInputIsotopicByOutputs}
Expand All @@ -66,26 +80,26 @@ function Base.getindex(inp::MOInputIsotopicByOutputs, ind::Integer)
@boundscheck checkbounds(inp, ind)
output_index, feature_index = fldmod1(ind, length(inp.x))
feature = @inbounds inp.x[feature_index]
return feature, output_index
return feature, @inbounds inp.outIndices[output_index]
FelixBenning marked this conversation as resolved.
Show resolved Hide resolved
end

function Base.getindex(inp::MOInputIsotopicByFeatures, ind::Integer)
@boundscheck checkbounds(inp, ind)
feature_index, output_index = fldmod1(ind, inp.out_dim)
feature_index, output_index = fldmod1(ind, length(inp.outIndices))
feature = @inbounds inp.x[feature_index]
return feature, output_index
return feature, @inbounds inp.outIndices[output_index]
end

Base.size(inp::IsotopicMOInputsUnion) = (inp.out_dim * length(inp.x),)
Base.size(inp::IsotopicMOInputsUnion) = (length(inp.outIndices) * length(inp.x),)

function Base.vcat(x::MOInputIsotopicByFeatures, y::MOInputIsotopicByFeatures)
x.out_dim == y.out_dim || throw(DimensionMismatch("out_dim mismatch"))
return MOInputIsotopicByFeatures(vcat(x.x, y.x), x.out_dim)
x.outIndices == y.outIndices || throw(DimensionMismatch("outIndices mismatch"))
return MOInputIsotopicByFeatures(vcat(x.x, y.x), x.outIndices)
end

function Base.vcat(x::MOInputIsotopicByOutputs, y::MOInputIsotopicByOutputs)
x.out_dim == y.out_dim || throw(DimensionMismatch("out_dim mismatch"))
return MOInputIsotopicByOutputs(vcat(x.x, y.x), x.out_dim)
x.outIndices == y.outIndices || throw(DimensionMismatch("outIndices mismatch"))
return MOInputIsotopicByOutputs(vcat(x.x, y.x), x.outIndices)
end

"""
Expand Down
5 changes: 3 additions & 2 deletions src/mokernels/slfm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ function (κ::LatentFactorMOKernel)((x, px)::Tuple{Any,Int}, (y, py)::Tuple{Any,
end

function kernelmatrix(k::LatentFactorMOKernel, x::MOInput, y::MOInput)
x.out_dim == y.out_dim || error("`x` and `y` should have the same output dimension")
x.out_dim == size(k.A, 1) ||
x.outIndices == y.outIndices ||
error("`x` and `y` should have the same output dimension")
length(x.outIndices) == size(k.A, 1) ||
error("Kernel not compatible with the given multi-output inputs")

# Weights matrix ((out_dim x out_dim) x length(k.g))
Expand Down