RFF
Compute Random Fourier Feature (RFF) kernel approximations.
RFF
RFF(base_kernel, num_basis_fns=50, frequencies=None, compute_engine=BasisFunctionComputation(), key=jr.PRNGKey(0))
Bases: AbstractKernel
Computes an approximation of the kernel using Random Fourier Features.
All stationary kernels are equivalent to the Fourier transform of a probability distribution. We call the corresponding distribution the spectral density. Using a finite number of basis functions, we can compute the spectral density using a Monte-Carlo approximation. This is done by sampling from the spectral density and computing the Fourier transform of the samples. The kernel is then approximated by the inner product of the Fourier transform of the samples with the Fourier transform of the data.
The key reference for this implementation is the following papers: - 'Random Features for Large-Scale Kernel Machines' by Rahimi and Recht (2008). - 'On the Error of Random Fourier Features' by Sutherland and Schneider (2015).
Parameters:
-
base_kernel
(StationaryKernel
) βThe base kernel to be approximated.
-
num_basis_fns
(int
, default:50
) βThe number of basis functions to use in the approximation.
-
frequencies
(Float[Array, 'M D'] | None
, default:None
) βThe frequencies to use in the approximation. If None, the frequencies are sampled from the spectral density of the base kernel.
-
compute_engine
(BasisFunctionComputation
, default:BasisFunctionComputation()
) βThe computation engine to use for the basis function computation.
-
key
(KeyArray
, default:PRNGKey(0)
) βThe random key to use for sampling the frequencies.
cross_covariance
Compute the cross-covariance matrix of the kernel.
Parameters:
-
x
(Num[Array, 'N D']
) βthe first input matrix of shape
(N, D)
. -
y
(Num[Array, 'M D']
) βthe second input matrix of shape
(M, D)
.
Returns:
-
Float[Array, 'N M']
βThe cross-covariance matrix of the kernel of shape
(N, M)
.
gram
Compute the gram matrix of the kernel.
Parameters:
-
x
(Num[Array, 'N D']
) βthe input matrix of shape
(N, D)
.
Returns:
-
LinearOperator
βThe gram matrix of the kernel of shape
(N, N)
.
diagonal
Compute the diagonal of the gram matrix of the kernel.
Parameters:
-
x
(Num[Array, 'N D']
) βthe input matrix of shape
(N, D)
.
Returns:
-
Float[Array, ' N']
βThe diagonal of the gram matrix of the kernel of shape
(N,)
.
slice_input
Slice out the relevant columns of the input matrix.
Select the relevant columns of the supplied matrix to be used within the kernel's evaluation.
Parameters:
-
x
(Float[Array, '... D']
) βthe matrix or vector that is to be sliced.
Returns:
-
Float[Array, '... Q']
βThe sliced form of the input matrix.
__add__
Add two kernels together. Args: other (AbstractKernel): The kernel to be added to the current kernel.
Returns:
-
AbstractKernel
(AbstractKernel
) βA new kernel that is the sum of the two kernels.
__mul__
Multiply two kernels together.
Parameters:
-
other
(AbstractKernel
) βThe kernel to be multiplied with the current kernel.
Returns:
-
AbstractKernel
(AbstractKernel
) βA new kernel that is the product of the two kernels.