machine-learning - 我的线性回归梯度下降不会收敛
问题描述
我被困了好几个小时,试图弄清楚为什么我的线性回归梯度下降代码不会收敛。尝试了非常小的 alpha 和非常大的迭代,仍然无法正常工作。
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent
% t to learn theta
% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y) % number of training examples
ST0=0;
ST1=0;
for iter = 1:num_iters
for i=1:m
ST0=ST0+((theta(1)+theta(2)*X(i))-y(i));
ST1=ST1+(((theta(1)+theta(2)*X(i))-y(i)))*X(i,2);
end
ST0=ST0/m;
ST0=ST0*alpha;
ST1=ST1/m;
ST1=ST1*alpha;
theta(1)=theta(1)-ST0;
theta(2)=theta(2)-ST1;
J= computeCost(X, y, theta);
J_history(iter) = J;
end
end
解决方案
推荐阅读
- python - 如何在 matplotlib 中使用 Font Awesome 符号作为标记
- php - 如何让我的 html/php 代码从 1 个提交按钮定向到两个不同的页面?
- java - 为链表创建我自己的 equals 方法
- assembly - 如何在 Assembly 中实现定点算术?
- sql - 在 Oracle DB 中使用 LISTAGG 时如何从字符串中删除 CHR(0)?
- forms - 如何从 VueX 商店触发组件内的表单
- java - 不确定如何使用 Junit 来验证对象是否有效
- javascript - 得到“无法序列化
作为字符串”在 Ionic 4 中转换为 C# 日期时间 - instagram - Instagram 自定义 URL 方案在 iOS12 中不起作用
- c++ - 初始化器列表是否只接受具有相同类型值的初始化变量?