Input Transforms

Overview

Transforms are designed to change input data before passing it on to a kernel object.

It can be as standard as IdentityTransform returning the same input, or multiplying the data by a scalar with ScaleTransform or by a vector with ARDTransform. There is a more general FunctionTransform that uses a function and applies it to each input.

You can also create a pipeline of Transforms via ChainTransform, e.g.,

LowRankTransform(rand(10, 5)) ∘ ScaleTransform(2.0)

A transformation t can be applied to a single input x with t(x) and to multiple inputs xs with map(t, xs).

Kernels can be coupled with input transformations with or its alias compose. It falls back to creating a TransformedKernel but allows more optimized implementations for specific kernels and transformations.

List of Input Transforms

KernelFunctions.ScaleTransformType
ScaleTransform(l::Real)

Transformation that multiplies the input elementwise with l.

Examples

julia> l = rand(); t = ScaleTransform(l); X = rand(100, 10);

julia> map(t, ColVecs(X)) == ColVecs(l .* X)
true
source
KernelFunctions.ARDTransformType
ARDTransform(v::AbstractVector)

Transformation that multiplies the input elementwise by v.

Examples

julia> v = rand(10); t = ARDTransform(v); X = rand(10, 100);

julia> map(t, ColVecs(X)) == ColVecs(v .* X)
true
source
KernelFunctions.LinearTransformType
LinearTransform(A::AbstractMatrix)

Linear transformation of the input realised by the matrix A.

The second dimension of A must match the number of features of the target.

Examples

julia> A = rand(10, 5); t = LinearTransform(A); X = rand(5, 100);

julia> map(t, ColVecs(X)) == ColVecs(A * X)
true
source
KernelFunctions.FunctionTransformType
FunctionTransform(f)

Transformation that applies function f to the input.

Make sure that f can act on an input. For instance, if the inputs are vectors, use f(x) = sin.(x) instead of f = sin.

Examples

julia> f(x) = sum(x); t = FunctionTransform(f); X = randn(100, 10);

julia> map(t, ColVecs(X)) == ColVecs(sum(X; dims=1))
true
source
KernelFunctions.SelectTransformType
SelectTransform(dims)

Transformation that selects the dimensions dims of the input.

Examples

julia> dims = [1, 3, 5, 6, 7]; t = SelectTransform(dims); X = rand(100, 10);

julia> map(t, ColVecs(X)) == ColVecs(X[dims, :])
true
source
KernelFunctions.ChainTransformType
ChainTransform(transforms)

Transformation that applies a chain of transformations ts to the input.

The transformation first(ts) is applied first.

Examples

julia> l = rand(); A = rand(3, 4); t1 = ScaleTransform(l); t2 = LinearTransform(A);

julia> X = rand(4, 10);

julia> map(ChainTransform([t1, t2]), ColVecs(X)) == ColVecs(A * (l .* X))
true

julia> map(t2 ∘ t1, ColVecs(X)) == ColVecs(A * (l .* X))
true
source
KernelFunctions.PeriodicTransformType
PeriodicTransform(f)

Transformation that maps the input elementwise onto the unit circle with frequency f.

Samples from a GP with a kernel with this transformation applied to the inputs will produce samples with frequency f.

Examples

julia> f = rand(); t = PeriodicTransform(f); x = rand();

julia> t(x) == [sinpi(2 * f * x), cospi(2 * f * x)]
true
source

Convenience functions

KernelFunctions.with_lengthscaleFunction
with_lengthscale(kernel::Kernel, lengthscale::Real)

Construct a transformed kernel with lengthscale.

Examples

julia> kernel = with_lengthscale(SqExponentialKernel(), 2.5);

julia> x = rand(2);

julia> y = rand(2);

julia> kernel(x, y) ≈ (SqExponentialKernel() ∘ ScaleTransform(0.4))(x, y)
true
source
with_lengthscale(kernel::Kernel, lengthscales::AbstractVector{<:Real})

Construct a transformed "ARD" kernel with different lengthscales for each dimension.

Examples

julia> kernel = with_lengthscale(SqExponentialKernel(), [0.5, 2.5]);

julia> x = rand(2);

julia> y = rand(2);

julia> kernel(x, y) ≈ (SqExponentialKernel() ∘ ARDTransform([2, 0.4]))(x, y)
true
source
KernelFunctions.median_heuristic_transformFunction
median_heuristic_transform(distance, x::AbstractVector)

Create a ScaleTransform that divides the input elementwise by the median distance of the data points in x.

The distance has to support pairwise evaluation with KernelFunctions.pairwise. All PreMetrics of the package Distances.jl such as Euclidean satisfy this requirement automatically.

Examples

julia> using Distances, Statistics

julia> x = ColVecs(rand(100, 10));

julia> t = median_heuristic_transform(Euclidean(), x);

julia> y = map(t, x);

julia> median(euclidean(y[i], y[j]) for i in 1:10, j in 1:10 if i != j) ≈ 1
true
source