Switch to unified view

a b/combinedDeepLearningActiveContour/minFunc/lbfgs.m
1
function [d] = lbfgs(g,s,y,Hdiag)
2
% BFGS Search Direction
3
%
4
% This function returns the (L-BFGS) approximate inverse Hessian,
5
% multiplied by the gradient
6
%
7
% If you pass in all previous directions/sizes, it will be the same as full BFGS
8
% If you truncate to the k most recent directions/sizes, it will be L-BFGS
9
%
10
% s - previous search directions (p by k)
11
% y - previous step sizes (p by k)
12
% g - gradient (p by 1)
13
% Hdiag - value of initial Hessian diagonal elements (scalar)
14
15
[p,k] = size(s);
16
17
for i = 1:k
18
    ro(i,1) = 1/(y(:,i)'*s(:,i));
19
end
20
21
q = zeros(p,k+1);
22
r = zeros(p,k+1);
23
al =zeros(k,1);
24
be =zeros(k,1);
25
26
q(:,k+1) = g;
27
28
for i = k:-1:1
29
    al(i) = ro(i)*s(:,i)'*q(:,i+1);
30
    q(:,i) = q(:,i+1)-al(i)*y(:,i);
31
end
32
33
% Multiply by Initial Hessian
34
r(:,1) = Hdiag*q(:,1);
35
36
for i = 1:k
37
    be(i) = ro(i)*y(:,i)'*r(:,i);
38
    r(:,i+1) = r(:,i) + s(:,i)*(al(i)-be(i));
39
end
40
d=r(:,k+1);