-
Notifications
You must be signed in to change notification settings - Fork 4
/
Finito.jl
234 lines (206 loc) · 6.38 KB
/
Finito.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Latafat, Themelis, Patrinos, "Block-coordinate and incremental aggregated
# proximal gradient methods for nonsmooth nonconvex problems."
# arXiv:1906.10053 (2019).
#
# Latafat. "Distributed proximal algorithms for large-scale structured optimization"
# PhD thesis, KU Leuven, 7 2020.
#
# Mairal, "Incremental majorization-minimization optimization with application to
# large-scale machine learning."
# SIAM Journal on Optimization 25, 2 (2015), 829–855.
#
# Defazio, Domke, "Finito: A faster, permutable incremental gradient method
# for big data problems."
# In International Conference on Machine Learning (2014), pp. 1125-1133.
#
using LinearAlgebra
using ProximalOperators
using ProximalAlgorithms.IterationTools
using Printf
using Base.Iterators
using Random
using StatsBase: sample
export solution
include("Finito_basic.jl")
include("Finito_LFinito.jl")
include("Finito_adaptive.jl")
struct Finito{R<:Real}
γ::Maybe{Union{Array{R},R}}
sweeping::Int8
LFinito::Bool
adaptive::Bool
minibatch::Tuple{Bool,Int}
maxit::Int
verbose::Bool
freq::Int
α::R
tol::R
tol_b::R
function Finito{R}(;
γ::Maybe{Union{Array{R},R}} = nothing,
sweeping = 1,
LFinito::Bool = false,
adaptive::Bool = false,
minibatch::Tuple{Bool,Int} = (false, 1),
maxit::Int = 10000,
verbose::Bool = false,
freq::Int = 10000,
α::R = R(0.999),
tol::R = R(1e-8),
tol_b::R = R(1e-9),
) where {R}
@assert γ === nothing || minimum(γ) > 0
@assert maxit > 0
@assert tol > 0
@assert tol_b > 0
@assert freq > 0
new(γ, sweeping, LFinito, adaptive, minibatch, maxit, verbose, freq, α, tol, tol_b)
end
end
function (solver::Finito{R})(
x0::AbstractArray{C};
F = nothing,
g = ProximalOperators.Zero(),
L = nothing,
N = N,
) where {R,C<:RealOrComplex{R}}
stop(state) = false
disp(it, state) = @printf "%5d | %.3e \n" it state.hat_γ
F === nothing && (F = fill(ProximalOperators.Zero(), (N,)))
# dispatching the iterator
if solver.LFinito
iter = FINITO_LFinito_iterable(
F,
g,
x0,
N,
L,
solver.γ,
solver.sweeping,
solver.minibatch[2],
solver.α,
)
elseif solver.adaptive
iter = FINITO_adaptive_iterable(
F,
g,
x0,
N,
L,
solver.tol,
solver.tol_b,
solver.sweeping,
solver.α,
)
else
iter = FINITO_basic_iterable(
F,
g,
x0,
N,
L,
solver.γ,
solver.sweeping,
solver.minibatch[2],
solver.α,
)
end
iter = take(halt(iter, stop), solver.maxit)
iter = enumerate(iter)
num_iters, state_final = nothing, nothing
for (it_, state_) in iter # unrolling the iterator
# see https://docs.julialang.org/en/v1/manual/interfaces/index.html
if solver.verbose && mod(it_, solver.freq) == 0
disp(it_, state_)
end
num_iters, state_final = it_, state_
end
if solver.verbose && mod(num_iters, solver.freq) !== 0
disp(num_iters, state_final)
end # for the final iteration
return solution(state_final), num_iters
end
"""
Finito([γ, sweeping, LFinito, adaptive, minibatch, maxit, verbose, freq, tol, tol_b])
Instantiate the Finito algorithm for solving fully nonconvex optimization problems of the form
minimize 1/N sum_{i=1}^N f_i(x) + g(x)
where `f_i` are smooth and `g` is possibly nonsmooth, all of which may be nonconvex.
If `solver = Finito(args...)`, then the above problem is solved with
solver(x0, [F, g, N, L])
where F is an array containing f_i's, x0 is the initial point, and L is an array of
smoothness moduli of f_i's; it is optional in the adaptive mode or if γ is provided.
Optional keyword arguments are:
* `γ`: an array of N stepsizes for each coordinate
* `sweeping::Int` 1 for uniform randomized (default), 2 for cyclic, 3 for shuffled
* `LFinito::Bool` low memory variant of the Finito/MISO algorithm
* `adaptive::Bool` to activate adaptive smoothness parameter computation
* `minibatch::(Bool,Int)` to use batchs of a given size
* `maxit::Integer` (default: `10000`), maximum number of iterations to perform.
* `verbose::Bool` (default: `true`), whether or not to print information during the iterations.
* `freq::Integer` (default: `10000`), frequency of verbosity.
* `α::R` parameter where γ_i = αN/L_i
* `tol::Real` (default: `1e-8`), absolute tolerance for the adaptive case
* `tol_b::R` tolerance for the backtrack (default: `1e-9`)
"""
Finito(::Type{R}; kwargs...) where {R} = Finito{R}(; kwargs...)
Finito(; kwargs...) = Finito(Float64; kwargs...)
"""
If `solver = Finito(args...)`, then
itr = iterator(solver, x0, [F, g, N, L])
is an iterable object. Note that [maxit, verbose, freq] fields of the solver are ignored here.
The solution at any given state can be obtained using solution(state), e.g.,
for state in Iterators.take(itr, maxit)
# do something using solution(state)
end
See https://docs.julialang.org/en/v1/manual/interfaces/index.html
and https://docs.julialang.org/en/v1/base/iterators/ for a list of iteration utilities
"""
function iterator(
solver::Finito{R},
x0::AbstractArray{C};
F = nothing,
g = ProximalOperators.Zero(),
L = nothing,
N = N,
) where {R,C<:RealOrComplex{R}}
F === nothing && (F = fill(ProximalOperators.Zero(), (N,)))
# dispatching the iterator
if solver.LFinito
iter = FINITO_LFinito_iterable(
F,
g,
x0,
N,
L,
solver.γ,
solver.sweeping,
solver.minibatch[2],
solver.α,
)
elseif solver.adaptive
iter = FINITO_adaptive_iterable(
F,
g,
x0,
N,
L,
solver.tol,
solver.tol_b,
solver.sweeping,
solver.α,
)
else
iter = FINITO_basic_iterable(
F,
g,
x0,
N,
L,
solver.γ,
solver.sweeping,
solver.minibatch[2],
solver.α,
)
end
return iter
end