kernels

package
v0.0.0-...-beb861e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Diff3D

func Diff3D(a, b tensor.Tensor, tol float64) error

Diff3D returns an error if input tensors max abs difference exceeds tol

func NewMatFromFunc

func NewMatFromFunc(r, c int, at func(i, j int) float64, set func(i, j int, v float64)) mat.Matrix

NewMatFromFunc ...

Types

type ConstantKernel

type ConstantKernel struct {
	ConstantValue       float64
	ConstantValueBounds [2]float64
	StationaryKernelMixin
}

ConstantKernel can be used as part of a product-kernel where it scales the magnitude of the other factor (kernel) or as part of a sum-kernel, where it modifies the mean of the Gaussian process. k(x_1, x_2) = constant_value for all x_1, x_2

Example
// np.random.seed(1)
state := randomkit.NewRandomkitSource(1)
// X=np.reshape(np.random.sample(6),(3,2))
X, Y := sample(state, 3, 2), sample(state, 3, 2)
K := &ConstantKernel{ConstantValue: 1.23}
fmt.Printf("K=%s, stationary:%v\n", K, K.IsStationary())
KXY, _ := K.Eval(X, Y, false)
KXX := K.Diag(X)
fmt.Printf("X=\n%.8f\nY=\n%.8f\nK(X,Y)=\n%.8f\nK(X,X)=\n%.8f\n", mat.Formatted(X), mat.Formatted(Y), mat.Formatted(KXY), mat.Formatted(KXX))
Output:

K=1.11**2, stationary:true
X=
⎡0.41702200  0.72032449⎤
⎢0.00011437  0.30233257⎥
⎣0.14675589  0.09233859⎦
Y=
⎡0.18626021  0.34556073⎤
⎢0.39676747  0.53881673⎥
⎣0.41919451  0.68521950⎦
K(X,Y)=
⎡1.23000000  1.23000000  1.23000000⎤
⎢1.23000000  1.23000000  1.23000000⎥
⎣1.23000000  1.23000000  1.23000000⎦
K(X,X)=
⎡1.23000000  0.00000000  0.00000000⎤
⎢0.00000000  1.23000000  0.00000000⎥
⎣0.00000000  0.00000000  1.23000000⎦

func (ConstantKernel) Bounds

func (k ConstantKernel) Bounds() mat.Matrix

Bounds ...

func (ConstantKernel) CloneWithTheta

func (k ConstantKernel) CloneWithTheta(theta mat.Matrix) Kernel

CloneWithTheta ...

func (*ConstantKernel) Diag

func (k *ConstantKernel) Diag(X mat.Matrix) (K *mat.DiagDense)

Diag returns the diagonal of the kernel k(X, X).

func (*ConstantKernel) Eval

func (k *ConstantKernel) Eval(X, Y mat.Matrix, evalGradient bool) (*mat.Dense, *t.Dense)

Eval returns K : array, shape (n_samples_X, n_samples_Y) Kernel k(X, Y)

func (*ConstantKernel) String

func (k *ConstantKernel) String() string

String returns string representation of kernel

func (*ConstantKernel) Theta

func (k *ConstantKernel) Theta() mat.Matrix

Theta ...

type DotProduct

type DotProduct struct {
	Sigma0       float64
	Sigma0Bounds [2]float64
}

DotProduct kernel The DotProduct kernel is non-stationary and can be obtained from linear regression by putting N(0, 1) priors on the coefficients of x_d (d = 1, . . . , D) and a prior of N(0, \sigma_0^2) on the bias. The DotProduct kernel is invariant to a rotation of the coordinates about the origin, but not translations. It is parameterized by a parameter sigma_0^2. For sigma_0^2 =0, the kernel is called the homogeneous linear kernel, otherwise it is inhomogeneous. The kernel is given by k(x_i, x_j) = sigma_0 ^ 2 + x_i \cdot x_j The DotProduct kernel is commonly combined with exponentiation.

