-
Notifications
You must be signed in to change notification settings - Fork 40
@kernel macro for creating kernel #38
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
Closed
Closed
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2ebb65d
First version of the macro
theogf 1df722a
Added MacroTools in dependency
theogf 742428c
include file and export
theogf c804235
Correction unwanted kw
theogf 80f057b
Adding tests for the macro
theogf 2982a18
Added print for a few transforms
theogf 1d5b4fe
Applying transform on a transformed kernel results in a chain transform
theogf 3ca8da8
Merge remote-tracking branch 'origin/master' into kernel_macro
theogf d80d0bf
Merge branch 'master' into kernel_macro
theogf 3946313
Added tests and description for the macro
theogf 771b8cb
Merge branch 'master' into kernel_macro
theogf 63219ff
Added some docs and updated tests
theogf 6ebbbcf
Update src/kernels/transformedkernel.jl
theogf 87d6211
Correction on docs
theogf d7df138
Applied suggestions on @kernel and adapted tests
theogf 09ca177
Added some more transform options and tests
theogf 18c2fec
Corrected error behavior
theogf 3e18f6c
Merge branch 'master' into kernel_macro
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 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
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,39 @@ | ||
""" | ||
@kernel [variance *]kernel::Kernel [l=Real/Vector / t=transform::Transform / transform::Transform] | ||
theogf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
The `@kernel` macro is an helping alias to the [`transform`](@ref) function. | ||
The first argument should be a kernel multiplied (or not) by a scalar (variance of the kernel). | ||
The second argument (optional) can be a keyword : | ||
- `l=ρ` where `ρ` is a positive scalar or a vector of scalar | ||
- `t=transform` where `transform` is a [`Transform`](@ref) object | ||
One can also directly use a `Transform` object without a keyword. | ||
Here are some examples : | ||
```julia | ||
devmotion marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
k = @kernel SqExponentialKernel() l=3.0 | ||
k == transform(SqExponentialKernel(), ScaleTransform(3.0)) | ||
k = @kernel (MaternKernel(ν=3.0) + LinearKernel()) t=LowRankTransform(rand(4,3)) | ||
k == transform(KernelSum(MaternKernel(ν=3.0), LinearKernel()), LowRankTransform(rand(4,3))) | ||
k = @kernel 4.0*ExponentiatedKernel() ScaleTransform(3.0) | ||
k == ScaleTransform(transform(ExponentiatedKernel(), ScaleTransform(3.0)), 4.0) | ||
""" | ||
macro kernel(expr::Expr, arg = nothing) | ||
@capture(expr, (scale_ * k_ | k_)) || throw(error("@kernel first arguments should be of the form `σ * kernel` or `kernel`")) | ||
t = if @capture(arg, kw_ = val_) | ||
if kw == :l | ||
val | ||
elseif kw == :t | ||
val | ||
else | ||
throw(error("The additional argument could not be intepreted. Please see documentation of `@kernel`")) | ||
end | ||
else | ||
arg | ||
end | ||
if isnothing(scale) | ||
return esc(:(transform($k, $t))) | ||
else | ||
return esc(:($scale * transform($k, $t))) | ||
end | ||
devmotion marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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
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,9 @@ | ||
@testset "Kernel Macro" begin | ||
@test (@kernel SqExponentialKernel()) isa SqExponentialKernel | ||
@test (@kernel 3.0 * SqExponentialKernel()) isa ScaledKernel{SqExponentialKernel,Float64} | ||
@test (@kernel 3.0 * SqExponentialKernel() l=3.0) isa ScaledKernel{TransformedKernel{SqExponentialKernel,ScaleTransform{Float64}},Float64} | ||
@test (@kernel 3.0 * SqExponentialKernel() 3.0) isa ScaledKernel{TransformedKernel{SqExponentialKernel,ScaleTransform{Float64}},Float64} | ||
@test (@kernel 3.0 * SqExponentialKernel() l=[3.0]) isa ScaledKernel{TransformedKernel{SqExponentialKernel,ARDTransform{Vector{Float64}}},Float64} | ||
@test (@kernel 3.0 * SqExponentialKernel() LinearTransform(rand(3,2))) isa ScaledKernel{TransformedKernel{SqExponentialKernel,LinearTransform{Array{Float64,2}}},Float64} | ||
@test (@kernel (3.0 * SqExponentialKernel() + 5.0 * Matern32Kernel()) 3.0) isa TransformedKernel{KernelSum,ScaleTransform{Float64}} | ||
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.