-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.jl
237 lines (212 loc) · 5.4 KB
/
utils.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
235
236
237
using DataStructures: MutableBinaryMaxHeap, top_with_handle, delete!
function proj_simplex0(v::Array{T, 1}, s::T=one(T)) where {T<:Real}
n = length(v)
if sum(v) == s && all(v .≥ 0)
w = v
else
u = sort(v, rev=true)
cssv = cumsum(u)
ρ = findall(>(0), u .* Array((1:n)) .> (cssv .- s))[end]
θ = (cssv[ρ] - s) / (ρ)
w = max.(v .- θ, zero(T))
end
return w
end
""" Algorithm from Held, M., Wolfe, P., Crowder, H.: 'Validation of
subgradient optimization'. The description is from the Condat L.:
'Fast Projection onto the Simplex and the l1 Ball'. (Algorithm 1)"""
function proj_simplex1(y::Array{T, 1}, a::T=one(T)) where {T<:Real}
n = length(y)
if sum(y) == a && all(y .≥ 0)
x = y
else
let τ
u = sort(y, rev=true)
cumsum_u = zero(T)
for k in 1:n
if cumsum_u + u[k] < k * u[k] + a
cumsum_u += u[k]
τ = (cumsum_u - a) / k
else
break
end
end
x = max.(y .- τ, zero(T))
end
end
return x
end
function proj_simplex12(y::Array{T, 1}, a::T=one(T)) where {T<:Real}
n = length(y)
if sum(y) == a && all(y .≥ 0)
return y
else
τ = 0.0
u = sort(y, rev=true)
cumsum_u = zero(T)
for k in 1:n
if cumsum_u + u[k] < k * u[k] + a
cumsum_u += u[k]
τ = (cumsum_u - a) / k
else
break
end
end
y = max.(y .- τ, zero(T))
end
return y
end
""" Algorithm from van den Berg, E., Friedlander, M.P.: 'Probing the
Pareto frontier for basis pursuit solution'. The description is from
Condat L: 'Fast Projection onto the Simplex and the l1
Ball'. (Algorithm 2)"""
function proj_simplex2(y::Array{T, 1}, a::T=one(T)) where {T<:Real}
N = length(y)
if sum(y) == a && all(y .≥ 0)
x = y
else
τ = zero(T)
v = MutableBinaryMaxHeap(y)
cumsum_u = zero(T)
for k in 1:N
u = first(v)
if cumsum_u + u < k * u + a
cumsum_u += u
i = top_with_handle(v)[2]
delete!(v, i)
τ = (cumsum_u - a) / k
else
break
end
end
x = max.(y .- τ, zero(T))
end
return x
end
function proj_simplex22(y::Array{Float64, 1}, a=1.0)
N = length(y)
if sum(y) == a && all(y .≥ 0)
x = y
else
τ = 0.
v = MutableBinaryMaxHeap(y)
cumsum_u = 0.
for k in 1:N
u = first(v)
if cumsum_u + u < k * u + a
cumsum_u += u
i = top_with_handle(v)[2]
delete!(v, i)
τ = (cumsum_u - a) / k
else
break
end
end
x = max.(y .- τ, 0.)
end
return x
end
"""
From Condat “Fast projection onto the simplex and the l_1 ball”. In: Mathematical Programming 158.1
(2016), pp. 575–585
"""
function proj_simplex_condat(y::Array{Float64, 1}, a=Float64(1.0))
N = length(y)
v = [y[1]]
v_tilde = Float64[]
ρ = y[1] - a
for n in 2:N
yn = y[n]
if yn > ρ
ρ += (yn - ρ) / (length(v) + 1)
if ρ > yn - a
append!(v, y[n])
else
append!(v_tilde, v)
v = [yn]
ρ = yn - a
end
end
end
if !isempty(v_tilde)
for yi in v_tilde
if yi > ρ
append!(v, yi)
ρ += (yi - ρ) / length(v)
end
end
end
# if during the loop rho is increases at least once, then the flag is true. Otherwise we stop
flag = true
while flag
flag = false
for (i, yi) in enumerate(v)
if yi ≤ ρ
deleteat!(v, i)
ρ += (ρ - yi) / length(v)
flag = true
end
end
end
τ = ρ
x = max.(y .- τ, zero(y))
return x
end
function proj_simplex3(v, z=1.)
n = length(v)
U = Array((1:n))
s = 0
ρ = 0
while length(U) > 0
G = []
L = []
k = U[rand(1:length(U))]
ds = v[k]
for j in U
if v[j] >= v[k]
if j != k
ds += v[j]
append!(G, j)
end
elseif v[j] < v[k]
append!(L, j)
end
end
drho = length(G) + 1
if s + ds - (ρ + drho) * v[k] < z
s += ds
ρ += drho
U = L
else
U = G
end
end
theta = (s - z) / ρ
return max.(v .- theta, 0.)
end
function proj_simplex4(v, z=1., τ=1e-7, max_iter=1000)
lower = 0
upper = maximum(v)
current = Inf
w = zeros(n)
for it in 1:max_iter
if abs(current) / z < τ && current < 0.
break
end
theta = (upper + lower) / 2.0
w = max.(v .- theta, 0.)
current = sum(w) - z
if current <= 0.
upper = theta
else
lower = theta
end
end
return w
end
function softmax(x::Array{T, 1}) where {T<:Real}
# compare with NNlib implementation
res = exp.(x .- maximum(x))
res ./= sum(res)
return res
end