【发布时间】:2014-08-26 18:28:27
【问题描述】:
我已经完成了 Andrew 教授的几门机器学习课程,并查看了使用牛顿法进行逻辑回归的成绩单。然而,在使用梯度下降实现逻辑回归时,我面临着某些问题。
生成的图不是凸的。
我的代码如下:
我正在使用等式的矢量化实现。
%1. The below code would load the data present in your desktop to the octave memory
x=load('ex4x.dat');
y=load('ex4y.dat');
%2. Now we want to add a column x0 with all the rows as value 1 into the matrix.
%First take the length
m=length(y);
x=[ones(m,1),x];
alpha=0.1;
max_iter=100;
g=inline('1.0 ./ (1.0 + exp(-z))');
theta = zeros(size(x(1,:)))'; % the theta has to be a 3*1 matrix so that it can multiply by x that is m*3 matrix
j=zeros(max_iter,1); % j is a zero matrix that is used to store the theta cost function j(theta)
for num_iter=1:max_iter
% Now we calculate the hx or hypothetis, It is calculated here inside no. of iteration because the hupothesis has to be calculated for new theta for every iteration
z=x*theta;
h=g(z); % Here the effect of inline function we used earlier will reflect
j(num_iter)=(1/m)*(-y'* log(h) - (1 - y)'*log(1-h)) ; % This formula is the vectorized form of the cost function J(theta) This calculates the cost function
j
grad=(1/m) * x' * (h-y); % This formula is the gradient descent formula that calculates the theta value.
theta=theta - alpha .* grad; % Actual Calculation for theta
theta
end
每个说的代码没有给出任何错误,但没有产生正确的凸图。
如果有人能指出错误或分享导致问题的原因,我会很高兴。
谢谢
【问题讨论】:
-
你能展示它产生的图表吗?
-
我已经添加了图表,如果你把点连接起来,你会发现梯度下降反复上下变化,它应该减少,一段时间后应该保持不变,对于 j(min j) 应确定θ。当我使用牛顿法对 j 方法采用相同的方法时,我没有得到它,我只在 10 次迭代时得到正确的输出。谢谢你的帮助!!
-
关于图:- X 轴是迭代次数,Y 轴是 j(theta) 成本函数。
标签: machine-learning octave logistic-regression gradient-descent