@@ -158,22 +158,20 @@ func (cg *CG) NextDirection(loc *Location, dir []float64) (stepSize float64) {
158
158
159
159
cg .iterFromRestart ++
160
160
var restart bool
161
- // Check if the method should be restarted because too many iterations have
162
- // been taken without a restart.
163
161
if cg .iterFromRestart == cg .restartAfter {
162
+ // Restart because too many iterations have been taken without a restart.
164
163
restart = true
165
164
}
166
- // Check if the method should be restarted because the angle between
167
- // the last two gradients is too large.
165
+
168
166
gDot := floats .Dot (loc .Gradient , cg .gradPrev )
169
167
gNorm := floats .Norm (loc .Gradient , 2 )
170
168
if gDot <= cg .AngleRestartThreshold * gNorm * cg .gradPrevNorm {
169
+ // Restart because the angle between the last two gradients is too large.
171
170
restart = true
172
171
}
173
172
174
- // Compute the scaling factor β_k according to the given CG variant. Do
175
- // this even when restarting, because cg.Variant may be keeping an inner
176
- // state that needs to be updated at every iteration.
173
+ // Compute the scaling factor β_k even when restarting, because cg.Variant
174
+ // may be keeping an inner state that needs to be updated at every iteration.
177
175
beta := cg .Variant .Beta (loc .Gradient , cg .gradPrev , cg .dirPrev )
178
176
if beta == 0 {
179
177
// β_k == 0 means that the steepest descent direction will be taken, so
@@ -191,7 +189,7 @@ func (cg *CG) NextDirection(loc *Location, dir []float64) (stepSize float64) {
191
189
}
192
190
}
193
191
194
- // Get the initial line search step size from the StepSizer, even if the
192
+ // Get the initial line search step size from the StepSizer even if the
195
193
// method was restarted, because StepSizers need to see every iteration.
196
194
stepSize = cg .InitialStep .StepSize (loc , dir )
197
195
if restart {
0 commit comments