Input Transforms
Overview
Transform
s 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 Transform
s 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.Transform
— TypeTransform
Abstract type defining a transformation of the input.
KernelFunctions.IdentityTransform
— TypeIdentityTransform()
Transformation that returns exactly the input.
KernelFunctions.ScaleTransform
— TypeScaleTransform(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
KernelFunctions.ARDTransform
— TypeARDTransform(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
KernelFunctions.ARDTransform
— MethodARDTransform(s::Real, dims::Integer)
Create an ARDTransform
with vector fill(s, dims)
.
KernelFunctions.LinearTransform
— TypeLinearTransform(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
KernelFunctions.FunctionTransform
— TypeFunctionTransform(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
KernelFunctions.SelectTransform
— TypeSelectTransform(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
KernelFunctions.ChainTransform
— TypeChainTransform(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
KernelFunctions.PeriodicTransform
— TypePeriodicTransform(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
Convenience functions
KernelFunctions.with_lengthscale
— Functionwith_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
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
KernelFunctions.median_heuristic_transform
— Functionmedian_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 PreMetric
s 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