|
a |
|
b/combinedDeepLearningActiveContour/minFunc/dampedUpdate.m |
|
|
1 |
function [old_dirs,old_stps,Hdiag,Bcompact] = lbfgsUpdate(y,s,corrections,debug,old_dirs,old_stps,Hdiag) |
|
|
2 |
|
|
|
3 |
%B0 = eye(length(y))/Hdiag; |
|
|
4 |
S = old_dirs(:,2:end); |
|
|
5 |
Y = old_stps(:,2:end); |
|
|
6 |
k = size(Y,2); |
|
|
7 |
L = zeros(k); |
|
|
8 |
for j = 1:k |
|
|
9 |
for i = j+1:k |
|
|
10 |
L(i,j) = S(:,i)'*Y(:,j); |
|
|
11 |
end |
|
|
12 |
end |
|
|
13 |
D = diag(diag(S'*Y)); |
|
|
14 |
N = [S/Hdiag Y]; |
|
|
15 |
M = [S'*S/Hdiag L;L' -D]; |
|
|
16 |
|
|
|
17 |
ys = y'*s; |
|
|
18 |
Bs = s/Hdiag - N*(M\(N'*s)); % Product B*s |
|
|
19 |
sBs = s'*Bs; |
|
|
20 |
|
|
|
21 |
eta = .02; |
|
|
22 |
if ys < eta*sBs |
|
|
23 |
if debug |
|
|
24 |
fprintf('Damped Update\n'); |
|
|
25 |
end |
|
|
26 |
theta = min(max(0,((1-eta)*sBs)/(sBs - ys)),1); |
|
|
27 |
y = theta*y + (1-theta)*Bs; |
|
|
28 |
end |
|
|
29 |
|
|
|
30 |
|
|
|
31 |
numCorrections = size(old_dirs,2); |
|
|
32 |
if numCorrections < corrections |
|
|
33 |
% Full Update |
|
|
34 |
old_dirs(:,numCorrections+1) = s; |
|
|
35 |
old_stps(:,numCorrections+1) = y; |
|
|
36 |
else |
|
|
37 |
% Limited-Memory Update |
|
|
38 |
old_dirs = [old_dirs(:,2:corrections) s]; |
|
|
39 |
old_stps = [old_stps(:,2:corrections) y]; |
|
|
40 |
end |
|
|
41 |
|
|
|
42 |
% Update scale of initial Hessian approximation |
|
|
43 |
Hdiag = (y'*s)/(y'*y); |