【问题标题】:Linear Regression using fminunc Implementation使用 fminunc 实现的线性回归
【发布时间】:2025-08-10 05:20:02
【问题描述】:

我正在尝试使用 Octave 中的 fminunc 仅使用一项功能实现线性回归。

这是我的代码。

x = load('/home/battousai/Downloads/ex2Data/ex2x.dat');
y = load('/home/battousai/Downloads/ex2Data/ex2y.dat');

m = length(y);
x = [ones(m , 1) , x];
theta = [0 , 0]';

X0 = [x , y , theta];

options = optimset('GradObj' , 'on' , 'MaxIter' , 1500);
[x , val] = fminunc(@computeCost , X0 , options) 

这里是成本函数,它返回梯度以及成本函数的值。

function [J , gradient] = computeCost(x , y , theta)
  m = length(y);
  J = (0.5 / m) .* (x * theta - y )' * (x * theta - y );
  gradient = (1/m) .* x' * (x * theta - y);
end

数据集的长度为50,即维度为50 x 1。我没有得到应该如何将X0 传递给fminunc 的部分。

更新的驱动程序代码:

x = load('/home/battousai/Downloads/ex2Data/ex2x.dat');
y = load('/home/battousai/Downloads/ex2Data/ex2y.dat');

m = length(y);
x = [ones(m , 1) x];
theta_initial = [0 , 0];
options = optimset('Display','iter','GradObj','on' , 'MaxIter' , 100);
[X , Cost] = fminunc(@(t)(computeCost(x , y , theta)), theta_initial , options) 

成本函数的更新代码:

function [J , gradient] = computeCost(x , y , theta)
  m = length(y);
  J = (1/(2*m)) * ((x * theta) - y )' * ((x * theta) - y) ;
  gradient = (1 / m) .* x' * ((x * theta) - y);  
end

现在我将theta 的值设为[0,0],但是当我使用正规方程时,theta 的值变成了[0.750163 , 0.063881]

【问题讨论】:

    标签: octave linear-regression


    【解决方案1】:

    来自 fminunc 的文档:

    FCN 应该接受定义未知变量的向量(数组)

    X0 确定起始猜测。

    由于您的输入是 cost 函数(即,它将您选择的参数向量与成本相关联),因此需要通过 fminunc 优化的成本函数的输入参数仅theta,因为xy(即你的观察和你的目标)被认为是问题的“给定”方面,而不是你想要优化的东西。因此,您可以声明 xy 全局并从您的函数中访问它们,如下所示:

    function [J , gradient] = computeCost(theta_0)
      global x; global y;
      % ...
    

    然后调用 fminunc 为:fminunc (@computeCost, t_0, options)

    ,将您的 computeCost 函数保留为 computeCost(x, y, theta),并将您的 fminunc 调用更改为如下内容:

    [x , val] = fminunc(@ (t) computeCost(x, y, t) , t0 , options) 
    

    更新不知道你做错了什么。这是完整的代码和运行它的 octave 会话。看起来不错。

    %% in file myscript.m
    x = load('ex2x.dat');
    y = load('ex2y.dat');
    
    m = length(y);
    x = [ones(m , 1) , x];
    theta_0 = [0 , 0]';
    
    options = optimset('GradObj' , 'on' , 'MaxIter' , 1500);
    [theta_opt, cost] = fminunc(@ (t) computeCost(x,y,t) , theta_0 , options) 
    

    %% in file computeCost.m
    function [J , gradient] = computeCost(x , y , theta)
      m = length(y);
      J = (0.5 / m) .* (x * theta - y )' * (x * theta - y );
      gradient = (1/m) .* x' * (x * theta - y);
    end
    

    %% in the octave terminal:
    >> myscript
    theta_opt =
    
       0.750163
       0.063881
    
    cost =    9.8707e-04
    

    【讨论】:

    • 那我如何使用给定的训练数据集?
    • 啊,抱歉,除了明显的语法错误,我没有研究你的问题。 fminunc 是“因”变量的最小化函数。因此,您的成本函数纯粹是 theta 的函数(并且 x 和 y 是“给定的”)。此外,X0 本身并不是“输入”,而只是一个开始的猜测。我将进行编辑以相应地显示这一点。请注意,如果您无法访问您的数据(或者最好是 minimal working example),我只能做这样的一般建议/有根据的猜测。
    • 我使用了第二个选项,现在它正在工作。但是我得到的 theta 值是 [0 , 0] 并且成本也是 0 但是当我使用梯度下降时,theta 的值结果是 [0.750163 , 0.063881]。我不明白!!
    • 1) pastebin.com/uU3phyJ2 -- 特征数据集 2) pastebin.com/wMaMNPbW -- 以及相应的结果
    • 非常感谢@tasos。我在成本函数中传递了错误的参数。 fminunc(@(t)computeCost(x,y,theta) ,initital_theta , options) theta 是这里的问题 我将代码更改为 fminunc(@(theta)computeCost(x ,y , theta) , initial_theta , options) 现在它工作正常。再次非常感谢@Tasos
    最近更新 更多