Skip to content

Commit 89f68ee

Browse files
author
William de Vazelhes
committed
Add verbose during iterations
1 parent 34c5457 commit 89f68ee

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

sklearn/neighbors/nca.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,19 @@ def _loss_grad_lbfgs(self, transformation, X, y, diffs,
430430
The new (flattened) gradient of the loss.
431431
"""
432432

433+
if self.n_iter_ == 0:
434+
self.n_iter_ += 1
435+
if self.verbose:
436+
header_fields = ['Iteration', 'Objective Value', 'Time(s)']
437+
header_fmt = '{:>10} {:>20} {:>10}'
438+
header = header_fmt.format(*header_fields)
439+
cls_name = self.__class__.__name__
440+
print('[{}]'.format(cls_name))
441+
print('[{}] {}\n[{}] {}'.format(cls_name, header,
442+
cls_name, '-' * len(header)))
443+
444+
t_funcall = time.time()
445+
433446
transformation = transformation.reshape(-1, X.shape[1])
434447
loss = 0
435448
gradient = np.zeros(transformation.shape)
@@ -454,6 +467,14 @@ def _loss_grad_lbfgs(self, transformation, X, y, diffs,
454467
p_i = np.sum(p_i_j)
455468
gradient += 2 * (p_i * (sum_ci.T + sum_not_ci.T) - sum_ci.T)
456469
loss += p_i
470+
471+
if self.verbose:
472+
t_funcall = time.time() - t_funcall
473+
values_fmt = '[{}] {:>10} {:>20.6e} {:>10.2f}'
474+
print(values_fmt.format(self.__class__.__name__, self.n_iter_,
475+
loss, t_funcall))
476+
sys.stdout.flush()
477+
457478
return - loss, - gradient.ravel()
458479

459480

sklearn/neighbors/tests/test_nca.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def test_finite_differences():
6060
X, y, init = nca._validate_params(X, y)
6161
masks = _make_masks(y)
6262
diffs = X[:, np.newaxis] - X[np.newaxis]
63+
nca.n_iter_ = 0
6364

6465
point = nca._initialize(X, init)
6566
# compute the gradient at `point`

0 commit comments

Comments
 (0)