forked from hichamjanati/srf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFF.py
43 lines (37 loc) · 1.59 KB
/
RFF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from sklearn.base import BaseEstimator
from sklearn.exceptions import NotFittedError
from scipy.stats import cauchy, laplace
import numpy as np
class RFF(BaseEstimator):
def __init__(self, gamma = 1, D = 50, metric = "rbf"):
self.gamma = gamma
self.metric = metric
#Dimensionality D (number of MonteCarlo samples)
self.D = D
self.fitted = False
def fit(self, X, y=None):
""" Generates MonteCarlo random samples """
d = X.shape[1]
#Generate D iid samples from p(w)
if self.metric == "rbf":
self.w = np.sqrt(2*self.gamma)*np.random.normal(size=(self.D,d))
elif self.metric == "laplace":
self.w = cauchy.rvs(scale = self.gamma, size=(self.D,d))
#Generate D iid samples from Uniform(0,2*pi)
self.u = 2*np.pi*np.random.rand(self.D)
self.fitted = True
return self
def transform(self,X):
""" Transforms the data X (n_samples, n_features) to the new map space Z(X) (n_samples, n_components)"""
if not self.fitted:
raise NotFittedError("RBF_MonteCarlo must be fitted beform computing the feature map Z")
#Compute feature map Z(x):
Z = np.sqrt(2/self.D)*np.cos((X.dot(self.w.T) + self.u[np.newaxis,:]))
return Z
def compute_kernel(self, X):
""" Computes the approximated kernel matrix K """
if not self.fitted:
raise NotFittedError("RBF_MonteCarlo must be fitted beform computing the kernel matrix")
Z = self.transform(X)
K = Z.dot(Z.T)
return K