You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何我为吴恩达Coursera机器学习课程编写的代码无法通过提交?

Octave梯度下降代码无法通过Coursera机器学习作业提交验证

大家好,我正在Coursera上跟着吴恩达学习斯坦福大学的机器学习课程,现在卡在了线性回归的梯度下降编程作业上。按照作业要求,我用课程指定的Octave语言实现了梯度下降函数和对应的代价函数,但每次调用submit()提交时,都没法通过系统的未知测试用例校验。我已经翻了StackOverflow上相关的问题帖,还是没找到问题所在,想请各位帮忙看看代码哪里出问题了!

我的代码实现

梯度下降函数

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
    m = length(y);
    J_history = zeros(num_iters, 1);
    numJ = size(theta, 1);
    for iter = 1:num_iters
        for i = 1:m
            for j = 1:numJ
                temp = theta(j) - alpha /m * X(i, j) * (((X * theta)(i, 1)) - y(i, 1));
                theta(j) = temp
            end
            prediction = X * theta;
            J_history(iter, 1) = computeCost(X,y,theta)
        end
    end
end

代价函数

function J = computeCost(X, y, theta)
    m = length(y);
    J = 0;
    prediction = X * theta;
    error = (prediction - y).^2;
    J = 1/(2 * m) .* sum(error);
end

我原本觉得代码的逻辑是符合课堂上讲的梯度下降步骤的,但就是过不了测试,实在找不到问题点,麻烦各位帮忙排查一下!

内容的提问来源于stack exchange,提问作者user4417148

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:55:48