Example
// np.random.seed(1)
state := randomkit.NewRandomkitSource(1)
// X=np.reshape(np.random.sample(6),(3,2))
X, Y := sample(state, 3, 2), sample(state, 3, 2)
// K=DotProduct(sigma_0=1.23)
K := &DotProduct{Sigma0: 1.23}
fmt.Printf("K=%s, stationary:%v\n", K, K.IsStationary())

KXY, _ := K.Eval(X, Y, false)
KXX := K.Diag(X)
fmt.Printf("X=\n%.8f\nY=\n%.8f\nK(X,Y)=\n%.8f\nK(X,X)=\n%.8f\n", mat.Formatted(X), mat.Formatted(Y), mat.Formatted(KXY), mat.Formatted(KXX))
Output:

K=DotProduct(sigma_0=1.23), stationary:false
X=
⎡0.41702200  0.72032449⎤
⎢0.00011437  0.30233257⎥
⎣0.14675589  0.09233859⎦
Y=
⎡0.18626021  0.34556073⎤
⎢0.39676747  0.53881673⎥
⎣0.41919451  0.68521950⎦
K(X,Y)=
⎡1.83949046  2.06648366  2.18129373⎤
⎢1.61739557  1.67584723  1.72011212⎥
⎣1.57214338  1.62088154  1.63769147⎦
K(X,X)=
⎡2.20567473  0.00000000  0.00000000⎤
⎢0.00000000  1.60430500  0.00000000⎥
⎣0.00000000  0.00000000  1.54296371⎦

func (DotProduct) Bounds

func (k DotProduct) Bounds() mat.Matrix

Bounds ...

func (DotProduct) CloneWithTheta

func (k DotProduct) CloneWithTheta(theta mat.Matrix) Kernel

CloneWithTheta ...

func (*DotProduct) Diag

func (k *DotProduct) Diag(X mat.Matrix) (K *mat.DiagDense)

Diag returns the diagonal of the kernel k(X, X)

func (*DotProduct) Eval

func (k *DotProduct) Eval(X, Y mat.Matrix, evalGradient bool) (K *mat.Dense, Kg *t.Dense)

Eval return the kernel k(X, Y)

func (*DotProduct) IsStationary

func (k *DotProduct) IsStationary() bool

IsStationary returns whether the kernel is stationary

func (*DotProduct) String

func (k *DotProduct) String() string

String returns string representation of kernel

func (*DotProduct) Theta

func (k *DotProduct) Theta() mat.Matrix

Theta ...

type Exponentiation

type Exponentiation struct {
	Kernel
	Exponent float64
}

Exponentiation exponentiate kernel by given exponent

func (*Exponentiation) Diag

func (k *Exponentiation) Diag(X mat.Matrix) *mat.DiagDense

Diag returns the diagonal of the kernel k(X, X)

func (*Exponentiation) Eval

func (k *Exponentiation) Eval(X, Y mat.Matrix, evalGradient bool) (*mat.Dense, *t.Dense)

Eval return the kernel k(X, Y) and optionally its gradient

func (*Exponentiation) String

func (k *Exponentiation) String() string

String ...

type Kernel

type Kernel interface {
	Theta() mat.Matrix
	Bounds() mat.Matrix
	CloneWithTheta(theta mat.Matrix) Kernel
	Eval(X, Y mat.Matrix, evalGradient bool) (*mat.Dense, *t.Dense)

	Diag(X mat.Matrix) (K *mat.DiagDense)
	IsStationary() bool
	String() string
	// contains filtered or unexported methods
}

Kernel interface

type KernelOperator

type KernelOperator struct {
	K1, K2 Kernel
}

KernelOperator is a kernel based on two others

func (KernelOperator) Bounds

func (k KernelOperator) Bounds() mat.Matrix

Bounds ...

func (KernelOperator) CloneWithTheta

func (k KernelOperator) CloneWithTheta(theta mat.Matrix) Kernel

