我在尝试使用梯度下降法来训练一些权重,但效果不佳。我最初设定的学习率lr
为0.01,结果成本反而急剧上升,这让我很惊讶。我只能假设这个学习率不够小,无法找到任何局部最小值。将其改为0.0000000000001后,成本得以稳定并缓慢下降。
第998次迭代 | 成本: 2444.995584
第999次迭代 | 成本: 2444.995577
第1000次迭代 | 成本: 2444.995571
最终权重: 5.66633309647e-07 | 4.32179246434e-09
然而,这些权重可能有问题,或者是我绘图的方式有误:
import numpy as npimport matplotlib.pyplot as pltdef 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 functionx = np.arange(1, 200, 2)d = np.random.uniform(-10, 10, x.size)y = .4 * x + 3 + d# number of training samplesm = y.size# add a column of ones for bias valuesit = np.ones(shape=(m, 2))it[:, 1] = xm, n = np.shape(it)# initialise weights to 0w = np.zeros(n)iter = 1000 # number of iterationslr = 0.0000000000001 # learning rate / alphatrained_w = gradient_descent(it, y, w, lr, m, iter)result = trained_w[1] * x + trained_w[0] # linear plot of our predicted functionprint("Final weights: %s | %s" % (trained_w[1], trained_w[0]))plt.plot(x, y, 'gx')plt.plot(x, result)plt.show()
回答:
你过度补偿了。这里的学习率太小,需要数十亿次迭代才能收敛。将其设置为比0.01
小,但比你现在设置的大一些的值。
我使用0.0001
的alpha值时效果很好。