Skip to content

Commit 999cb5b

Browse files
wdevazelhesperimosocordiae
authored andcommitted
[MRG] Add ChangedBehaviorWarning message for LMNN too (#214)
* Add ChangedBehaviorWarning message for LMNN too * Remove useless 'as an init'
1 parent 130cbad commit 999cb5b

File tree

7 files changed

+54
-20
lines changed

7 files changed

+54
-20
lines changed

metric_learn/lmnn.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import warnings
1818
from collections import Counter
1919
from six.moves import xrange
20+
from sklearn.exceptions import ChangedBehaviorWarning
2021
from sklearn.metrics import euclidean_distances
2122
from sklearn.base import TransformerMixin
2223

@@ -26,18 +27,20 @@
2627

2728
# commonality between LMNN implementations
2829
class _base_LMNN(MahalanobisMixin, TransformerMixin):
29-
def __init__(self, init='auto', k=3, min_iter=50, max_iter=1000,
30+
def __init__(self, init=None, k=3, min_iter=50, max_iter=1000,
3031
learn_rate=1e-7, regularization=0.5, convergence_tol=0.001,
3132
use_pca=True, verbose=False, preprocessor=None,
3233
n_components=None, num_dims='deprecated', random_state=None):
3334
"""Initialize the LMNN object.
3435
3536
Parameters
3637
----------
37-
init : string or numpy array, optional (default='auto')
38+
init : None, string or numpy array, optional (default=None)
3839
Initialization of the linear transformation. Possible options are
39-
'auto', 'pca', 'lda', 'identity', 'random', and a numpy array of shape
40-
(n_features_a, n_features_b).
40+
'auto', 'pca', 'identity', 'random', and a numpy array of shape
41+
(n_features_a, n_features_b). If None, will be set automatically to
42+
'auto' (this option is to raise a warning if 'init' is not set,
43+
and stays to its default value None, in v0.5.0).
4144
4245
'auto'
4346
Depending on ``n_components``, the most reasonable initialization
@@ -135,7 +138,21 @@ def fit(self, X, y):
135138
if len(label_inds) != num_pts:
136139
raise ValueError('Must have one label per point.')
137140
self.labels_ = np.arange(len(unique_labels))
138-
self.transformer_ = _initialize_transformer(output_dim, X, y, self.init,
141+
142+
# if the init is the default (None), we raise a warning
143+
if self.init is None:
144+
# TODO: replace init=None by init='auto' in v0.6.0 and remove the warning
145+
msg = ("Warning, no init was set (`init=None`). As of version 0.5.0, "
146+
"the default init will now be set to 'auto', instead of the "
147+
"previous identity matrix. If you still want to use the identity "
148+
"matrix as before, set init='identity'. This warning "
149+
"will disappear in v0.6.0, and `init` parameter's default value "
150+
"will be set to 'auto'.")
151+
warnings.warn(msg, ChangedBehaviorWarning)
152+
init = 'auto'
153+
else:
154+
init = self.init
155+
self.transformer_ = _initialize_transformer(output_dim, X, y, init,
139156
self.verbose,
140157
self.random_state)
141158
required_k = np.bincount(label_inds).min()

metric_learn/lsml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def _fit(self, quadruplets, weights=None):
9494
else:
9595
self.w_ = weights
9696
self.w_ /= self.w_.sum() # weights must sum to 1
97-
# if the prior is the default (identity), we raise a warning just in case
97+
# if the prior is the default (None), we raise a warning
9898
if self.prior is None:
9999
msg = ("Warning, no prior was set (`prior=None`). As of version 0.5.0, "
100100
"the default prior will now be set to "

metric_learn/mlkr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def fit(self, X, y):
156156
m = self.n_components
157157
if m is None:
158158
m = d
159-
# if the init is the default (identity), we raise a warning just in case
159+
# if the init is the default (None), we raise a warning
160160
if self.init is None:
161161
# TODO:
162162
# replace init=None by init='auto' in v0.6.0 and remove the warning

metric_learn/nca.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,16 @@ def fit(self, X, y):
141141
train_time = time.time()
142142

143143
# Initialize A
144-
# if the init is the default (auto), we raise a warning just in case
144+
# if the init is the default (None), we raise a warning
145145
if self.init is None:
146146
# TODO: replace init=None by init='auto' in v0.6.0 and remove the warning
147147
msg = ("Warning, no init was set (`init=None`). As of version 0.5.0, "
148148
"the default init will now be set to 'auto', instead of the "
149-
"previous scaling matrix. same scaling matrix as before as an "
150-
"init, set init=np.eye(X.shape[1])/"
151-
"(np.maximum(X.max(axis=0)-X.min(axis=0), EPS))). This warning "
152-
"will disappear in v0.6.0, and `init` parameter's default value "
153-
"will be set to 'auto'.")
149+
"previous scaling matrix. If you still want to use the same "
150+
"scaling matrix as before, set "
151+
"init=np.eye(X.shape[1])/(np.maximum(X.max(axis=0)-X.min(axis=0)"
152+
", EPS))). This warning will disappear in v0.6.0, and `init` "
153+
"parameter's default value will be set to 'auto'.")
154154
warnings.warn(msg, ChangedBehaviorWarning)
155155
init = 'auto'
156156
else:

metric_learn/sdml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _fit(self, pairs, y):
111111
type_of_inputs='tuples')
112112

113113
# set up (the inverse of) the prior M
114-
# if the prior is the default (identity), we raise a warning just in case
114+
# if the prior is the default (None), we raise a warning
115115
if self.prior is None:
116116
# TODO:
117117
# replace prior=None by prior='identity' in v0.6.0 and remove the

test/metric_learn_test.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,23 @@ def grad(x):
275275
np.linalg.norm(approx_fprime(L.ravel(), fun, epsilon)))
276276
np.testing.assert_almost_equal(rel_diff, 0., decimal=5)
277277

278+
def test_changed_behaviour_warning(self):
279+
# test that a ChangedBehavior warning is thrown about the init, if the
280+
# default parameters are used.
281+
# TODO: remove in v.0.6
282+
X = np.array([[0, 0], [0, 1], [2, 0], [2, 1]])
283+
y = np.array([1, 0, 1, 0])
284+
lmnn = LMNN(k=2)
285+
msg = ("Warning, no init was set (`init=None`). As of version 0.5.0, "
286+
"the default init will now be set to 'auto', instead of the "
287+
"previous identity matrix. If you still want to use the identity "
288+
"matrix as before, set init='identity'. This warning "
289+
"will disappear in v0.6.0, and `init` parameter's default value "
290+
"will be set to 'auto'.")
291+
with pytest.warns(ChangedBehaviorWarning) as raised_warning:
292+
lmnn.fit(X, y)
293+
assert any(msg == str(wrn.message) for wrn in raised_warning)
294+
278295

279296
@pytest.mark.parametrize('X, y, loss', [(np.array([[0], [1], [2], [3]]),
280297
[1, 1, 0, 0], 3.0),
@@ -744,11 +761,11 @@ def test_changed_behaviour_warning(self):
744761
nca = NCA()
745762
msg = ("Warning, no init was set (`init=None`). As of version 0.5.0, "
746763
"the default init will now be set to 'auto', instead of the "
747-
"previous scaling matrix. same scaling matrix as before as an "
748-
"init, set init=np.eye(X.shape[1])/"
749-
"(np.maximum(X.max(axis=0)-X.min(axis=0), EPS))). This warning will"
750-
" disappear in v0.6.0, and `init` parameter's default value will "
751-
"be set to 'auto'.")
764+
"previous scaling matrix. If you still want to use the same "
765+
"scaling matrix as before, set "
766+
"init=np.eye(X.shape[1])/(np.maximum(X.max(axis=0)-X.min(axis=0)"
767+
", EPS))). This warning will disappear in v0.6.0, and `init` "
768+
"parameter's default value will be set to 'auto'.")
752769
with pytest.warns(ChangedBehaviorWarning) as raised_warning:
753770
nca.fit(X, y)
754771
assert any(msg == str(wrn.message) for wrn in raised_warning)

test/test_base_metric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_covariance(self):
2121
def test_lmnn(self):
2222
self.assertRegexpMatches(
2323
str(metric_learn.LMNN()),
24-
r"(python_)?LMNN\(convergence_tol=0.001, init='auto', k=3, "
24+
r"(python_)?LMNN\(convergence_tol=0.001, init=None, k=3, "
2525
r"learn_rate=1e-07,\s+"
2626
r"max_iter=1000, min_iter=50, n_components=None, "
2727
r"num_dims='deprecated',\s+preprocessor=None, random_state=None, "

0 commit comments

Comments
 (0)