I have this regularized least square formula: $$\sum\limits_{i=1}^N (\omega^T x_i - y_i)^2 + \lambda \left\|\omega\right\|^2$$
And the gradient: $$2 \sum\limits_{i=1}^N ((\sum\limits_{j=1}^d x_{ij}\omega_j)x_{ik} - x_{ik} y_i) + 2\lambda \omega_k$$
I want to use gradient descent to find the vector w. I am using matlab. I though I would be able to make two loops and calculate the ws but my solution is very unstable and I need to use very small learning term a (a=0.000000001) in order to get not NAN solution. But I thought the values of w should head towards 0 when the lambda is large but it does not happen... My data set is a matrix X (400x64) and y (400x1). This is two class problem where contains the class labels (+1 for class 1 and -1 for class 2).
Here is my matlab code:
function [ w ] = gradDecent( X, Y, a, lambda, iter )
% GRADIENT DESCENT
w = zeros(size(X(1,:)))';
for it=1:iter % For each iteration
for k = 1:size(w,1)
s = 0;
for i = 1:size(X,1)
s = s + (X(i,:)*w - Y(i))*X(i,k);
end
w(k) = w(k) - a*(2*s+2*lambda*w(k));
end
end
Am I making some stupid mistakes?