2016-08-13 103 views
0

我想實現一個先前用matlab中的numpy編寫的梯度下降算法,但是我得到了一組類似但不同的結果。在numpy與matlab中的不同結果

這裏的MATLAB代碼

function [theta] = gradientDescentMulti(X, y, theta, alpha, num_iters) 

m = length(y); 
num_features = size(X,2); 
for iter = 1:num_iters; 
    temp_theta = theta; 
    for i = 1:num_features 
     temp_theta(i) = theta(i)-((alpha/m)*(X * theta - y)'*X(:,i)); 
    end 
    theta = temp_theta; 
end 


end 

和我的Python版本

def gradient_descent(X,y, alpha, trials): 

    m = X.shape[0] 
    n = X.shape[1] 
    theta = np.zeros((n, 1)) 

    for i in range(trials): 

     temp_theta = theta 
     for p in range(n): 
      thetaX = np.dot(X, theta) 
      tMinY = thetaX-y 
      temp_theta[p] = temp_theta[p]-(alpha/m)*np.dot(tMinY.T, X[:,p:p+1]) 

     theta = temp_theta 

    return theta 

測試用例和MATLAB結果

X = [1 2 1 3; 1 7 1 9; 1 1 8 1; 1 3 7 4] 
y = [2 ; 5 ; 5 ; 6]; 
[theta] = gradientDescentMulti(X, y, zeros(4,1), 0.01, 1); 

theta = 

    0.0450 
    0.1550 
    0.2225 
    0.2000 

測試用例,並導致蟒蛇

test_X = np.array([[1,2,1,3],[1,7,1,9],[1,1,8,1],[1,3,7,4]]) 
test_y = np.array([[2], [5], [5], [6]]) 
theta, cost = gradient_descent(test_X, test_y, 0.01, 1) 
print theta 
>>[[ 0.045  ] 
    [ 0.1535375 ] 
    [ 0.20600144] 
    [ 0.14189214]] 
+0

@Kartik「MATLAB結果可能是錯誤的」真的不是一個有用的建議,沒有詳細的理由。 – dbliss

+0

我的評論被誤解了。我與您分享我的經驗,並且我建議您可以使用其他軟件來解決此問題。 (我知道你可能無法訪問另一個軟件。)當我嘗試一個簡單的直方圖時,解釋爲什麼MATLAB結果是錯誤的,這是我當時沒有研究或弄清楚的事情。我把它歸咎於MATLAB的封閉源代碼本質,並假定某些東西在測試中滑落,並繼續使用Python,這讓我感覺更「在家」使用。 – Kartik

回答

7

這條線在你的Python:

temp_theta = theta 

沒有做什麼,你認爲它。它不復制theta並將其「分配」給「變量」temp_theta - 它只是說「temp_theta現在是當前由theta命名的對象的新名稱」。

所以當你修改temp_theta這裏:

 temp_theta[p] = temp_theta[p]-(alpha/m)*np.dot(tMinY.T, X[:,p:p+1]) 

你實際上修改theta - 因爲那裏有兩個名字只有一個陣列,現在。

如果改爲寫

temp_theta = theta.copy() 

你就會得到這樣

(3.5) [email protected]:~/coding$ python peter.py 
[[ 0.045 ] 
[ 0.155 ] 
[ 0.2225] 
[ 0.2 ]] 

符合你Matlab的結果。

相關問題