作业帮 > 综合 > 作业

用matlab实现梯度下降算法(gradient descent).

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/27 13:28:05
用matlab实现梯度下降算法(gradient descent).
看的斯坦福的machine learning 课程,作业任务是用matlab实现梯度下降算法,theta(j)=theta-alpha*(theta(j)'s derivative of costFunction).实现之后,costFunction应该是逐渐收敛.
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESENT(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
J_history = zeros(num_iters, 1);
for iter = 1:num_iters,
%
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
再问: 哥们,你也学这个么?可以交流下不? 十分感谢。。我qq,345360234.。可以的话加我。。谢谢