CloneWithTheta ...

func (KernelOperator) Diag

func (k KernelOperator) Diag(X mat.Matrix) *mat.DiagDense

Diag ...

func (KernelOperator) Eval

func (k KernelOperator) Eval(X, Y mat.Matrix, evalGradient bool) (*mat.Dense, *t.Dense)

Eval ...

func (KernelOperator) IsStationary

func (k KernelOperator) IsStationary() bool

IsStationary returns whether the kernel is stationary

func (KernelOperator) String

func (k KernelOperator) String() string

String ...

func (KernelOperator) Theta

func (k KernelOperator) Theta() mat.Matrix

Theta ...

type NormalizedKernelMixin

type NormalizedKernelMixin struct{}

NormalizedKernelMixin is Mixin for kernels which are normalized: k(X, X)=1

func (NormalizedKernelMixin) Diag

func (k NormalizedKernelMixin) Diag(X mat.Matrix) (K *mat.DiagDense)

Diag returns the diagonal of the kernel k(X, X)

type Product

type Product struct {
	KernelOperator
}

Product kernel K1 * K2 of two kernels K1 and K2

Example
// np.random.seed(1)
state := randomkit.NewRandomkitSource(1)
// X=np.reshape(np.random.sample(6),(3,2))
X, Y := sample(state, 3, 2), sample(state, 3, 2)
// K=DotProduct(sigma_0=1.23)
K := &Product{KernelOperator{
	K1: &ConstantKernel{ConstantValue: 1.23},
	K2: &DotProduct{Sigma0: 1.23}},
}
fmt.Printf("K=%s, stationary:%v\n", K, K.IsStationary())

KXY, _ := K.Eval(X, Y, false)
KXX := K.Diag(X)
fmt.Printf("X=\n%.8f\nY=\n%.8f\nK(X,Y)=\n%.8f\nK(X,X)=\n%.8f\n", mat.Formatted(X), mat.Formatted(Y), mat.Formatted(KXY), mat.Formatted(KXX))
Output:

K=1.11**2 * DotProduct(sigma_0=1.23), stationary:false
X=
⎡0.41702200  0.72032449⎤
⎢0.00011437  0.30233257⎥
⎣0.14675589  0.09233859⎦
Y=
⎡0.18626021  0.34556073⎤
⎢0.39676747  0.53881673⎥
⎣0.41919451  0.68521950⎦
K(X,Y)=
⎡2.26257327  2.54177490  2.68299128⎤
⎢1.98939655  2.06129209  2.11573791⎥
⎣1.93373635  1.99368430  2.01436051⎦
K(X,X)=
⎡2.71297992  0.00000000  0.00000000⎤
⎢0.00000000  1.97329515  0.00000000⎥
⎣0.00000000  0.00000000  1.89784536⎦

func (Product) CloneWithTheta

func (k Product) CloneWithTheta(theta mat.Matrix) Kernel

CloneWithTheta ...

func (*Product) Diag

func (k *Product) Diag(X mat.Matrix) *mat.DiagDense

Diag returns the diagonal of the kernel k(X, X)

func (*Product) Eval

func (k *Product) Eval(X, Y mat.Matrix, evalGradient bool) (*mat.Dense, *t.Dense)

Eval return the kernel k(X, Y) and optionally its gradient

func (*Product) String

func (k *Product) String() string

String ...

type RBF

type RBF struct {
	LengthScale       []float64
	LengthScaleBounds [][2]float64
	StationaryKernelMixin
	NormalizedKernelMixin
}

RBF kernel is a stationary kernel. It is also known as the "squared exponential" kernel. It is parameterized by a length-scale parameter length_scale>0, which can either be a scalar (isotropic variant of the kernel) or a vector with the same number of dimensions as the inputs X (anisotropic variant of the kernel). The kernel is given by: k(x_i, x_j) = exp(-1 / 2 d(x_i / length_scale, x_j / length_scale)^2) This kernel is infinitely differentiable, which implies that GPs with this kernel as covariance function have mean square derivatives of all orders, and are thus very smooth.

