【问题标题】:Trouble Implementing Gradient Descent in Octave在 Octave 中实现梯度下降的麻烦
【发布时间】:2018-11-13 12:15:21
【问题描述】:

我一直在尝试在 Octave 中实现梯度下降。这是我到目前为止的代码:

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);

  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.
%
theta
X
y
theta' .* X
for inner = 1:length(theta)
  hypothesis = (X * theta - y)';        

  % Updating the parameters

  temp0 = theta(1) - (alpha * (1/m) * hypothesis * X(:, 1));
  temp1 = theta(2) - (alpha * (1/m) * hypothesis * X(:, 2)); 
  theta(1) = temp0;
  theta(2) = temp1;

  J_history(iter) = computeCost(X, y, theta);

  end

  end

我真的不知道这段代码出了什么问题,它可以编译并运行,但它正在自动评分,并且每次都失败。

编辑:抱歉,没有具体说明。我应该实现GD的一个步骤,而不是整个循环

编辑 2:这是完整的内容。只有 for 循环中的内容与 imo 相关。

编辑 3:两个测试用例都失败了,所以我的计算有问题。

【问题讨论】:

  • 这个问题需要minimal reproducible example。无论如何,你有数学问题的测试用例吗?您知道解决方案的一些简单域/方程?梯度下降是一个迭代过程 afaik,但我在这里没有看到循环......
  • 抱歉,没有具体说明。我应该实现 GD 的一个步骤,循环已经预编码到我正在处理的文件中。我有一个测试用例,但它有 97 个不同的数据点,所以我认为我没有空间发布所有数据。
  • 发布整个文件,包括预编码的内容。我们还能如何重现这个问题?
  • 你能创建一个应该有效而无效的简单数据集吗?
  • 已更新。你是对的卡尔菲利普

标签: machine-learning octave gradient-descent


【解决方案1】:

我认为我的问题是由于某种原因我有一个额外的 for 循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2021-04-08
    • 2019-11-15
    • 2014-03-14
    相关资源
    最近更新 更多