-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.jl
206 lines (187 loc) · 5.11 KB
/
misc.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
# Miscellaneous supporting functions
using LinearAlgebra, Printf, SparseArrays
# Numerical gradient for minFuncSO. Call with optimized loss function eval.
function numGrad(objFunc,w,X,Xw;k=nothing)
nMM = 0
(m,n) = size(X)
delta = 2*sqrt(1e-12)*(1+norm(w))
Xwplus = Xw
if k!= nothing
Xwplus = Xwplus + k
end
g = zeros(n,1)
for i in 1:n
pertCol = delta*X[:,i]
fxp,nmm = objFunc(Xwplus + pertCol,w); nMM += nmm
fxm,nmm = objFunc(Xwplus - pertCol,w); nMM += nmm
g[i,1] = (fxp - fxm)/(2*delta)
end
return (g,nMM)
end
# Numerical gradient for minFuncNonOpt. Call with non-optimized loss+gradient
# function that does not calculate gradient
function numGrad(funObj,w,X)
nMM = 0
(m,n) = size(X)
delta = 2*sqrt(1e-12)*(1+norm(w))
g = zeros(n,1)
e_i = zeros(n)
for i in 1:n
e_i[i] = 1
(fxp,_,nmm) = funObj(w + delta*e_i,X); nMM += nmm
(fxm,_,nmm) = funObj(w - delta*e_i,X); nMM += nmm
g[i,1] = (fxp - fxm)/(2*delta)
e_i[i] = 0
end
return (g,nMM)
end
# Numerical gradient for minFuncSO (forward diff)
function forwardDiffGrad(objFunc,w,X,fXw)
nMM = 0
(m,n) = size(X)
h = 2*sqrt(1e-12)*(1+norm(w))
g = zeros(n,1)
for i in 1:n
hXe_i = h*X[:,i]
fhXe_i,nmm = objFunc(hXe_i,w); nMM += nmm
g[i,1] = (fhXe_i - fXw)/h
end
return (g,nMM)
end
# Check if number is a real-finite number
function isfinitereal(x)
return (imag(x) == 0) & (!isnan(x)) & (!isinf(x))
end
# Updates memory for minFuncSO
function updateDiffs(i,lBfgsSize,g_prev,g,w_prev,w,X,DiffIterates,DiffGrads,
XDiffIterates;XDiffGrads=nothing,normalizeColumns=false,calcXDiffIterates=false,calcXDiffGrads=false)
nMM = 0
j = mod(i,lBfgsSize)
if j==0
j = lBfgsSize
end
DiffIterates[j] = w.-w_prev
DiffGrads[j] = g.-g_prev
if normalizeColumns
colNorm = norm(DiffIterates[j],2)
if colNorm > 1e-4
DiffIterates[j] = DiffIterates[j]./colNorm
end
end
if calcXDiffIterates
XDiffIterates[j] = X*DiffIterates[j]; nMM += 1
end
if calcXDiffGrads && XDiffGrads!=nothing
XDiffGrads[j] = X*DiffGrads[j]; nMM += 1
end
return nMM
end
# 2: multidimension Wolfe
# 3: solver optimized for linear composition problems, momentum as secondary directions
# 4: non-opt solver, momentum as secondary directions
# 5: solver optimized for linear composition problems, TN as secondary directions
# 7: solver optimized for linear composition problems, Adagrad as secondary directions
function lsTypeIsSO(lsType)
if (2<= lsType && lsType <= 5) || (7<=lsType && lsType<=10)
return true
else
return false
end
end
function methodHasPrecond(method)
if method== 3 || method==5
return true
end
return false
end
# returns a string description of iterative method
function methodToLabel(method;abbreviate=false)
methodLabel=""
if method==0
methodLabel="GD"
elseif method==1
methodLabel="BB"
elseif method==2
methodLabel="CG"
elseif method==3
if abbreviate
methodLabel="lQN"
else
methodLabel="lBFGS"
end
elseif method==4
methodLabel="TN"
elseif method==5
if abbreviate
methodLabel="NEW"
else
methodLabel="Newton"
end
end
return methodLabel
end
function lsTypeToLabel(lsType)
lsLabel=""
if lsType==0
lsLabel = "Arm"
elseif lsType==1
lsLabel = "lsW"
elseif lsType==2
lsLabel = "mdW"
elseif lsType==6
lsLabel = "Lip"
elseif lsType==7
lsLabel = "Adg"
elseif lsType==8
lsLabel = "NAG"
elseif lsType==9
lsLabel = "GD"
elseif lsType==10
lsLabel = "GDm"
end
return lsLabel
end
# returns a string description of line/ subspace search
function lsTypeToLabel(lsType,lsInterp,nMomDirs,ssMethod,ssLS,nonOpt)
lsLabel=""
if lsType==0
lsLabel = "Armijo"
elseif lsType==1
lsLabel = "lsWolfe"
elseif lsType==2
lsLabel = "mdWolfe"
elseif lsType==3 || lsType==4
lsLabel="2"
if nMomDirs!=0
lsLabel=string(nMomDirs+1)
end
lsLabel=string(lsLabel,"d-Mm")
elseif lsType==5
lsLabel = "2d-TN"
elseif lsType==6
lsLabel = "Lipschitz"
elseif lsType==7
lsLabel="2"
if nMomDirs!=0
lsLabel=string(nMomDirs+1)
end
lsLabel=string(lsLabel,"d-Ag")
elseif lsType==8
lsLabel = "NAG"
elseif lsType==9
lsLabel = "2d-GD"
elseif lsType==10
lsLabel = "3d-GDm"
end
if lsTypeIsSO(lsType) && lsType!=2
lsLabel=string(lsLabel,"-",methodToLabel(ssMethod,abbreviate=true),"-",
lsTypeToLabel(ssLS))
end
if lsType==4 || nonOpt==1
lsLabel=string(lsLabel,"-")
end
if lsInterp==2
lsLabel=string(lsLabel,"2")
end
return lsLabel
end