[英]Gradient Descent in Matlab
我正在學習機器學習課程。 機器學習對我來說是一個很好的領域。 在第一次編程練習中,我在梯度體面算法中遇到了一些困難。 如果有人可以幫助我,我將不勝感激。
這是更新thetas的說明;
“你將在文件gradientDescent.m中實現梯度下降。循環結構已經為你編寫,你只需要在每次迭代中提供θ的更新。
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESENT(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.
%
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
所以我這樣做是為了同時更新這些內容;
temp0 = theta(1,1) - (alpha/m)*sum((X*theta-y));
temp1 = theta(2,1) - (alpha/m)*sum((X*theta-y).*X);
theta(1,1) = temp0;
theta(2,1) = temp1;
我運行此代碼時收到錯誤。 有人可以幫我嗎?
我已經解釋了為什么你可以使用矢量化形式:
theta = theta - (alpha/m) * (X' * (X * theta - y));
或等同物
theta = theta - (alpha/m) * ((X * theta - y)' * X)';
在這個答案 。
梯度下降算法矩陣版本的說明:
假設給出以下X,y和θ值:
這里
進一步,
h(x) = ([X] * [θ])
(mx 1我們訓練集的預測值矩陣) h(x)-y = ([X] * [θ] - [y])
(在我們的預測中mx 1個錯誤矩陣) 機器學習的整個目標是最小化預測中的錯誤。 基於上述推論,我們的誤差矩陣是mx 1
向量矩陣,如下所示:
為了計算θj的新的價值,我們必須得到乘以第 j 個訓練集X.這是特征值的所有錯誤(m行)的總和 ,把所有的值E,分別與第 j 個功能將它們相乘相應的訓練示例,並將它們全部加在一起。 這將幫助我們在得到θj的新的(希望更好)值。 對所有j或特征數重復此過程。 在矩陣形式中,這可以寫成:
[E]' x [X]
將給出一個行向量矩陣,因為E'是1 xm矩陣,X是mxn矩陣。 但我們對獲得列矩陣很感興趣,因此我們將轉換結果矩陣。 theta = theta - (alpha/m) * (X' * (X * theta - y));
這是正確的答案
temp0 = theta(1,1) - (alpha/m)*sum((X*theta-y));
temp1 = theta(2,1) - (alpha/m)*sum((X*theta-y).*X(:,2));
theta(1,1) = temp0;
theta(2,1) = temp1;
或者您可以使用以下代碼。 它更簡單。 只有兩個參數theta1和theta2。 但如果存在更多參數,那就更好了。
for i=1:2
theta(i) = theta(i) - (alpha/m)*sum((X*theta-y).*X(:,i));
end
你得到的Error using .* Matrix dimensions must agree. Error in gradientDescent (line 20) temp1 = theta(2,1) - (alpha/m)*sum((X*theta-y).*X);
Error using .* Matrix dimensions must agree. Error in gradientDescent (line 20) temp1 = theta(2,1) - (alpha/m)*sum((X*theta-y).*X);
表示.*
不起作用。 因此,在該行之前,添加以下代碼:
size(X*theta-y)
size(X)
如果你想做(X*theta-y).*X
,那么X*theta-y
和X
應該是相同的大小。 如果不是,則需要檢查算法。
這個問題有一點需要注意:
X = [ones(m, 1), data(:,1)];
所以
theta = theta - (alpha / m) * (X' * (X * theta - y));
和
temp0 = theta(1, 1) - (alpha / m) * sum((X * theta - y));
temp1 = theta(2, 1) - (alpha / m) * sum((X * theta - y) .* X(:, 2));
theta(1, 1) = temp0;
theta(2, 1) = temp1;
兩者都是對的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.