Skip to content

Commit

Permalink
Fix lazy evaluation of gradients in line searches (argmin-rs#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
w1th0utnam3 authored Mar 23, 2021
1 parent 0286082 commit 9fab420
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/solver/linesearch/backtracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ where
cost
};

self.init_grad = state.get_grad().unwrap_or(op.gradient(&self.init_param)?);
self.init_grad = state
.get_grad()
.map(Result::Ok)
.unwrap_or_else(|| op.gradient(&self.init_param))?;

if self.search_direction.is_none() {
return Err(ArgminError::NotInitialized {
Expand Down
5 changes: 4 additions & 1 deletion src/solver/linesearch/hagerzhang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,10 @@ where
cost
};

self.init_grad = state.get_grad().unwrap_or(op.gradient(&self.init_param)?);
self.init_grad = state
.get_grad()
.map(Result::Ok)
.unwrap_or_else(|| op.gradient(&self.init_param))?;

self.a_x = self.a_x_init;
self.b_x = self.b_x_init;
Expand Down
3 changes: 2 additions & 1 deletion src/solver/linesearch/morethuente.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ where

self.init_grad = state
.get_grad()
.unwrap_or_else(|| op.gradient(&self.init_param).unwrap());
.map(Result::Ok)
.unwrap_or_else(|| op.gradient(&self.init_param))?;

self.dginit = self.init_grad.dot(&self.search_direction);

Expand Down

0 comments on commit 9fab420

Please sign in to comment.