Skip to content

[MRG] Fix random state in algorithms #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion metric_learn/lmnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def fit(self, X, y):
init = self.init
self.components_ = _initialize_components(output_dim, X, y, init,
self.verbose,
self.random_state)
random_state=self.random_state)
required_k = np.bincount(label_inds).min()
if self.k > required_k:
raise ValueError('not enough class labels for specified k'
Expand Down
5 changes: 2 additions & 3 deletions metric_learn/lsml.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ def _fit(self, quadruplets, weights=None):
else:
prior = self.prior
M, prior_inv = _initialize_metric_mahalanobis(quadruplets, prior,
return_inverse=True,
strict_pd=True,
matrix_name='prior')
return_inverse=True, strict_pd=True, matrix_name='prior',
random_state=self.random_state)

step_sizes = np.logspace(-10, 0, 10)
# Keep track of the best step size and the loss at that step.
Expand Down
3 changes: 2 additions & 1 deletion metric_learn/nca.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ def fit(self, X, y):
init = 'auto'
else:
init = self.init
A = _initialize_components(n_components, X, labels, init, self.verbose)
A = _initialize_components(n_components, X, labels, init, self.verbose,
self.random_state)

# Run NCA
mask = labels[:, np.newaxis] == labels[np.newaxis, :]
Expand Down
5 changes: 2 additions & 3 deletions metric_learn/sdml.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ def _fit(self, pairs, y):
else:
prior = self.prior
_, prior_inv = _initialize_metric_mahalanobis(pairs, prior,
return_inverse=True,
strict_pd=True,
matrix_name='prior')
return_inverse=True, strict_pd=True, matrix_name='prior',
random_state=self.random_state)
diff = pairs[:, 0] - pairs[:, 1]
loss_matrix = (diff.T * y).dot(diff)
emp_cov = prior_inv + self.balance_param * loss_matrix
Expand Down
22 changes: 22 additions & 0 deletions test/test_mahalanobis_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,25 @@ def test_singular_array_init_or_prior(estimator, build_dataset, w0):
with pytest.raises(LinAlgError) as raised_err:
model.fit(input_data, labels)
assert str(raised_err.value) == msg


@pytest.mark.integration
@pytest.mark.parametrize('estimator, build_dataset', metric_learners,
ids=ids_metric_learners)
def test_deterministic_initialization(estimator, build_dataset):
"""Test that estimators that have a prior or an init are deterministic
when it is set to to random and when the random_state is fixed."""
input_data, labels, _, X = build_dataset()
model = clone(estimator)
if hasattr(estimator, 'init'):
model.set_params(init='random')
if hasattr(estimator, 'prior'):
model.set_params(prior='random')
model1 = clone(model)
set_random_state(model1, 42)
model1 = model1.fit(input_data, labels)
model2 = clone(model)
set_random_state(model2, 42)
model2 = model2.fit(input_data, labels)
np.testing.assert_allclose(model1.get_mahalanobis_matrix(),
model2.get_mahalanobis_matrix())