【发布时间】:2019-09-02 04:36:51
【问题描述】:
我正在尝试实施/解决 Andrew ng 在 coursera 上的机器学习课程中的第一个编程练习。 我无法以八度音阶实现线性梯度下降(对于一个变量)。我没有像在解决方案中那样得到相同的参数值,但我的参数朝着相同的方向发展(至少我是这么认为的)。所以我的代码中可能有一个错误。或许比我更有经验的人可以启发我。
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent 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
J_history = zeros(num_iters, 1);
theta1 = theta(1);
theta2 = theta(2);
temp0 = 0;
temp1 = 0;
h = X * theta;
for iter = 1:(num_iters)
% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCost) and gradient here.
%
temp0 = 0;
temp1 = 0;
for i=1:m
error = (h(i) - y(i));
temp0 = temp0 + error * X(i, 1));;
temp1 = temp1 + error * X(i, 2));
end
theta1 = theta1 - ((alpha/m) * temp0);
theta2 = theta2 - ((alpha/m) * temp1);
theta = [theta1;theta2];
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
对于使用 [0;0] 初始化 theta 的练习 1,我的预期结果应该是 theta1:-3.6303 和 theta2:1.1664
但是我变成了输出 theta1 是 0.095420 而 thetha2 是 0.51890
这是我用于线性梯度下降的公式。
EDIT1: 已编辑的代码。现在我得到了 theta1:
87.587
对于 theta2
979.93
【问题讨论】:
-
在内部 for 循环中,您将替换
temp0和temp1m次,然后只使用最后一个值 -
谢谢,我认为这可能是错误。我完全没有看到我这么愚蠢。非常感谢。
标签: machine-learning octave gradient-descent