Skip to content

Commit abd80ca

Browse files
authored
Merge pull request #23 from leburgel/patch-1
Record time at start of every LBFGS iteration
2 parents e861492 + 3b8ec9b commit abd80ca

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

src/OptimKit.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ Also see [`GradientDescent`](@ref), [`ConjugateGradient`](@ref), [`LBFGS`](@ref)
104104
"""
105105
function optimize end
106106

107+
function format_time(t::Float64)
108+
if t < 1e-3
109+
return @sprintf("%5.1f μs", 1e6*t)
110+
elseif t < 1
111+
return @sprintf("%5.1f ms", 1e3*t)
112+
elseif t < 60
113+
return @sprintf("%5.2f s", t)
114+
elseif t < 3600
115+
return @sprintf("%5.2f m", t / 60)
116+
else
117+
return @sprintf("%.2f h", t / 3600)
118+
end
119+
end
120+
107121
include("linesearches.jl")
108122
include("gd.jl")
109123
include("cg.jl")

src/cg.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ function optimize(fg, x, alg::ConjugateGradient;
9898

9999
numiter = 0
100100
verbosity >= 2 &&
101-
@info @sprintf("CG: initializing with f = %.12f, ‖∇f‖ = %.4e", f, normgrad)
101+
@info @sprintf("CG: initializing with f = %.12e, ‖∇f‖ = %.4e", f, normgrad)
102102
local xprev, gprev, Pgprev, ηprev
103103
while !(_hasconverged || _shouldstop)
104+
told = t
104105
# compute new search direction
105106
if precondition === _precondition
106107
Pg = g
@@ -140,6 +141,7 @@ function optimize(fg, x, alg::ConjugateGradient;
140141
push!(fhistory, f)
141142
push!(normgradhistory, normgrad)
142143
t = time() - t₀
144+
Δt = t - told
143145
_hasconverged = hasconverged(x, f, g, normgrad)
144146
_shouldstop = shouldstop(x, f, g, numfg, numiter, t)
145147

@@ -148,8 +150,8 @@ function optimize(fg, x, alg::ConjugateGradient;
148150
break
149151
end
150152
verbosity >= 3 &&
151-
@info @sprintf("CG: iter %4d, time %7.2f s: f = %.12f, ‖∇f‖ = %.4e, α = %.2e, β = %.2e, nfg = %d",
152-
numiter, t, f, normgrad, α, β, nfg)
153+
@info @sprintf("CG: iter %4d, Δt %s: f = %.12e, ‖∇f‖ = %.4e, α = %.2e, β = %.2e, nfg = %d",
154+
numiter, format_time(Δt), f, normgrad, α, β, nfg)
153155

154156
# transport gprev, ηprev and vectors in Hessian approximation to x
155157
gprev = transport!(gprev, xprev, ηprev, α, x)
@@ -165,12 +167,12 @@ function optimize(fg, x, alg::ConjugateGradient;
165167
end
166168
if _hasconverged
167169
verbosity >= 2 &&
168-
@info @sprintf("CG: converged after %d iterations and time %.2f s: f = %.12f, ‖∇f‖ = %.4e",
169-
numiter, t, f, normgrad)
170+
@info @sprintf("CG: converged after %d iterations and time %s: f = %.12e, ‖∇f‖ = %.4e",
171+
numiter, format_time(t), f, normgrad)
170172
else
171173
verbosity >= 1 &&
172-
@warn @sprintf("CG: not converged to requested tol after %d iterations and time %.2f s: f = %.12f, ‖∇f‖ = %.4e",
173-
numiter, t, f, normgrad)
174+
@warn @sprintf("CG: not converged to requested tol after %d iterations and time %s: f = %.12e, ‖∇f‖ = %.4e",
175+
numiter, format_time(t), f, normgrad)
174176
end
175177
history = [fhistory normgradhistory]
176178
return x, f, g, numfg, history

src/gd.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ function optimize(fg, x, alg::GradientDescent;
7676

7777
numiter = 0
7878
verbosity >= 2 &&
79-
@info @sprintf("GD: initializing with f = %.12f, ‖∇f‖ = %.4e", f, normgrad)
79+
@info @sprintf("GD: initializing with f = %.12e, ‖∇f‖ = %.4e", f, normgrad)
8080
while !(_hasconverged || _shouldstop)
81+
told = t
8182
# compute new search direction
8283
Pg = precondition(x, deepcopy(g))
8384
η = scale!(Pg, -1) # we don't need g or Pg anymore, so we can overwrite it
@@ -97,6 +98,7 @@ function optimize(fg, x, alg::GradientDescent;
9798
push!(fhistory, f)
9899
push!(normgradhistory, normgrad)
99100
t = time() - t₀
101+
Δt = t - told
100102
_hasconverged = hasconverged(x, f, g, normgrad)
101103
_shouldstop = shouldstop(x, f, g, numfg, numiter, t)
102104

@@ -105,20 +107,20 @@ function optimize(fg, x, alg::GradientDescent;
105107
break
106108
end
107109
verbosity >= 3 &&
108-
@info @sprintf("GD: iter %4d, time %7.2f s: f = %.12f, ‖∇f‖ = %.4e, α = %.2e, nfg = %d",
109-
numiter, t, f, normgrad, α, nfg)
110+
@info @sprintf("GD: iter %4d, Δt %s: f = %.12e, ‖∇f‖ = %.4e, α = %.2e, nfg = %d",
111+
numiter, format_time(Δt), f, normgrad, α, nfg)
110112

111113
# increase α for next step
112114
α = 2 * α
113115
end
114116
if _hasconverged
115117
verbosity >= 2 &&
116-
@info @sprintf("GD: converged after %d iterations and time %.2f s: f = %.12f, ‖∇f‖ = %.4e",
117-
numiter, t, f, normgrad)
118+
@info @sprintf("GD: converged after %d iterations and time %s: f = %.12e, ‖∇f‖ = %.4e",
119+
numiter, format_time(t), f, normgrad)
118120
else
119121
verbosity >= 1 &&
120-
@warn @sprintf("GD: not converged to requested tol after %d iterations and time %.2f s: f = %.12f, ‖∇f‖ = %.4e",
121-
numiter, t, f, normgrad)
122+
@warn @sprintf("GD: not converged to requested tol after %d iterations and time %s: f = %.12e, ‖∇f‖ = %.4e",
123+
numiter, format_time(t), f, normgrad)
122124
end
123125
history = [fhistory normgradhistory]
124126
return x, f, g, numfg, history

src/lbfgs.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ function optimize(fg, x, alg::LBFGS;
8080
H = LBFGSInverseHessian(m, TangentType[], TangentType[], ScalarType[])
8181

8282
verbosity >= 2 &&
83-
@info @sprintf("LBFGS: initializing with f = %.12f, ‖∇f‖ = %.4e", f, normgrad)
83+
@info @sprintf("LBFGS: initializing with f = %.12e, ‖∇f‖ = %.4e", f, normgrad)
8484

8585
while !(_hasconverged || _shouldstop)
86+
told = t
8687
# compute new search direction
8788
if length(H) > 0
8889
Hg = let x = x
@@ -117,6 +118,7 @@ function optimize(fg, x, alg::LBFGS;
117118
push!(fhistory, f)
118119
push!(normgradhistory, normgrad)
119120
t = time() - t₀
121+
Δt = t - told
120122
_hasconverged = hasconverged(x, f, g, normgrad)
121123
_shouldstop = shouldstop(x, f, g, numfg, numiter, t)
122124

@@ -125,8 +127,8 @@ function optimize(fg, x, alg::LBFGS;
125127
break
126128
end
127129
verbosity >= 3 &&
128-
@info @sprintf("LBFGS: iter %4d, time %7.2f s: f = %.12f, ‖∇f‖ = %.4e, α = %.2e, m = %d, nfg = %d",
129-
numiter, t, f, normgrad, α, length(H), nfg)
130+
@info @sprintf("LBFGS: iter %4d, Δt %s: f = %.12e, ‖∇f‖ = %.4e, α = %.2e, m = %d, nfg = %d",
131+
numiter, format_time(Δt), f, normgrad, α, length(H), nfg)
130132

131133
# transport gprev, ηprev and vectors in Hessian approximation to x
132134
gprev = transport!(gprev, xprev, ηprev, α, x)
@@ -190,12 +192,12 @@ function optimize(fg, x, alg::LBFGS;
190192
end
191193
if _hasconverged
192194
verbosity >= 2 &&
193-
@info @sprintf("LBFGS: converged after %d iterations and time %.2f s: f = %.12f, ‖∇f‖ = %.4e",
194-
numiter, t, f, normgrad)
195+
@info @sprintf("LBFGS: converged after %d iterations and time %s: f = %.12e, ‖∇f‖ = %.4e",
196+
numiter, format_time(t), f, normgrad)
195197
else
196198
verbosity >= 1 &&
197-
@warn @sprintf("LBFGS: not converged to requested tol after %d iterations and time %.2f s: f = %.12f, ‖∇f‖ = %.4e",
198-
numiter, t, f, normgrad)
199+
@warn @sprintf("LBFGS: not converged to requested tol after %d iterations and time %s: f = %.12e, ‖∇f‖ = %.4e",
200+
numiter, format_time(t), f, normgrad)
199201
end
200202
history = [fhistory normgradhistory]
201203
return x, f, g, numfg, history

0 commit comments

Comments
 (0)