蘑菇先生 发表于 2015-7-16 08:49

自己动手写matlab——梯度下降

%最速下降法(批量梯度下降)
X=;
theta_new=';
theta_old=';
theta_best=';
Y=';
learning_rate=0.01;
loss = 1000;
epsilon = 0.0003;
i = 0;
while(1)
        i = i + 1;
        theta_new = theta_old - learning_rate*(X'*X*theta_old-X'*Y);
        theta_new
        %fprintf('theta now is %2.0f\n',theta_new);
        if((theta_new-theta_old)<epsilon)
            theta_best=theta_new;
            break;
        end
        theta_old = theta_new;
        error = (Y - X*theta_new)'*(Y - X*theta_new);
        fprintf('error now is %f\n',error);
        hold on
        plot(i,error,'+');        
end
%随机梯度下降(下降速度快,但收敛精度没有批量梯度下降法高,适用于样本量庞大的情况下)
X=;
theta_new=';
theta_old=';
theta_best=';
Y=';
learning_rate=0.01;
epsilon = 0.0003;
j=0;
while(1)
    j = j + 1;
    for i = 1:4
        temp1 = X*theta_old;
        theta_new = theta_old + learning_rate*(Y(i) - temp1(i))*X(i,:)';      
        temp2 = theta_old;
        theta_old = theta_new;   
    end
    if(abs(theta_new-temp2)<epsilon)
        theta_best = theta_new;
        break;
    end;
    error = (Y-X*theta_new)'*(Y-X*theta_new);
    hold on
    plot(j,error,'+');
end参考李航的《统计学习方法》。


蘑菇先生 发表于 2015-7-16 14:03

希望可以和大家一起交流,共同进步。

Macintosh2012 发表于 2015-11-10 02:53




这段时间要用,参考一下
页: [1]
查看完整版本: 自己动手写matlab——梯度下降