In minimize.least_squares, compute values at the solution at full verbosity.

PiperOrigin-RevId: 745301888
Change-Id: Ibd8e35e405bb2cebc7ac59a6dfc77118a6e6ba9f
This commit is contained in:
Yuval Tassa
2025-04-08 14:54:51 -07:00
committed by Copybara-Service
parent 699ad13a72
commit 5031f88158
2 changed files with 19 additions and 2 deletions
+17
View File
@@ -403,6 +403,23 @@ def least_squares(
yfinal = norm.value(r)
red = np.float64(0.0) # No reduction since we didn't take a step.
log = IterLog(candidate=x, objective=yfinal, reduction=red, regularizer=mu)
# If full verbosity requested, compute values at the final point.
if verbose >= Verbosity.FULLITER.value:
# Get Jacobian jac.
t_start = time.time()
if jacobian is None:
jac, n_res = jacobian_fd(residual, x, r, eps, n_res, bounds)
t_res += time.time() - t_start
else:
jac = jacobian(x, r)
t_jac += time.time() - t_start
n_jac += 1
# Get gradient, add to log.
grad, _ = norm.grad_hess(r, jac)
log = dataclasses.replace(log, residual=r, jacobian=jac, grad=grad)
trace.append(log)
if iter_callback is not None:
iter_callback(trace)
+2 -2
View File
@@ -127,8 +127,8 @@ class MinimizeTest(absltest.TestCase):
output=out,
verbose=minimize.Verbosity.FULLITER,
)
self.assertIn(' < tol', out.getvalue())
grad = trace[-2].jacobian.T @ trace[-2].residual
self.assertIn('norm(gradient) < tol', out.getvalue())
grad = trace[-1].grad
# If x_i is on the boundary, gradient points out, otherwise it is 0.
for i, xi in enumerate(x):
if xi == bounds[0][i]: