Kernel layers¶
UnlockNN provides Keras-compatible implementations of a few kernel functions for use with the Gaussian process, as well as an extensible API for implementing your own kernels.
Built-in kernels¶
|
A radial basis function implementation that works with keras. |
|
A Matern kernel with parameter 1/2 implementation that works with keras. |
Each of these kernels can be saved using their KernelLayer.save() method and then
loaded using load_kernel():
- unlocknn.kernel_layers.load_kernel(directory: PathLike, kernel_type: Optional[Type[KernelLayer]] = None) KernelLayer¶
Load a kernel from the disk.
- Parameters
directory – Path to the kernel save folder.
kernel_type – A specific class to force the model to load. Useful for custom kernels which don’t appear in this module.
Examples
Create an
RBFKernelFn, save it, then reload from disk:>>> from tempfile import TemporaryDirectory >>> rbf = RBFKernelFn() >>> with TemporaryDirectory() as tmpdirname: ... rbf.save(tmpdirname) ... new_rbf = load_kernel(tmpdirname)
Extending the kernels API¶
All kernels should:
Inherit from the
KernelLayerabstract base classImplement the
KernelLayer.kernel()propertyAdd appropriate “weights” (kernel parameters) during
KernelLayer.__init__()Implement
KernelLayer.config()for compatibility withload_kernel()
- class unlocknn.kernel_layers.KernelLayer(*args, **kwargs)¶
An ABC for kernel function implementations that work with keras.
- __init__(trainable=True, name=None, dtype=None, dynamic=False, **kwargs)¶
Initialize kernel parameters.
Subclasses should use
add_weight()to initialize kernel layer weights during initialization.
- call(x)¶
Do nothing – a placeholder for keras.
- abstract property config: dict¶
Get configuration information for the class.
This configuration information is used by
load_kernel()to determine what type of kernel to load. For built-in kernels, this configuration dictionary specifies{"type": type}, wheretypeis one of the keys inKERNEL_TYPES.For custom kernels, this dictionary should return any keyword arguments to be passed during kernel initialization.
- abstract property kernel: PositiveSemidefiniteKernel¶
Get a callable kernel.
- save(directory: PathLike)¶
Save the kernel to disk.
Subclasses should update this method to save configuration information as well (c.f.
config()). See alsoload_kernel().
Both of the current built-in kernels are parameterized by an amplitude and
a length scale. The AmpAndLengthScaleFn abstract base class initializes
these weights during instantiation and other kernels that are parameterized as
such may inherit from this class also:
- class unlocknn.kernel_layers.AmpAndLengthScaleFn(*args, **kwargs)¶
An ABC for kernels with amplitude and length scale parameters.
- _amplitude_basis¶
The basis for the kernel amplitude, which is passed through a softplus to calculate the actual amplitude.
- Type
tf.Tensor
- _length_scale_basis¶
The basis for the length scale of the kernel. which is passed through a softplus to calculate the actual amplitude.
- Type
tf.Tensor
- __init__(**kwargs)¶
Initialize the layer, its amplitude and its length scale.
- property amplitude: Tensor¶
Get the current kernel amplitude.
- property length_scale: Tensor¶
Get the current kernel length scale.