-
Notifications
You must be signed in to change notification settings - Fork 4
/
SAGA.jl
191 lines (144 loc) · 5.78 KB
/
SAGA.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
# Defazio, Bach, Lacoste-Julien: SAGA: A fast incremental gradient method
# with support for non-strongly convex composite objectives.
# In: Advances in neural information processing systems, pp. 1646–1654 (2014).
#
# Reddi, Sra, Poczos, and Smola, "Proximal stochastic methods for nonsmooth
# nonconvex finite-sum optimization." In Advances in Neural Information
# Processing Systems, pp. 1145–1153 (2016).
#
# Schmidt, Le Roux, Bach, "Minimizing finite sums with the stochastic average gradient"
# Mathematical Programming, 162(1-2), 83-112 (2017).
#
using LinearAlgebra
using ProximalOperators
using ProximalAlgorithms.IterationTools
using Printf
using Base.Iterators
using Random
export solution
include("SAGA_basic.jl")
struct SAGA{R<:Real}
γ::Maybe{R}
maxit::Int
verbose::Bool
freq::Int
SAG_flag::Bool
function SAGA{R}(;
γ::Maybe{R} = nothing,
maxit::Int = 10000,
verbose::Bool = false,
freq::Int = 1000,
SAG_flag::Bool = false,
) where {R}
@assert γ === nothing || γ > 0
@assert maxit > 0
@assert freq > 0
new(γ, maxit, verbose, freq, SAG_flag)
end
end
function (solver::SAGA{R})(
x0::AbstractArray{C};
F = nothing,
g = ProximalOperators.Zero(),
L = nothing,
N = N,
) where {R,C<:RealOrComplex{R}}
stop(state::SAGA_basic_state) = false
disp(it, state) = @printf "%5d | %.3e \n" it state.γ
F === nothing && (F = fill(ProximalOperators.Zero(), (N,)))
# dispatching the structure
iter = SAGA_basic_iterable(F, g, x0, N, L, solver.γ, solver.SAG_flag)
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
"""
SAGA([γ, maxit, verbose, freq])
Instantiate the SAGA algorithm for solving convex optimization problems of the form
minimize 1/N sum_{i=1}^N f_i(x) + g(x)
If `solver = SAGA(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 when γ is provided.
Optional keyword arguments are:
* `γ`: stepsize
* `L`: an array of smoothness moduli of f_i's
* `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: `100`), frequency of verbosity.
References:
[1] Defazio, Bach, Lacoste-Julien, "SAGA: A fast incremental gradient method
with support for non-strongly convex composite objectives"
In: Advances in neural information processing systems, pp. 1646–1654 (2014).
[2] Reddi, Sra, Poczos, and Smola, "Proximal stochastic methods for nonsmooth
nonconvex finite-sum optimization" In Advances in Neural Information
Processing Systems, pp. 1145–1153 (2016).
"""
SAGA(::Type{R}; kwargs...) where {R} = SAGA{R}(; kwargs...)
SAGA(; kwargs...) = SAGA(Float64; kwargs...)
"""
If `solver = SAGA(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::SAGA{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
iter = SAGA_basic_iterable(F, g, x0, N, L, solver.γ, solver.SAG_flag)
return iter
end
"""
SAG([γ, maxit, verbose, freq])
Instantiate the SAG algorithm for solving convex optimization problems of the form
minimize 1/N sum_{i=1}^N f_i(x) + g(x)
If `solver = SAG(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 when γ is provided.
Optional keyword arguments are:
* `γ`: stepsize
* `L`: an array of smoothness moduli of f_i's
* `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: `100`), frequency of verbosity.
References:
[1] Schmidt, Le Roux, Bach, "Minimizing finite sums with the stochastic average gradient"
Mathematical Programming, 162(1-2), 83-112 (2017).
"""
"""
If `solver = SAG(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
"""
SAG(::Type{R}; kwargs...) where {R} = SAGA{R}(; kwargs..., SAG_flag = true)
SAG(; kwargs...) = SAG(Float64; kwargs...)