Example
// np.random.seed(1)
state := randomkit.NewRandomkitSource(1)
// X=np.reshape(np.random.sample(6),(3,2))
X, Y := sample(state, 3, 2), sample(state, 3, 2)
// K=DotProduct(sigma_0=1.23)
K := &RBF{LengthScale: []float64{1.23}}
fmt.Printf("K=%s, stationary:%v\n", K, K.IsStationary())

KXY, _ := K.Eval(X, Y, false)
KXX := K.Diag(X)
fmt.Printf("X=\n%.8f\nY=\n%.8f\nK(X,Y)=\n%.8f\nK(X,X)=\n%.8f\n", mat.Formatted(X), mat.Formatted(Y), mat.Formatted(KXY), mat.Formatted(KXX))
Output:

K=RBF([1.23]), stationary:true
X=
⎡0.41702200  0.72032449⎤
⎢0.00011437  0.30233257⎥
⎣0.14675589  0.09233859⎦
Y=
⎡0.18626021  0.34556073⎤
⎢0.39676747  0.53881673⎥
⎣0.41919451  0.68521950⎦
K(X,Y)=
⎡0.93799022  0.98903690  0.99959124⎤
⎢0.98800335  0.93194636  0.89898014⎥
⎣0.97852658  0.91710014  0.86874975⎦
K(X,X)=
⎡1.00000000  0.00000000  0.00000000⎤
⎢0.00000000  1.00000000  0.00000000⎥
⎣0.00000000  0.00000000  1.00000000⎦

func (RBF) Bounds

func (k RBF) Bounds() mat.Matrix

Bounds ...

func (RBF) CloneWithTheta

func (k RBF) CloneWithTheta(theta mat.Matrix) Kernel

CloneWithTheta ...

func (*RBF) Eval

func (k *RBF) Eval(X, Y mat.Matrix, evalGradient bool) (K *mat.Dense, Kg *t.Dense)

Eval return the kernel k(X, Y)

func (*RBF) IsAnisotropic

func (k *RBF) IsAnisotropic() bool

IsAnisotropic ...

func (*RBF) String

func (k *RBF) String() string

String returns string representation of kernel

func (*RBF) Theta

func (k *RBF) Theta() mat.Matrix

Theta ...

type StationaryKernelMixin

type StationaryKernelMixin struct{}

StationaryKernelMixin mixin for kernels which are stationary: k(X, Y)= f(X-Y)

func (StationaryKernelMixin) IsStationary

func (StationaryKernelMixin) IsStationary() bool

IsStationary returns whether the kernel is stationary

type Sum

type Sum struct {
	KernelOperator
}

Sum kernel K1 + K2 of two kernels K1 and K2

Example
// np.random.seed(1)
state := randomkit.NewRandomkitSource(1)
// X=np.reshape(np.random.sample(6),(3,2))
X, Y := sample(state, 3, 2), sample(state, 3, 2)
// K=DotProduct(sigma_0=1.23)
K := &Sum{KernelOperator{K1: &ConstantKernel{ConstantValue: 1.23}, K2: &WhiteKernel{NoiseLevel: 1.23}}}
fmt.Printf("K=%s, stationary:%v\n", K, K.IsStationary())

KXY, _ := K.Eval(X, Y, false)
KXX := K.Diag(X)
fmt.Printf("X=\n%.8f\nY=\n%.8f\nK(X,Y)=\n%.8f\nK(X,X)=\n%.8f\n", mat.Formatted(X), mat.Formatted(Y), mat.Formatted(KXY), mat.Formatted(KXX))
Output:

