|View source on GitHub|
Layer that projects its inputs into a random feature space.
Compat aliases for migration
See Migration guide for more details.
tf.keras.layers.experimental.RandomFourierFeatures( output_dim, kernel_initializer='gaussian', scale=None, trainable=False, name=None, **kwargs )
This layer implements a mapping from input space to a space with
dimensions, which approximates shift-invariant kernels. A kernel function
K(x, y) is shift-invariant if
K(x, y) == k(x - y) for some function
Many popular Radial Basis Functions (RBF), including Gaussian and
Laplacian kernels, are shift-invariant.
The implementation of this layer is based on the following paper: "Random Features for Large-Scale Kernel Machines" by Ali Rahimi and Ben Recht.
The distribution from which the parameters of the random features map (layer) are sampled determines which shift-invariant kernel the layer approximates (see paper for more details). You can use the distribution of your choice. The layer supports out-of-the-box approximation sof the following two RBF kernels:
K(x, y) == exp(- square(x - y) / (2 * square(scale)))
K(x, y) = exp(-abs(x - y) / scale))
Usage: Typically, this layer is used to "kernelize" linear models by applying a non-linear transformation (this layer) to the input features and then training a linear model on top of the transformed features. Depending on the loss function of the linear model, the composition of this layer and the linear model results to models that are equivalent (up to approximation) to kernel SVMs (for hinge loss), kernel logistic regression (for logistic loss), kernel linear regression (for squared loss), etc.
A kernel multinomial logistic regression model with Gaussian kernel for MNIST:
model = keras.Sequential([ keras.Input(shape=(784,)), RandomFourierFeatures( output_dim=4096, scale=10., kernel_initializer='gaussian'), layers.Dense(units=10, activation='softmax'), ]) model.compile( optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'] )
A quasi-SVM classifier for MNIST:
model = keras.Sequential([ keras.Input(shape=(784,)), RandomFourierFeatures( output_dim=4096, scale=10., kernel_initializer='gaussian'), layers.Dense(units=10), ]) model.compile( optimizer='adam', loss='hinge', metrics=['categorical_accuracy'] )
To use another kernel, just replace the layer creation line with:
random_features_layer = RandomFourierFeatures( output_dim=500, kernel_initializer=<my_initializer>, scale=..., ...)
||Positive integer, the dimension of the layer's output, i.e., the number of random features used to approximate the kernel.|
Determines the distribution of the parameters of the
random features map (and therefore the kernel approximated by the layer).
It can be either a string identifier or a Keras
For Gaussian and Laplacian kernels, this corresponds to a scaling
factor of the corresponding kernel approximated by the layer (see concrete
definitions above). When provided, it should be a positive float. If None,
a default value is used: if the kernel initializer is set to "gaussian",
Whether the scaling parameter of the layer should be trainable.
||String, name to use for this layer.|