pyrfm.random_feature.FastFood¶
-
class
pyrfm.random_feature.
FastFood
(n_components=100, gamma=0.5, distribution='gaussian', random_fourier=True, use_offset=False, random_state=None)[source]¶ Bases:
sklearn.base.BaseEstimator
,sklearn.base.TransformerMixin
Approximates feature maps of the product between random matrix and feature vectors by FastFood.
This class can be used not only for approximating RBF kernel but also as a sub-routine for approximating the product between random matrix and feature vectors in some random features.
- Parameters
n_components (int (default=100)) – Number of Monte Carlo samples per original features. Equals the dimensionality of the computed (mapped) feature space. If n_components is not a n-tuple of power of two, it is automatically changed to the smallest n-tuple of the smallest power of two number that is bigger than n_features, which is bigger than n_components. That is, ceil(n_components/2**{p})*2**{p}, where p = ceil(log_2 (n_features)).
gamma (float (default=0.5)) – Bandwidth parameter. gamma = 1/2sigma^2, where sigma is a std parameter for the Gaussian distribution.
distribution (str or function (default="gaussian")) – A function for sampling random bases. Its arguments must be random_state and size. For str, “gaussian” (or “normal”), “rademacher”, “laplace”, or “uniform” can be used.
random_fourier (boolean (default=True)) – Whether to approximate the RBF kernel or not. If True, Fastfood samples random_offset_ in the fit method and computes the cosine of structured_matrix-feature_vector product + random_offset_ in transform. If False, Fastfood does not sample it and computes just structured_matrix-feature_vector product (i.e., approximates dot product kernel).
use_offset (bool (default=False)) – If True, Z(x) = (cos(w_1x+b_1), cos(w_2x+b_2), … , cos(w_Dx+b_D), where w is random_weights and b is offset (D=n_components). If False, Z(x) = (cos(w_1x), …, cos(w_{D/2}x), sin(w_1x), …, sin(w_{D/2}x)).
random_state (int, RandomState instance or None, optional (default=None)) – If int, random_state is the seed used by the random number generator; If np.RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random.
-
random_weights_
¶ The sampled basis, where T = np.ceil(n_components/n_features) and n_feature_padded = 2**np.ceil(np.log2(n_features))
- Type
array, shape (T, n_feature_padded)
-
random_perm_
¶ The sampled permutation matrix.
- Type
array, shape (T, n_features_padded)
-
random_sign_
¶ The sampled siged matrix.
- Type
array, shape (T, n_features)
-
random_scaling_
¶ The sampled scaling matrix.
- Type
array, shape (T, n_features_padded)
-
random_offset_
¶ The sampled random offset for random fourier features. If self.random_fouier is False, random_offset_ is None.
- Type
array, shape (n_components)
References
[1] Fastfood — Approximating Kernel Expansions in Loglinear Time. Quoc Le, Tam´as Sarl´os, and Alex Smola. In ICML 2013. (http://proceedings.mlr.press/v28/le13-supp.pdf)
-
fit
(X, y=None)[source]¶ Generate random weights according to n_features.
- Parameters
X ({array-like, sparse matrix}, shape (n_samples, n_features)) – Training data, where n_samples is the number of samples and n_features is the number of features.
- Returns
self – Returns the transformer.
- Return type
object
-
fit_transform
(X, y=None, **fit_params)¶ Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- Parameters
X (numpy array of shape [n_samples, n_features]) – Training set.
y (numpy array of shape [n_samples]) – Target values.
- Returns
X_new – Transformed array.
- Return type
numpy array of shape [n_samples, n_features_new]
-
get_params
(deep=True)¶ Get parameters for this estimator.
- Parameters
deep (boolean, optional) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
params – Parameter names mapped to their values.
- Return type
mapping of string to any
-
set_params
(**params)¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it’s possible to update each component of a nested object.- Returns
- Return type
self
-
transform
(X)[source]¶ Apply the approximate feature map to X.
- Parameters
X ({array-like, sparse matrix}, shape (n_samples, n_features)) – New data, where n_samples is the number of samples and n_features is the number of features.
- Returns
X_new
- Return type
array-like, shape (n_samples, n_components)