Skip to content

@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
wants to merge 18 commits into from
Closed

@kernel macro for creating kernel #38

wants to merge 18 commits into from

Conversation

theogf
Copy link
Member

@theogf theogf commented Feb 28, 2020

So I started creating a first version of the @kernel macro which for now follows this syntax:

@kernel scale*Kernel(keywords arguments of the kernel) <keywordargument/argument for a transform>

So the transform is given either by a keyword l=2.0, l=[2.0,3.0] etc or directly by a parameter
For the kernel parameters they are restricted to be in the Kernel constructor

Edit : Left to do :

  • Add docs
  • Add tests

@theogf theogf requested a review from willtebbutt February 28, 2020 16:56
@theogf theogf added the WIP label Feb 28, 2020
@willtebbutt
Copy link
Member

Am busy this week. Will take a look early next week.

@willtebbutt
Copy link
Member

willtebbutt commented Mar 11, 2020

Unable to review at the minute as the @kernel macro gives the error:

julia> @kernel 5 * Matern32Kernel() l=5.0
ERROR: LoadError: UndefVarError: kw not defined
Stacktrace:
 [1] macro expansion at ./show.jl:562 [inlined]
 [2] @kernel(::LineNumberNode, ::Module, ::Expr, ::Any) at /Users/willtebbutt/.julia/dev/KernelFunctions/src/kernels/kernel_macro.jl:8
in expression starting at REPL[13]:1

@theogf
Copy link
Member Author

theogf commented Mar 11, 2020

Sorry about that! it should be fixed now

@willtebbutt
Copy link
Member

willtebbutt commented Mar 11, 2020

This seems good:

julia> (@kernel Matern32Kernel() l=[5.0, 2.0]) + (@kernel Matern52Kernel() l=[4.0, 3.0])
Sum of 2 kernels:
	- (w=1.0) Matern32Kernel
			- ARDTransform{Float64,2}([5.0, 2.0])
	- (w=1.0) Matern52Kernel
			- ARDTransform{Float64,2}([4.0, 3.0])

(the printing is already quite cool, nice work!)

This I found surprising:

julia> @kernel (Matern32Kernel() + Matern52Kernel()) l=[5.0, 4.0]
ERROR: MethodError: no method matching transform(::KernelSum, ::Array{Float64,1})
Closest candidates are:
  transform(::BaseKernel, ::AbstractArray{T,1} where T) at /Users/willtebbutt/.julia/dev/KernelFunctions/src/kernels/transformedkernel.jl:22
Stacktrace:
 [1] top-level scope at REPL[26]:1

Maybe it's just the case that transform's type constraints need to be relaxed to Kernel from BaseKernel?

Also, this doesn't seem right:

julia> @kernel Matern32Kernel() l=5.0
Matern32Kernel
	- Scale Transform s=5.0

edit: I just found this last example confusing because I was confused as to what `ScaleTransform` is doing -- it's got a very similar name to `ScaledKernel`. It might be better for change how some of this prints, but that's for a separate PR I would imagine.

@theogf
Copy link
Member Author

theogf commented Mar 11, 2020

(the printing is already quite cool)

This I found surprising:

julia> @kernel (Matern32Kernel() + Matern52Kernel()) l=[5.0, 4.0]
ERROR: MethodError: no method matching transform(::KernelSum, ::Array{Float64,1})
Closest candidates are:
  transform(::BaseKernel, ::AbstractArray{T,1} where T) at /Users/willtebbutt/.julia/dev/KernelFunctions/src/kernels/transformedkernel.jl:22
Stacktrace:
 [1] top-level scope at REPL[26]:1

Maybe it's just the case that transform's type constraints need to be relaxed to Kernel from BaseKernel?

Yeah somehow I got fixed in the idea that Transform could only be applied on a BaseKernel I will extend the transform to all kernels.

Also, this doesn't seem right:

julia> @kernel Matern32Kernel() l=5.0
Matern32Kernel
	- Scale Transform s=5.0

I don't understand why? Should we start consider the parameter l as the actual lengthscale ? (1/l)

@willtebbutt
Copy link
Member

I don't understand why? Should we start consider the parameter l as the actual lengthscale ? (1/l)

Sorry, no, I just misunderstood what is going on. I think it might be better to modify how this is printed a little bit, but that can be addressed in a separate issue. Please ignore my third point :)

@theogf theogf mentioned this pull request Apr 27, 2020
6 tasks
@theogf theogf added this to the Road do 1.0 milestone Apr 28, 2020
@theogf theogf requested a review from devmotion May 11, 2020 15:57
@theogf
Copy link
Member Author

theogf commented May 12, 2020

In terms in UI design, is it preferable to force to have a keyword, e.g. @kernel SEKernel() l = 3.0 or is it okay to throw in any argument to make it an equivalent of transform, e.g. @kernel SEKernel 3.0 ?

@devmotion
Copy link
Member

In terms in UI design, is it preferable to force to have a keyword, e.g. @kernel SEKernel() l = 3.0 or is it okay to throw in any argument to make it an equivalent of transform, e.g. @kernel SEKernel 3.0 ?

Initially I thought @kernel SEKernel() 3.0 would be too cryptic. However, I'm not so sure anymore since l = ... or t = ... is also not a completely intuitive syntax. That got me thinking if we need @kernel at all if one can just write transform(SEKernel(), 3.0) instead (but arguably the explicit syntax is completely user-friendly). At least it seems it's not needed to handle the scale with @kernel since the same syntax can be used outside of @kernel by writing 3 * @kernel SEKernel() l = 3.0 instead?

@theogf
Copy link
Member Author

theogf commented Jun 16, 2020

This is true I can completely remove the scaling. If one put it inside of outside would not matter.
@kernel is indeed an alias for transform, but should be more intuitive. But there is no absolute best solution here. I tend to prefer l=3.0 but I guess it is personal.

st-- added a commit that referenced this pull request Jan 9, 2021
…2Kernel alias (#213)

* various edits for clarity and typos
* remove reference to not-yet-implemented feature (#38)
* adds Matern12Kernel as alias for ExponentialKernel (in line with the explicitly defined Matern32Kernel and Matern52Kernel) and gives all aliases docstrings
* incorporates the lengthscales explanation from #212.

Co-authored-by: David Widmann <[email protected]>
@theogf
Copy link
Member Author

theogf commented Jan 15, 2021

Goodbye little one!

@theogf theogf closed this Jan 15, 2021
@theogf theogf deleted the kernel_macro branch January 15, 2021 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants