Skip to content

Commit f0521dc

Browse files
committed
Add nightly wheels and try to pass check_estimator
1 parent 3d17530 commit f0521dc

File tree

4 files changed

+56
-41
lines changed

4 files changed

+56
-41
lines changed

.travis.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ matrix:
1515
SKLEARN_VERSION="0.20.2"
1616
- env: PYTHON_VERSION="3.7" NUMPY_VERSION="*" SCIPY_VERSION="*"
1717
SKLEARN_VERSION="*"
18+
- env: PYTHON_VERSION="3.7" NUMPY_VERSION="*" SCIPY_VERSION="*"
19+
SKLEARN_VERSION="nightly"
1820

1921
install:
2022
# install miniconda
@@ -27,7 +29,14 @@ install:
2729
# create the testing environment
2830
- conda create -n testenv --yes python=$PYTHON_VERSION pip
2931
- source activate testenv
30-
- conda install --yes numpy==$NUMPY_VERSION scipy==$SCIPY_VERSION scikit-learn==$SKLEARN_VERSION cython nose pytest pytest-cov
32+
- |
33+
if [ $SKLEARN_VERSION = "nightly" ]; then
34+
conda install --yes numpy==$NUMPY_VERSION scipy==$SCIPY_VERSION cython nose pytest pytest-cov
35+
# install nightly wheels
36+
pip install --pre -f https://sklearn-nightly.scdn8.secure.raxcdn.com scikit-learn
37+
else
38+
conda install --yes numpy==$NUMPY_VERSION scipy==$SCIPY_VERSION scikit-learn==$SKLEARN_VERSION cython nose pytest pytest-cov
39+
fi
3140
- pip install codecov
3241
- pip install .
3342

examples/rks_vs_fastfood.py renamed to benchmarks/bench_rbfsampler_fastfood.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# rbf_kernel(X, Y, gamma=gamma)
2626
exact_end = datetime.datetime.utcnow()
2727
exact_spent_time = exact_end - exact_start
28-
print "Timimg exact rbf: \t\t", exact_spent_time
28+
print("Timimg exact rbf: \t\t", exact_spent_time)
2929

