2016-05-14 75 views
0

使用梯度的矢量化版本在描述: gradient descent seems to fail梯度下降不更新2θ值

theta = theta - (alpha/m * (X * theta-y)' * X)'; 

的2θ值不被更新,所以無論初始THETA值 這是後設置的值運行梯度下降:

例1:

m = 1 
X = [1] 
y = [0] 
theta = 2 
theta = theta - (alpha/m .* (X .* theta-y)' * X)' 

theta = 

    2.0000 

例2:

m = 1 
X = [1;1;1] 
y = [1;0;1] 
theta = [1;2;3] 
theta = theta - (alpha/m .* (X .* theta-y)' * X)' 

theta = 

    1.0000 
    2.0000 
    3.0000 

theta = theta - (alpha/m * (X * theta-y)' * X)';梯度下降的正確向量化實現?

回答

0

theta = theta - (alpha/m * (X * theta-y)' * X)';確實是梯度下降的正確向量化實現。

你完全忘了設置學習率,alpha

設置alpha = 0.01後,你的代碼就變成了:

m = 1    # number of training examples 
X = [1;1;1] 
y = [1;0;1] 
theta = [1;2;3] 
alpha = 0.01 
theta = theta - (alpha/m .* (X .* theta-y)' * X)' 
theta = 

    0.96000 
    1.96000 
    2.96000