@@ -67,12 +67,12 @@ def __init__(self,
67
67
self .tradeoff_mem_accuracy = tradeoff_mem_accuracy
68
68
69
69
@staticmethod
70
- def is_number_power_of_two (n ):
70
+ def _is_number_power_of_two (n ):
71
71
return n != 0 and ((n & (n - 1 )) == 0 )
72
72
73
73
@staticmethod
74
- def enforce_dimensionality_constraints (d , n ):
75
- if not (Fastfood .is_number_power_of_two (d )):
74
+ def _enforce_dimensionality_constraints (d , n ):
75
+ if not (Fastfood ._is_number_power_of_two (d )):
76
76
# find d that fulfills 2^l
77
77
d = np .power (2 , np .floor (np .log2 (d )) + 1 )
78
78
divisor , remainder = divmod (n , d )
@@ -83,7 +83,7 @@ def enforce_dimensionality_constraints(d, n):
83
83
times_to_stack_v = int (divisor + 1 )
84
84
return int (d ), int (n ), times_to_stack_v
85
85
86
- def pad_with_zeros (self , X ):
86
+ def _pad_with_zeros (self , X ):
87
87
try :
88
88
X_padded = np .pad (
89
89
X ,
@@ -98,42 +98,42 @@ def pad_with_zeros(self, X):
98
98
return X_padded
99
99
100
100
@staticmethod
101
- def approx_fourier_transformation_multi_dim (result ):
101
+ def _approx_fourier_transformation_multi_dim (result ):
102
102
cyfht (result )
103
103
104
104
@staticmethod
105
- def l2norm_along_axis1 (X ):
105
+ def _l2norm_along_axis1 (X ):
106
106
return np .sqrt (np .einsum ('ij,ij->i' , X , X ))
107
107
108
- def uniform_vector (self ):
108
+ def _uniform_vector (self , rng ):
109
109
if self .tradeoff_mem_accuracy != 'accuracy' :
110
- return self . _rng .uniform (0 , 2 * np .pi , size = self ._n )
110
+ return rng .uniform (0 , 2 * np .pi , size = self ._n )
111
111
else :
112
112
return None
113
113
114
- def apply_approximate_gaussian_matrix (self , B , G , P , X ):
114
+ def _apply_approximate_gaussian_matrix (self , B , G , P , X ):
115
115
""" Create mapping of all x_i by applying B, G and P step-wise """
116
116
num_examples = X .shape [0 ]
117
117
118
118
result = np .multiply (B , X .reshape ((1 , num_examples , 1 , self ._d )))
119
119
result = result .reshape ((num_examples * self ._times_to_stack_v , self ._d ))
120
- Fastfood .approx_fourier_transformation_multi_dim (result )
120
+ Fastfood ._approx_fourier_transformation_multi_dim (result )
121
121
result = result .reshape ((num_examples , - 1 ))
122
122
np .take (result , P , axis = 1 , mode = 'wrap' , out = result )
123
123
np .multiply (np .ravel (G ), result .reshape (num_examples , self ._n ),
124
124
out = result )
125
125
result = result .reshape (num_examples * self ._times_to_stack_v , self ._d )
126
- Fastfood .approx_fourier_transformation_multi_dim (result )
126
+ Fastfood ._approx_fourier_transformation_multi_dim (result )
127
127
return result
128
128
129
- def scale_transformed_data (self , S , VX ):
129
+ def _scale_transformed_data (self , S , VX ):
130
130
""" Scale mapped data VX to match kernel(e.g. RBF-Kernel) """
131
131
VX = VX .reshape (- 1 , self ._times_to_stack_v * self ._d )
132
132
133
133
return (1 / (self .sigma * np .sqrt (self ._d )) *
134
134
np .multiply (np .ravel (S ), VX ))
135
135
136
- def phi (self , X ):
136
+ def _phi (self , X ):
137
137
if self .tradeoff_mem_accuracy == 'accuracy' :
138
138
return (1 / np .sqrt (X .shape [1 ])) * \
139
139
np .hstack ([np .cos (X ), np .sin (X )])
@@ -161,27 +161,27 @@ def fit(self, X, y=None):
161
161
X = check_array (X , dtype = np .float64 )
162
162
163
163
d_orig = X .shape [1 ]
164
- self . _rng = check_random_state (self .random_state )
164
+ rng = check_random_state (self .random_state )
165
165
166
166
self ._d , self ._n , self ._times_to_stack_v = \
167
- Fastfood .enforce_dimensionality_constraints (d_orig ,
168
- self .n_components )
167
+ Fastfood ._enforce_dimensionality_constraints (d_orig ,
168
+ self .n_components )
169
169
self ._number_of_features_to_pad_with_zeros = self ._d - d_orig
170
170
171
- self ._G = self . _rng .normal (size = (self ._times_to_stack_v , self ._d ))
172
- self ._B = self . _rng .choice (
171
+ self ._G = rng .normal (size = (self ._times_to_stack_v , self ._d ))
172
+ self ._B = rng .choice (
173
173
[- 1 , 1 ],
174
174
size = (self ._times_to_stack_v , self ._d ),
175
175
replace = True )
176
- self ._P = np .hstack ([(i * self ._d )+ self . _rng .permutation (self ._d )
176
+ self ._P = np .hstack ([(i * self ._d ) + rng .permutation (self ._d )
177
177
for i in range (self ._times_to_stack_v )])
178
- self ._S = np .multiply (1 / self .l2norm_along_axis1 (self ._G )
178
+ self ._S = np .multiply (1 / self ._l2norm_along_axis1 (self ._G )
179
179
.reshape ((- 1 , 1 )),
180
180
chi .rvs (self ._d ,
181
181
size = (self ._times_to_stack_v , self ._d ),
182
182
random_state = self .random_state ))
183
183
184
- self ._U = self .uniform_vector ( )
184
+ self ._U = self ._uniform_vector ( rng )
185
185
186
186
return self
187
187
@@ -199,10 +199,8 @@ def transform(self, X):
199
199
X_new : array-like, shape (n_samples, n_components)
200
200
"""
201
201
X = check_array (X , dtype = np .float64 )
202
- X_padded = self .pad_with_zeros (X )
203
- HGPHBX = self .apply_approximate_gaussian_matrix (self ._B ,
204
- self ._G ,
205
- self ._P ,
206
- X_padded )
207
- VX = self .scale_transformed_data (self ._S , HGPHBX )
208
- return self .phi (VX )
202
+ X_padded = self ._pad_with_zeros (X )
203
+ HGPHBX = self ._apply_approximate_gaussian_matrix (
204
+ self ._B , self ._G , self ._P , X_padded )
205
+ VX = self ._scale_transformed_data (self ._S , HGPHBX )
206
+ return self ._phi (VX )
0 commit comments