function w = learnRegERMLearningRate(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 = 100000;
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;
    objective = sum(l)/numel(l) + lambda/2*r;
	if k > 1
        if objective < objective_old
            mu = mu;
        else
            mu = mu*(g_old'*g_old)/((g_old - g)'*g_old);
%             mu = mu / 1.1;
%         mu = k^(-1.2);
        end

	end
	w = w - mu*g;
	if norm(mu*g) < e
		break
    end
    objective_old = objective;
    g_old = g;
end
disp(['number of used iterations: ' num2str(k)]);
beep;

