-
Notifications
You must be signed in to change notification settings - Fork 36
Add datatype for multi-output GP input #138
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
sharanry
merged 13 commits into
JuliaGaussianProcesses:master
from
sharanry:multi_output
Jul 23, 2020
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
eed4335
Add datatype for multi-output GP input
sharanry 4d0010e
Add IndependentKernel
sharanry 7130ce7
Address code review
sharanry 6c59aee
Address code review
sharanry 87c03c1
Address code review
sharanry 7aad315
Update Tests
sharanry 125ed8c
Redefine Indendent kernel and add kernelmatrix function
sharanry 511e557
Use block diagonal outputs and fix kernel and kix style issues
sharanry 76ff895
Remove exports for base functions.
sharanry 5c2f539
Remove MOKernel type and make MOInput a subtype of AbstractVector
sharanry abb5b90
Make Delta metric return promoted type of the input arrays
sharanry 68c007b
Promote and convert in single step
sharanry 7945695
Add more tests
sharanry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
IndependentMOKernel(k::Kernel) <: Kernel | ||
|
||
A Multi-Output kernel which assumes each output is independent of the other. | ||
""" | ||
struct IndependentMOKernel{Tkernel<:Kernel} <: Kernel | ||
kernel::Tkernel | ||
end | ||
|
||
function (κ::IndependentMOKernel)(x::Tuple{Vector, Int}, y::Tuple{Vector, Int}) | ||
if last(x) == last(y) | ||
return κ.kernel(first(x), first(y)) | ||
else | ||
return 0.0 | ||
end | ||
end | ||
|
||
function kernelmatrix(k::IndependentMOKernel, x::MOInput, y::MOInput) | ||
@assert x.out_dim == y.out_dim | ||
temp = k.kernel.(x.x, permutedims(y.x)) | ||
return cat((temp for _ in 1:y.out_dim)...; dims=(1,2)) | ||
end | ||
|
||
function Base.show(io::IO, k::IndependentMOKernel) | ||
print(io, string("Independent Multi-Output Kernel\n\t", string(k.kernel))) | ||
end |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
MOInput(x::AbstractVector, out_dim::Integer) | ||
|
||
A data type to accomodate modelling multi-dimensional output data. | ||
""" | ||
struct MOInput{T<:AbstractVector} <: AbstractVector{Tuple{Any,Int}} | ||
x::T | ||
out_dim::Integer | ||
end | ||
|
||
Base.length(inp::MOInput) = inp.out_dim * length(inp.x) | ||
|
||
Base.size(inp::MOInput, d) = d::Integer == 1 ? inp.out_dim * size(inp.x, 1) : 1 | ||
Base.size(inp::MOInput) = (inp.out_dim * size(inp.x, 1),) | ||
|
||
Base.lastindex(inp::MOInput) = length(inp) | ||
Base.firstindex(inp::MOInput) = 1 | ||
|
||
function Base.getindex(inp::MOInput, ind::Integer) | ||
if ind > 0 | ||
out_dim = ind ÷ length(inp.x) + 1 | ||
ind = ind % length(inp.x) | ||
if ind==0 ind = length(inp.x); out_dim-=1 end | ||
return (inp.x[ind], out_dim::Int) | ||
else | ||
throw(BoundsError(string("Trying to access at ", ind))) | ||
end | ||
end | ||
|
||
Base.iterate(inp::MOInput) = (inp[1], 1) | ||
Base.iterate(inp::MOInput, state) = (state<length(inp)) ? (inp[state + 1], state + 1) : nothing |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@testset "independent" begin | ||
x = MOInput([rand(5) for _ in 1:4], 3) | ||
y = MOInput([rand(5) for _ in 1:4], 3) | ||
|
||
k = IndependentMOKernel(GaussianKernel()) | ||
@test k isa IndependentMOKernel | ||
@test k isa Kernel | ||
@test k.kernel isa KernelFunctions.BaseKernel | ||
@test k(x[2], y[2]) isa Real | ||
|
||
@test kernelmatrix(k, x, y) == kernelmatrix(k, collect(x), collect(y)) | ||
@test kernelmatrix(k, x, x) == kernelmatrix(k, x) | ||
@test string(k) == "Independent Multi-Output Kernel\n\tSquared Exponential Kernel" | ||
end |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@testset "moinput" begin | ||
|
||
x = [rand(5) for _ in 1:4] | ||
mgpi = MOInput(x, 3) | ||
|
||
@test length(mgpi) == 12 | ||
@test size(mgpi) == (12,) | ||
@test size(mgpi, 1) == 12 | ||
@test size(mgpi, 2) == 1 | ||
@test lastindex(mgpi) == 12 | ||
@test firstindex(mgpi) == 1 | ||
@test iterate(mgpi) == (mgpi[1], 1) | ||
@test iterate(mgpi, 2) == (mgpi[3], 3) | ||
@test_throws BoundsError mgpi[0] | ||
|
||
@test mgpi[2] == (x[2], 1) | ||
@test mgpi[5] == (x[1], 2) | ||
@test mgpi[7] == (x[3], 2) | ||
@test all([(x_, i) for i in 1:3 for x_ in x ] .== mgpi) | ||
|
||
end |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.