K=1.11**2 + WhiteKernel(noise_level=1.23), stationary:true
X=
⎡0.41702200  0.72032449⎤
⎢0.00011437  0.30233257⎥
⎣0.14675589  0.09233859⎦
Y=
⎡0.18626021  0.34556073⎤
⎢0.39676747  0.53881673⎥
⎣0.41919451  0.68521950⎦
K(X,Y)=
⎡1.23000000  1.23000000  1.23000000⎤
⎢1.23000000  1.23000000  1.23000000⎥
⎣1.23000000  1.23000000  1.23000000⎦
K(X,X)=
⎡2.46000000  0.00000000  0.00000000⎤
⎢0.00000000  2.46000000  0.00000000⎥
⎣0.00000000  0.00000000  2.46000000⎦

func (Sum) CloneWithTheta

func (k Sum) CloneWithTheta(theta mat.Matrix) Kernel

CloneWithTheta ...

func (*Sum) Diag

func (k *Sum) Diag(X mat.Matrix) *mat.DiagDense

Diag returns the diagonal of the kernel k(X, X)

func (*Sum) Eval

func (k *Sum) Eval(X, Y mat.Matrix, evalGradient bool) (*mat.Dense, *t.Dense)

Eval return the kernel k(X, Y) and optionally its gradient

func (*Sum) String

func (k *Sum) String() string

String ...

type WhiteKernel

type WhiteKernel struct {
	NoiseLevel       float64
	NoiseLevelBounds [2]float64
	StationaryKernelMixin
}

WhiteKernel ... The main use-case of this kernel is as part of a sum-kernel where it explains the noise-component of the signal. Tuning its parameter corresponds to estimating the noise-level. k(x_1, x_2) = noise_level if x_1 == x_2 else 0

Example
// np.random.seed(1)
state := randomkit.NewRandomkitSource(1)
// X=np.reshape(np.random.sample(6),(3,2))
X, Y := sample(state, 3, 2), sample(state, 3, 2)
K := &WhiteKernel{NoiseLevel: 1.23}
fmt.Printf("K=%s, stationary:%v\n", K, K.IsStationary())

KXY, _ := K.Eval(X, Y, false)
KXX := K.Diag(X)
fmt.Printf("X=\n%.8f\nY=\n%.8f\nK(X,Y)=\n%.8f\nK(X,X)=\n%.8f\n", mat.Formatted(X), mat.Formatted(Y), mat.Formatted(KXY), mat.Formatted(KXX))
Output:

K=WhiteKernel(noise_level=1.23), stationary:true
X=
⎡0.41702200  0.72032449⎤
⎢0.00011437  0.30233257⎥
⎣0.14675589  0.09233859⎦
Y=
⎡0.18626021  0.34556073⎤
⎢0.39676747  0.53881673⎥
⎣0.41919451  0.68521950⎦
K(X,Y)=
⎡0.00000000  0.00000000  0.00000000⎤
⎢0.00000000  0.00000000  0.00000000⎥
⎣0.00000000  0.00000000  0.00000000⎦
K(X,X)=
⎡1.23000000  0.00000000  0.00000000⎤
⎢0.00000000  1.23000000  0.00000000⎥
⎣0.00000000  0.00000000  1.23000000⎦

func (WhiteKernel) Bounds

func (k WhiteKernel) Bounds() mat.Matrix

Bounds ...

func (WhiteKernel) CloneWithTheta

func (k WhiteKernel) CloneWithTheta(theta mat.Matrix) Kernel

CloneWithTheta ...

func (*WhiteKernel) Diag

func (k *WhiteKernel) Diag(X mat.Matrix) *mat.DiagDense

Diag returns the diagonal of the kernel k(X, X)

func (*WhiteKernel) Eval

func (k *WhiteKernel) Eval(X, Y mat.Matrix, evalGradient bool) (*mat.Dense, *t.Dense)

Eval return the kernel k(X, Y)

func (*WhiteKernel) String

func (k *WhiteKernel) String() string

String returns string representation of kernel

func (*WhiteKernel) Theta

func (k *WhiteKernel) Theta() mat.Matrix

Theta ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL