Skip to content

Commit

Permalink
add both function value and normgrad to history
Browse files Browse the repository at this point in the history
  • Loading branch information
Jutho committed Jun 25, 2020
1 parent 94a710e commit 6f08a89
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/cg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function optimize(fg, x, alg::ConjugateGradient;
numfg = 1
innergg = inner(x, g, g)
normgrad = sqrt(innergg)
fhistory = [f]
normgradhistory = [normgrad]

# compute here once to define initial value of α in scale-invariant way
Expand Down Expand Up @@ -71,6 +72,7 @@ function optimize(fg, x, alg::ConjugateGradient;
x, f, g = finalize!(x, f, g, numiter)
innergg = inner(x, g, g)
normgrad = sqrt(innergg)
push!(fhistory, f)
push!(normgradhistory, normgrad)

# check stopping criteria and print info
Expand Down Expand Up @@ -102,7 +104,8 @@ function optimize(fg, x, alg::ConjugateGradient;
f, normgrad)
end
end
return x, f, g, numfg, normgradhistory
history = [fhistory normgradhistory]
return x, f, g, numfg, history
end

struct HagerZhang{T<:Real} <: CGFlavor
Expand Down
9 changes: 6 additions & 3 deletions src/gd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function optimize(fg, x, alg::GradientDescent;
numfg = 1
innergg = inner(x, g, g)
normgrad = sqrt(innergg)
fhistory = [f]
normgradhistory = [normgrad]

# compute here once to define initial value of α in scale-invariant way
Expand All @@ -46,15 +47,16 @@ function optimize(fg, x, alg::GradientDescent;
x, f, g = finalize!(x, f, g, numiter)
innergg = inner(x, g, g)
normgrad = sqrt(innergg)
push!(fhistory, f)
push!(normgradhistory, normgrad)

# check stopping criteria and print info
if normgrad <= alg.gradtol || numiter >= alg.maxiter
break
end
verbosity >= 2 &&
@info @sprintf("GD: iter %4d: f = %.12f, ‖∇f‖ = %.4e, step size = %.2e",
numiter, f, normgrad, α)
@info @sprintf("GD: iter %4d: f = %.12f, ‖∇f‖ = %.4e, α = %.2e, nfg = %d",
numiter, f, normgrad, α, nfg)

# increase α for next step
α = 2*α
Expand All @@ -68,5 +70,6 @@ function optimize(fg, x, alg::GradientDescent;
f, normgrad)
end
end
return x, f, g, numfg, normgradhistory
history = [fhistory normgradhistory]
return x, f, g, numfg, history
end
5 changes: 4 additions & 1 deletion src/lbfgs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function optimize(fg, x, alg::LBFGS;
numfg = 1
innergg = inner(x, g, g)
normgrad = sqrt(innergg)
fhistory = [f]
normgradhistory = [normgrad]

TangentType = typeof(g)
Expand Down Expand Up @@ -64,6 +65,7 @@ function optimize(fg, x, alg::LBFGS;
x, f, g = finalize!(x, f, g, numiter)
innergg = inner(x, g, g)
normgrad = sqrt(innergg)
push!(fhistory, f)
push!(normgradhistory, normgrad)

# check stopping criteria and print info
Expand Down Expand Up @@ -140,7 +142,8 @@ function optimize(fg, x, alg::LBFGS;
f, normgrad)
end
end
return x, f, g, numfg, normgradhistory
history = [fhistory normgradhistory]
return x, f, g, numfg, history
end

mutable struct LBFGSInverseHessian{TangentType,ScalarType}
Expand Down
3 changes: 2 additions & 1 deletion src/linesearches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ function bisect(iter::HagerZhangLineSearchIterator, a::LineSearchPoint, b::LineS
numfg = 0
while true
if b.α - a.α < eps()
error(@sprintf("Linesearch bisection failure: [a, b] = [%.2e, %.2e], b-a = %.2e, dϕᵃ = %.2e, dϕᵇ = %.2e, (ϕᵇ - ϕᵃ)/(b-a) = %.2e", a.α, b.α, b.α - a.α, a.dϕ, b.dϕ, (b.ϕ - a.ϕ)/(b.α - a.α)))
@warn @sprintf("Linesearch bisection failure: [a, b] = [%.2e, %.2e], b-a = %.2e, dϕᵃ = %.2e, dϕᵇ = %.2e, (ϕᵇ - ϕᵃ)/(b-a) = %.2e", a.α, b.α, b.α - a.α, a.dϕ, b.dϕ, (b.ϕ - a.ϕ)/(b.α - a.α))
return a, b, numfg
end
αc = (1 - θ) * a.α + θ * b.α
c = takestep(iter, αc)
Expand Down

0 comments on commit 6f08a89

Please sign in to comment.