2
我正在嘗試使用梯度下降法來訓練一些權重,但是我沒有獲得太多成功。 我開始時的學習率lr
爲0.01,我的成本實際上是飛速上升,這讓我感到驚訝。我只能假設它不足以找到任何當地的最低標準。將其更改爲0.0000000000001允許其穩定並緩慢下降。使用批梯度下降訓練單個線性神經元進行迴歸
Iteration 998 |費用:2444.995584
Iteration 999 |費用:2444.995577
Iteration 1000 |費用:2444.995571
最終權重:5.66633309647e-07 | 4.32179246434e-09
然而無論是東西是錯誤的論文重物或我如何繪製出來:
import numpy as np
import matplotlib.pyplot as plt
def gradient_descent(x, y, w, lr, m, iter):
xTrans = x.transpose()
for i in range(iter):
prediction = np.dot(x, w)
loss = prediction - y
cost = np.sum(loss ** 2)/m
print("Iteration %d | Cost: %f" % (i + 1, cost))
gradient = np.dot(xTrans, loss)/m # avg gradient
w = w - lr * gradient # update the weight vector
return w
# generate data from uniform distribution -10. +10 and linear function
x = np.arange(1, 200, 2)
d = np.random.uniform(-10, 10, x.size)
y = .4 * x + 3 + d
# number of training samples
m = y.size
# add a column of ones for bias values
it = np.ones(shape=(m, 2))
it[:, 1] = x
m, n = np.shape(it)
# initialise weights to 0
w = np.zeros(n)
iter = 1000 # number of iterations
lr = 0.0000000000001 # learning rate/alpha
trained_w = gradient_descent(it, y, w, lr, m, iter)
result = trained_w[1] * x + trained_w[0] # linear plot of our predicted function
print("Final weights: %s | %s" % (trained_w[1], trained_w[0]))
plt.plot(x, y, 'gx')
plt.plot(x, result)
plt.show()
謝謝,就這麼簡單。我應該慢慢減少它,而不是跳到這樣一個可笑的小數目。當我考慮它的時候是有意義的,它正在逐漸降低梯度,所以它不能到達任何地方。 –
沒問題!這是一個容易犯的錯誤。 – senderle