3030
rbf_transform = Fastfood(sigma=sigma,
3131
n_components=number_of_features_to_generate,
@@ -39,7 +39,7 @@
3939
fastfood_fast_vec_end = datetime.datetime.utcnow()
4040
fastfood_fast_vec_spent_time = fastfood_fast_vec_end - \
4141
fastfood_fast_vec_start
42-
print "Timimg fastfood fast vectorized: \t\t", fastfood_fast_vec_spent_time
42+
print("Timimg fastfood fast vectorized: \t\t", fastfood_fast_vec_spent_time)
4343

4444
rks_rbf_transform = RBFSampler(gamma=gamma,
4545
n_components=number_of_features_to_generate,
@@ -51,6 +51,6 @@
5151
_ = rks_rbf_transform.transform(Y)
5252
rks_end = datetime.datetime.utcnow()
5353
rks_spent_time = rks_end - rks_start
54-
print "Timimg rks: \t\t\t", rks_spent_time
54+
print("Timimg rks: \t\t\t", rks_spent_time)
5555

5656
assert_greater(rks_spent_time, fastfood_fast_vec_spent_time)

sklearn_extra/kernel_approximation/_fastfood.py

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Fastfood(BaseEstimator, TransformerMixin):
4444
Notes
4545
-----
4646
See "Fastfood | Approximating Kernel Expansions in Loglinear Time" by
47-
Quoc Le, Tamas Sarl and Alex Smola.
47+
Quoc Le, Tamas Sarl and Alex Smola.
4848
4949
Examples
5050
----
@@ -62,11 +62,9 @@ def __init__(self,
6262
self.sigma = sigma
6363
self.n_components = n_components
6464
self.random_state = random_state
65-
self.rng = check_random_state(self.random_state)
6665
# map to 2*n_components features or to n_components features with less
6766
# accuracy
68-
self.tradeoff_mem_accuracy = \
69-
tradeoff_mem_accuracy
67+
self.tradeoff_mem_accuracy = tradeoff_mem_accuracy
7068

7169
@staticmethod
7270
def is_number_power_of_two(n):
@@ -87,13 +85,14 @@ def enforce_dimensionality_constraints(d, n):
8785

8886
def pad_with_zeros(self, X):
8987
try:
90-
X_padded = np.pad(X,
91-
((0, 0),
92-
(0, self.number_of_features_to_pad_with_zeros)),
93-
'constant')
88+
X_padded = np.pad(
89+
X,
90+
((0, 0),
91+
(0, self._number_of_features_to_pad_with_zeros)),
92+
'constant')
9493
except AttributeError:
9594
zeros = np.zeros((X.shape[0],
96-
self.number_of_features_to_pad_with_zeros))
95+
self._number_of_features_to_pad_with_zeros))
9796
X_padded = np.concatenate((X, zeros), axis=1)
9897

9998
return X_padded
@@ -108,38 +107,38 @@ def l2norm_along_axis1(X):
108107

109108
def uniform_vector(self):
110109
if self.tradeoff_mem_accuracy != 'accuracy':
111-
return self.rng.uniform(0, 2 * np.pi, size=self.n)
110+
return self._rng.uniform(0, 2 * np.pi, size=self._n)
112111
else:
113112
return None
114113

115114
def apply_approximate_gaussian_matrix(self, B, G, P, X):
116115
""" Create mapping of all x_i by applying B, G and P step-wise """
117116
num_examples = X.shape[0]
118117

119-
result = np.multiply(B, X.reshape((1, num_examples, 1, self.d)))
120-
result = result.reshape((num_examples*self.times_to_stack_v, self.d))
118+
result = np.multiply(B, X.reshape((1, num_examples, 1, self._d)))
119+
result = result.reshape((num_examples*self._times_to_stack_v, self._d))
121120
Fastfood.approx_fourier_transformation_multi_dim(result)
122121
result = result.reshape((num_examples, -1))
123122
np.take(result, P, axis=1, mode='wrap', out=result)
124-
np.multiply(np.ravel(G), result.reshape(num_examples, self.n),
123+
np.multiply(np.ravel(G), result.reshape(num_examples, self._n),
125124
out=result)
126-
result = result.reshape(num_examples*self.times_to_stack_v, self.d)
125+
result = result.reshape(num_examples*self._times_to_stack_v, self._d)
127126
Fastfood.approx_fourier_transformation_multi_dim(result)
128127
return result
129128

130129
def scale_transformed_data(self, S, VX):
131130
""" Scale mapped data VX to match kernel(e.g. RBF-Kernel) """
132-
VX = VX.reshape(-1, self.times_to_stack_v*self.d)
131+
VX = VX.reshape(-1, self._times_to_stack_v*self._d)
133132

134-
return (1 / (self.sigma * np.sqrt(self.d)) *
133+
return (1 / (self.sigma * np.sqrt(self._d)) *
135134
np.multiply(np.ravel(S), VX))
136135

137136
def phi(self, X):
138137
if self.tradeoff_mem_accuracy == 'accuracy':
139138
return (1 / np.sqrt(X.shape[1])) * \
140139
np.hstack([np.cos(X), np.sin(X)])
141140
else:
142-
np.cos(X+self.U, X)
141+
np.cos(X+self._U, X)
143142
return X * np.sqrt(2. / X.shape[1])
144143

145144
def fit(self, X, y=None):
@@ -159,28 +158,29 @@ def fit(self, X, y=None):
159158
self : object
160159
Returns the transformer.
161160
"""
162-
X = check_array(X)
161+
X = check_array(X, dtype=np.float64)
163162

164163
d_orig = X.shape[1]
164+
self._rng = check_random_state(self.random_state)
165165

166-
self.d, self.n, self.times_to_stack_v = \
166+
self._d, self._n, self._times_to_stack_v = \
167167
Fastfood.enforce_dimensionality_constraints(d_orig,
168168
self.n_components)
169-
self.number_of_features_to_pad_with_zeros = self.d - d_orig
169+
self._number_of_features_to_pad_with_zeros = self._d - d_orig
170170

171-
self.G = self.rng.normal(size=(self.times_to_stack_v, self.d))
172-
self.B = self.rng.choice(
171+
self._G = self._rng.normal(size=(self._times_to_stack_v, self._d))
172+
self._B = self._rng.choice(
173173
[-1, 1],
174-
size=(self.times_to_stack_v, self.d),
174+
size=(self._times_to_stack_v, self._d),
175175
replace=True)
176-
self.P = np.hstack([(i*self.d)+self.rng.permutation(self.d)
177-
for i in range(self.times_to_stack_v)])
178-
self.S = np.multiply(1 / self.l2norm_along_axis1(self.G)
179-
.reshape((-1, 1)),
180-
chi.rvs(self.d,
181-
size=(self.times_to_stack_v, self.d)))
176+
self._P = np.hstack([(i*self._d)+self._rng.permutation(self._d)
177+
for i in range(self._times_to_stack_v)])
178+
self._S = np.multiply(1 / self.l2norm_along_axis1(self._G)
179+
.reshape((-1, 1)),
180+
chi.rvs(self._d,
181+
size=(self._times_to_stack_v, self._d)))
182182

183-
self.U = self.uniform_vector()
183+
self._U = self.uniform_vector()
184184

185185
return self
186186

@@ -197,11 +197,14 @@ def transform(self, X):
197197
-------
198198
X_new : array-like, shape (n_samples, n_components)
199199
"""
200-
X = check_array(X)
200+
X = check_array(X, dtype=np.float64)
201201
X_padded = self.pad_with_zeros(X)
202-
HGPHBX = self.apply_approximate_gaussian_matrix(self.B,
203-
self.G,
204-
self.P,
202+
HGPHBX = self.apply_approximate_gaussian_matrix(self._B,
203+
self._G,
204+
self._P,
205205
X_padded)
206-
VX = self.scale_transformed_data(self.S, HGPHBX)
206+
VX = self.scale_transformed_data(self._S, HGPHBX)
207207
return self.phi(VX)
208+
209+
def _more_tags(self):
210+
return {'non_deterministic': True}

sklearn_extra/tests/test_common.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
from sklearn_extra import TemplateEstimator
66
from sklearn_extra import TemplateClassifier
77
from sklearn_extra import TemplateTransformer
8+
from sklearn_extra.kernel_approximation import Fastfood
89

910

1011
@pytest.mark.parametrize(
11-
"Estimator", [TemplateEstimator, TemplateTransformer, TemplateClassifier]
12+
"Estimator",
13+
[TemplateEstimator, TemplateTransformer, TemplateClassifier,
14+
Fastfood]
1215
)
13-
def test_all_estimators(Estimator):
16+
def test_all_estimators(Estimator, request):
1417
return check_estimator(Estimator)

0 commit comments

Comments
 (0)