
function alpha = learnKernelRegERM(X,y,lambda,f_krnl,krnl_p)
% w = learnRegERM(X,y,lambda,f_loss,f_reg)
%
% X,y labeled examples to train
% lambda regularization parameter (default 1)
% f_loss function handle of loss function (default hinge)
% f_kernl: used kernel

if nargin < 3
	lambda = 1;
end
if nargin < 4
	f_krnl = @dtw_kernel;
	krnl_p = 1;
end
if nargin < 5
	krnl_p = {};
end

f_loss = @hinge;

max_iter = 20000;
e  = 1e-3;
mu = 1;

% compute Kernel:
if strcmp(func2str(f_krnl),'dist_chi2')
	K = X;
else
	K = f_krnl(X, X, 2,krnl_p);
end
n = size(K,1);

alpha = zeros(n, 1);
for k = 1:max_iter
	[l,lg] = f_loss(K*alpha, y);
	rg = lambda * alpha;
	v = lg + rg;
	if k > 1
		mu = mu*(v_old'*K*v_old)/((v_old - v)'*K*v_old);
	end
	alpha = alpha - mu*v;
	if norm(mu*v) < e
		break
	end
	v_old = v;
end
beep;

