仅贴添加部分

lrCostFunction.m

hx=sigmoid(X*theta);
J=-(y'*log(hx)+(1-y)'*log(1-hx))/m+lambda/(2*m)*sum(theta(2:end,:).^2);
theta(1)=0;
grad=1/m*X'*(hx-y)+lambda/m*theta;

 

oneVsAll.m

options=optimset('GradObj','on','MaxIter',50);
for k=1:num_labels
initial_theta=zeros(n+1,1);
theta=fmincg(@(t)(lrCostFunction(t,X,(y==k),lambda)),initial_theta,options);
all_theta(k,:)=theta';

 

predictOneVsAll.m

[c,i]=max(sigmoid(X*all_theta'),[],2);
p=i;

 

predict.m

a1=[ones(m,1) X];
z2=Theta1*a1';
a2=[ones(1,m);sigmoid(z2)];
z3=Theta2*a2;
output=z3';
[c,i]=max(output,[],2);
p=i;

 

相关文章:

  • 2021-11-23
  • 2021-09-02
  • 2021-07-26
  • 2021-12-09
  • 2021-09-16
  • 2021-09-19
  • 2022-12-23
  • 2022-02-28
猜你喜欢
  • 2022-12-23
  • 2021-04-02
  • 2021-04-16
  • 2021-04-09
  • 2021-05-08
  • 2021-07-30
  • 2022-02-24
相关资源
相似解决方案