Skip to content

Commit

Permalink
minor refactor: nest "cauchy" and "grad" options for handle_npd
Browse files Browse the repository at this point in the history
  • Loading branch information
rfeinman committed Mar 10, 2022
1 parent 83602fa commit c6b54ae
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions torchmin/newton.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,17 @@ def _minimize_newton_exact(
nfail += 1
if handle_npd == 'lu':
d = torch.linalg.solve(hess, g.neg())
elif handle_npd == 'grad':
elif handle_npd in ['grad', 'cauchy']:
d = g.neg()
elif handle_npd == 'cauchy':
# cauchy point for a trust radius of delta=1.
# equivalent to 'grad' method with scaled lr
gnorm = g.norm(p=2)
scale = 1 / gnorm
gHg = g.dot(hess.mv(g))
if gHg > 0:
scale *= torch.clamp_max_(gnorm.pow(3) / gHg, max=1)
d = scale * g.neg()
if handle_npd == 'cauchy':
# cauchy point for a trust radius of delta=1.
# equivalent to 'grad' with a scaled lr
gnorm = g.norm(p=2)
scale = 1 / gnorm
gHg = g.dot(hess.mv(g))
if gHg > 0:
scale *= torch.clamp_(gnorm.pow(3) / gHg, max=1)
d *= scale
elif handle_npd == 'eig':
# this setting is experimental! use with caution
# TODO: why use the factor 1.5 here? Seems to work best
Expand Down

0 comments on commit c6b54ae

Please sign in to comment.