我正在Matlab上实现批量梯度下降.我的更新步骤有问题theta
.
theta
是两个组件(两行)的向量.
X
是包含m
行(训练样本数)和n=2
列(特征数)的矩阵.Y是m
行向量.
在更新步骤中,我需要将每个设置theta(i)
为
theta(i) = theta(i) - (alpha/m)*sum((X*theta-y).*X(:,i))
Run Code Online (Sandbox Code Playgroud)
这可以通过for
循环完成,但我无法弄清楚如何对其进行矢量化(因为该X(:,i)
术语).
有什么建议吗?
我在matlab中为多个变量做渐变下降,并且代码没有达到我用正常eq得到的预期值.即:theta = 1.0e + 05*3.4041 1.1063 -0.0665使用Normal eq.我已经实施了.
而对于GDM,我得到的结果是:theta = 1.0e + 05*2.6618 -2.6718 -0.5954我不明白为什么会这样,也许有人可以帮助我并告诉我代码中的错误在哪里.
码:
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
thetas = size(theta,1);
features = size(X,2)
mu = mean(X);
sigma = std(X);
mu_size = size(mu);
sigma_size = size(sigma);
%for all iterations
for iter = 1:num_iters
tempo = [];
result = [];
theta_temp = [];
%for all the thetas
for t = 1:thetas
%all …
Run Code Online (Sandbox Code Playgroud)