【发布时间】:2020-08-28 18:02:21
【问题描述】:
我尝试在训练集中拟合 6 次多项式,但一直失败。它不适合。我使用了以下代码,
% X is feature scaled
% Y is feature scaled too
function [J,grad,h]=costFunction(theta,X,Y,lambda)
% theta is a (n+1)*1 vectorize
% X is a m*(n+1) vector
% Y is a m*1 vector
h = X*theta;
% h is a m*1 vector
theta_r=[0;theta(2:end,:)];
J=sum([h-Y].^2)+(lambda/(2*length(Y)))*theta_r'*theta_r;
grad=zeros(length(theta),1);
for j=1:length(theta)
grad(j)=(1/length(Y))*sum((h-Y).*X(:,j));
endfor
endfunction
function [cost_history,theta]=gradientDescent(theta,X,Y,alpha,num_iter,lambda)
cost_history=zeros(num_iter,1);
for i=1:num_iter
[cost,grad,hyp]=costFunction(theta,X,Y,lambda);
theta=theta*(1-((alpha*lambda)/length(Y)))-(alpha*grad);
cost_history(i)=cost;
endfor
endfunction
initial_theta=ones(size(X)(1,2),1);
[c_history,theta]=gradientDescent(initial_theta,X,Y,1,10000,0);
plot(X*theta,cx); % cx is non-scaled feature X
hold on;
plot(cx,cy); % cx,cy contain datasets
你能找到任何问题吗?
【问题讨论】:
-
您是否尝试过调整正则化参数和学习率?
-
好吧,让我多试几次,然后我会回复先生。
-
@TathagataDey,你写了
plot(X*theta,cx);。我觉得应该是plot(cx,X*theta);。
标签: machine-learning regression octave gradient-descent