Skip to content

Commit d2cc7ce

Browse files
author
William de Vazelhes
committed
Fix test_singular_covariance_init
1 parent d622fae commit d2cc7ce

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

metric_learn/lsml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def _total_loss(self, metric, vab, vcd, prior_inv):
154154
return self._comparison_loss(metric, vab, vcd) + reg_loss
155155

156156
def _gradient(self, metric, vab, vcd, prior_inv):
157-
dMetric = prior_inv - np.linalg.inv(metric)
157+
dMetric = prior_inv - scipy.linalg.pinvh(metric)
158158
dabs = np.sum(vab.dot(metric) * vab, axis=1)
159159
dcds = np.sum(vcd.dot(metric) * vcd, axis=1)
160160
violations = dabs > dcds

metric_learn/sdml.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ def _fit(self, pairs, y):
112112
return_inverse=True)
113113
diff = pairs[:, 0] - pairs[:, 1]
114114
loss_matrix = (diff.T * y).dot(diff)
115-
emp_cov = prior_inv + self.balance_param * loss_matrix
115+
emp_cov = (prior_inv + self.balance_param * loss_matrix +
116+
# We add a small value on the diagonal in case the
117+
# emp_cov matrix is singular (see
118+
# #https://github.com/metric-learn/metric-learn/issues/202)
119+
np.eye(diff.shape[1]) * 1e-10)
116120

117121
# our initialization will be the matrix with emp_cov's eigenvalues,
118122
# with a constant added so that they are all positive (plus an epsilon

0 commit comments

Comments
 (0)