function w = learnRegERM(X,y,lambda,f_loss,f_reg)
% 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_reg function handle of regularizer (default omega2)
%
% Trains linear model and returns weight vector

if nargin < 3
	lambda = 1;
end
if nargin < 4
	f_loss = @hinge;
end
if nargin < 5
	f_reg = @omega2;
end

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

w = zeros(size(X, 2), 1);
for k = 1:max_iter
	[l,lg] = f_loss(X*w, y);
	[r,rg] = f_reg(w, lambda);
	g = X'*lg + rg;
	if k > 1
		mu = mu*(g_old'*g_old)/((g_old - g)'*g_old);
	end
	w = w - mu*g;
	if norm(mu*g) < e
		break
	end
	g_old = g;
end
beep;

