-
Notifications
You must be signed in to change notification settings - Fork 34
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
Removing transform field and creating TransformedKernel (and ScaledKernel) #32
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
7ee9279
Removed transform fiels from all base kernels
theogf ad53f32
Added ScaledKernel and TransformedKernel
theogf 836265f
Fixing all tests
theogf d1e9efc
Fixed typo
theogf 0e629b2
Making constructors uniform
theogf af12fe8
Removed transform for base kernels and rewrote the generic for kappa
theogf 8cbc30e
Esthetic corrections
theogf d5d077c
Removed inner constructor kernelsum
theogf 197f663
Set obsdim as a keyword for transform
theogf e8f4e00
Readded a transform(kernel,x) function
theogf 70b508f
Removed the transform function and adapted the kernelmatrix functions
theogf 6559873
Adapting test to the new backend
theogf ba12bc8
Removed all @inline and added BaseKernel
theogf 937a235
Improved tests on KernelProduct and KernelSum
theogf 22d2992
Readapted the generic methods to subtypes of BaseKernel with subtypes
theogf 54c4872
Started readapting documentation
theogf f3f85bf
Removed depecrated `duplicate` function
theogf 3d9a52e
Moved to keyword based constructors for BaseKernel
theogf f6c3e7a
Adapting tests and constructors
theogf efb7d8c
First work on printing output
theogf a267406
Print of the full kernel structure via recursion
theogf fe5487e
Corrected transform behavior as a constructor
theogf 486ba20
Merge branch 'master' into remove-transform
theogf ee6fc0a
Readding StatsBase
theogf 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 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,3 +1,4 @@ | ||
*.json | ||
*.cov | ||
Manifest.toml | ||
coverage/ |
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
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
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,33 +1,32 @@ | ||
@inline metric(κ::Kernel) = κ.metric | ||
|
||
## Allows to iterate over kernels | ||
Base.length(::Kernel) = 1 | ||
Base.iterate(k::Kernel) = (k,nothing) | ||
Base.iterate(k::Kernel, ::Any) = nothing | ||
|
||
# default fallback for evaluating a kernel with two arguments (such as vectors etc) | ||
kappa(κ::Kernel, x, y) = kappa(κ, evaluate(metric(κ), transform(κ, x), transform(κ, y))) | ||
kappa(κ::Kernel, x, y) = kappa(κ, evaluate(metric(κ), x, y)) | ||
kappa(κ::TransformedKernel, x, y) = kappa(kernel(κ), apply(κ.transform,x), apply(κ.transform,y)) | ||
kappa(κ::TransformedKernel{<:BaseKernel,<:ScaleTransform}, x, y) = kappa(κ, _scale(κ.transform, metric(κ), x, y)) | ||
_scale(t::ScaleTransform, metric::Euclidean, x, y) = first(t.s) * evaluate(metric, x, y) | ||
_scale(t::ScaleTransform, metric::Union{SqEuclidean,DotProduct}, x, y) = first(t.s)^2 * evaluate(metric, x, y) | ||
_scale(t::ScaleTransform, metric, x, y) = evaluate(metric, apply(t, x), apply(t, y)) | ||
|
||
printshifted(io::IO,κ::Kernel,shift::Int) = print(io,"$κ") | ||
Base.show(io::IO,κ::Kernel) = print(io,nameof(typeof(κ))) | ||
|
||
### Syntactic sugar for creating matrices and using kernel functions | ||
for k in [:ExponentialKernel,:SqExponentialKernel,:GammaExponentialKernel,:MaternKernel,:Matern32Kernel,:Matern52Kernel,:LinearKernel,:PolynomialKernel,:ExponentiatedKernel,:ZeroKernel,:WhiteKernel,:ConstantKernel,:RationalQuadraticKernel,:GammaRationalQuadraticKernel] | ||
for k in subtypes(BaseKernel) | ||
@eval begin | ||
@inline (κ::$k)(d::Real) = kappa(κ,d) #TODO Add test | ||
@inline (κ::$k)(x::AbstractVector{<:Real}, y::AbstractVector{<:Real}) = kappa(κ, x, y) | ||
@inline (κ::$k)(X::AbstractMatrix{T},Y::AbstractMatrix{T};obsdim::Integer=defaultobs) where {T} = kernelmatrix(κ,X,Y,obsdim=obsdim) | ||
@inline (κ::$k)(X::AbstractMatrix{T};obsdim::Integer=defaultobs) where {T} = kernelmatrix(κ,X,obsdim=obsdim) | ||
@inline (κ::$k)(X::AbstractMatrix{T}, Y::AbstractMatrix{T}; obsdim::Integer=defaultobs) where {T} = kernelmatrix(κ, X, Y, obsdim=obsdim) | ||
@inline (κ::$k)(X::AbstractMatrix{T}; obsdim::Integer=defaultobs) where {T} = kernelmatrix(κ, X, obsdim=obsdim) | ||
end | ||
end | ||
|
||
### Transform generics | ||
@inline transform(κ::Kernel) = κ.transform | ||
@inline transform(κ::Kernel, x) = transform(transform(κ), x) | ||
@inline transform(κ::Kernel, x, obsdim::Int) = transform(transform(κ), x, obsdim) | ||
|
||
## Constructors for kernels without parameters | ||
for kernel in [:ExponentialKernel,:SqExponentialKernel,:Matern32Kernel,:Matern52Kernel,:ExponentiatedKernel] | ||
for k in nameof.(subtypes(BaseKernel)) | ||
@eval begin | ||
$kernel() = $kernel(IdentityTransform()) | ||
$kernel(ρ::Real) = $kernel(ScaleTransform(ρ)) | ||
$kernel(ρ::AbstractVector{<:Real}) = $kernel(ARDTransform(ρ)) | ||
@deprecate($k(ρ::Real;args...),transform($k(args...),ρ)) | ||
@deprecate($k(ρ::AbstractVector{<:Real};args...),transform($k(args...),ρ)) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,49 @@ | ||
""" | ||
ZeroKernel([tr=IdentityTransform()]) | ||
ZeroKernel() | ||
|
||
Create a kernel always returning zero | ||
Create a kernel that always returning zero | ||
``` | ||
κ(x,y) = 0.0 | ||
``` | ||
The output type depends of `x` and `y` | ||
""" | ||
struct ZeroKernel{Tr} <: Kernel{Tr} | ||
transform::Tr | ||
end | ||
|
||
ZeroKernel() = ZeroKernel(IdentityTransform()) | ||
struct ZeroKernel <: BaseKernel end | ||
|
||
@inline kappa(κ::ZeroKernel, d::T) where {T<:Real} = zero(T) | ||
kappa(κ::ZeroKernel, d::T) where {T<:Real} = zero(T) | ||
|
||
metric(::ZeroKernel) = Delta() | ||
|
||
""" | ||
`WhiteKernel([tr=IdentityTransform()])` | ||
`WhiteKernel()` | ||
|
||
``` | ||
κ(x,y) = δ(x,y) | ||
``` | ||
Kernel function working as an equivalent to add white noise. | ||
""" | ||
struct WhiteKernel{Tr} <: Kernel{Tr} | ||
transform::Tr | ||
end | ||
|
||
WhiteKernel() = WhiteKernel(IdentityTransform()) | ||
struct WhiteKernel <: BaseKernel end | ||
|
||
@inline kappa(κ::WhiteKernel,δₓₓ::Real) = δₓₓ | ||
kappa(κ::WhiteKernel,δₓₓ::Real) = δₓₓ | ||
|
||
metric(::WhiteKernel) = Delta() | ||
|
||
""" | ||
`ConstantKernel([tr=IdentityTransform(),[c=1.0]])` | ||
`ConstantKernel(c=1.0)` | ||
``` | ||
κ(x,y) = c | ||
``` | ||
Kernel function always returning a constant value `c` | ||
""" | ||
struct ConstantKernel{Tr, Tc<:Real} <: Kernel{Tr} | ||
transform::Tr | ||
struct ConstantKernel{Tc<:Real} <: BaseKernel | ||
c::Tc | ||
function ConstantKernel(;c::T=1.0) where {T<:Real} | ||
new{T}(c) | ||
end | ||
end | ||
|
||
params(k::ConstantKernel) = (params(k.transform),k.c) | ||
opt_params(k::ConstantKernel) = (opt_params(k.transform),k.c) | ||
|
||
ConstantKernel(c::Real=1.0) = ConstantKernel(IdentityTransform(),c) | ||
|
||
ConstantKernel(t::Tr,c::Tc=1.0) where {Tr<:Transform,Tc<:Real} = ConstantKernel{Tr,Tc}(t,c) | ||
params(k::ConstantKernel) = (k.c,) | ||
opt_params(k::ConstantKernel) = (k.c,) | ||
|
||
@inline kappa(κ::ConstantKernel,x::Real) = κ.c | ||
kappa(κ::ConstantKernel,x::Real) = κ.c*one(x) | ||
|
||
metric(::ConstantKernel) = Delta() |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, that does not add the syntactic sugar for user-defined
BaseKernel
s - in the future (when support for Julia < 1.3 is dropped) this can be done in a nice way due to JuliaLang/julia#31916: one can just defineand so on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I did not know there was this new feature in 1.3.
I was thinking for now and as an alternative to create a macro that would do the same on a user-defined kernel. Including creating the
TransformedKernel
wrapper