[英]simultaneously update theta0 and theta1 to calculate gradient descent in python
我正在學習coursera的機器學習課程。 有一個主題叫做梯度下降來優化成本函數。 它說同時更新 theta0 和 theta1,這樣它將最小化成本函數並達到全局最小值。
梯度下降的公式是
我如何使用 python 以編程方式執行此操作? 我正在使用 numpy array 和 pandas 從頭開始逐步理解其邏輯。
現在我只計算了成本函數
# step 1 - collect our data
data = pd.read_csv("datasets.txt", header=None)
def compute_cost_function(x, y, theta):
'''
Taking in a numpy array x, y, theta and generate the cost function
'''
m = len(y)
# formula for prediction = theta0 + theta1.x
predictions = x.dot(theta)
# formula for square error = ((theta1.x + theta0) - y)**2
square_error = (predictions - y)**2
# sum of square error function
return 1/(2*m) * np.sum(square_error)
# converts into numpy represetation of the pandas dataframe. The axes labels will be excluded
numpy_data = data.values
m = data[0].size
x = np.append(np.ones((m, 1)), numpy_data[:, 0].reshape(m, 1), axis=1)
y = numpy_data[:, 1].reshape(m, 1)
theta = np.zeros((2, 1))
compute_cost_function(x, y, theta)
def gradient_descent(x, y, theta, alpha):
'''
simultaneously update theta0 and theta1 where
theta0 = theta0 - apha * 1/m * (sum of square error)
'''
pass
我知道我必須從梯度下降中調用計算compute_cost_function
,但無法應用該公式。
這意味着您使用參數的先前值並在右側計算您需要的值。 完成后,更新參數。 要最清楚地做到這一點,請在函數內部創建一個臨時數組,將結果存儲在右側,並在完成后返回計算結果。
def gradient_descent(x, y, theta, alpha):
''' simultaneously update theta0 and theta1 where
theta0 = theta0 - apha * 1/m * (sum of square error) '''
theta_return = np.zeros((2, 1))
theta_return[0] = theta[0] - (alpha / m) * ((x.dot(theta) - y).sum())
theta_return[1] = theta[1] - (alpha / m) * (((x.dot(theta) - y)*x[:, 1][:, None]).sum())
return theta_return
我們首先聲明臨時數組,然后分別計算每個部分的參數,即截距和斜率,然后返回我們需要的。 上面代碼的好處在於我們將其矢量化。 對於截距項, x.dot(theta)
執行矩陣向量乘法,其中您有數據矩陣x
和參數向量theta
。 通過用輸出值y
減去這個結果,我們計算預測值和真實值之間所有誤差的總和,然后乘以學習率,然后除以樣本數。 我們對斜率項做了一些類似的事情,只是我們額外乘以每個輸入值而沒有偏置項。 我們還需要確保輸入值在列中,因為沿着x
的第二列切片會產生一維 NumPy 數組,而不是帶有單列的二維數組。 這允許元素乘法很好地一起玩。
還有一點要注意的是,更新參數時根本不需要計算成本。 請注意,在您的優化循環中,在更新參數時調用它會很好,這樣您就可以看到參數從數據中學習的情況。
為了使其真正矢量化並因此利用同步更新,您可以將其表述為單獨在訓練示例上的矩陣向量乘法:
def gradient_descent(x, y, theta, alpha):
''' simultaneously update theta0 and theta1 where
theta0 = theta0 - apha * 1/m * (sum of square error) '''
return theta - (alpha / m) * x.T.dot(x.dot(theta) - y)
它的作用是當我們計算x.dot(theta)
,它會計算預測值,然后我們通過減去期望值來將其組合起來。 這會產生誤差向量。 當我們預乘x
的轉置時,最終發生的是我們取誤差向量並執行向量化的求和,使得轉置矩陣x
的第一行對應於 1 的值,這意味着我們只是將所有的誤差項,它為我們提供了偏差或截距項的更新。 類似地,轉置矩陣x
的第二行額外通過x
的相應樣本值(沒有偏差項 1)對每個誤差項進行加權,並以這種方式計算總和。 結果是一個 2 x 1 的向量,當我們減去參數的先前值並由學習率和樣本數加權時,它會為我們提供最終更新。
我沒有意識到您將代碼放入迭代框架中。 在這種情況下,您需要在每次迭代時更新參數。
def gradient_descent(x, y, theta, alpha, iterations):
''' simultaneously update theta0 and theta1 where
theta0 = theta0 - apha * 1/m * (sum of square error) '''
theta_return = np.zeros((2, 1))
for i in range(iterations):
theta_return[0] = theta[0] - (alpha / m) * ((x.dot(theta) - y).sum())
theta_return[1] = theta[1] - (alpha / m) * (((x.dot(theta) - y)*x[:, 1][:, None]).sum())
theta = theta_return
return theta
theta = gradient_descent(x, y, theta, 0.01, 1000)
在每次迭代中,您更新參數然后正確設置它,以便下一次,當前更新成為以前的更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.