gpt4 book ai didi

Python梯度下降-成本不断增加

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:43 24 4
gpt4 key购买 nike

我正在尝试在 python 中实现梯度下降,但我的损失/成本随着每次迭代而不断增加。

我看到一些人发布了关于此的帖子,并在这里看到了一个答案:gradient descent using python and numpy

我相信我的实现是相似的,但看不出我做错了什么以获得爆炸式的成本值:

Iteration: 1 | Cost: 697361.660000
Iteration: 2 | Cost: 42325117406694536.000000
Iteration: 3 | Cost: 2582619233752172973298548736.000000
Iteration: 4 | Cost: 157587870187822131053636619678439702528.000000
Iteration: 5 | Cost: 9615794890267613993157742129590663647488278265856.000000

我正在网上找到的数据集(洛杉矶心脏数据)上对此进行测试:http://www.umass.edu/statdata/statdata/stat-corr.html

导入代码:

dataset = np.genfromtxt('heart.csv', delimiter=",")

x = dataset[:]
x = np.insert(x,0,1,axis=1) # Add 1's for bias
y = dataset[:,6]
y = np.reshape(y, (y.shape[0],1))

梯度下降:

def gradientDescent(weights, X, Y, iterations = 1000, alpha = 0.01):
theta = weights
m = Y.shape[0]
cost_history = []

for i in xrange(iterations):
residuals, cost = calculateCost(theta, X, Y)
gradient = (float(1)/m) * np.dot(residuals.T, X).T
theta = theta - (alpha * gradient)

# Store the cost for this iteration
cost_history.append(cost)
print "Iteration: %d | Cost: %f" % (i+1, cost)

计算成本:

def calculateCost(weights, X, Y):
m = Y.shape[0]
residuals = h(weights, X) - Y
squared_error = np.dot(residuals.T, residuals)

return residuals, float(1)/(2*m) * squared_error

计算假设:

def h(weights, X):   
return np.dot(X, weights)

实际运行它:

gradientDescent(np.ones((x.shape[1],1)), x, y, 5)

最佳答案

假设您对梯度的推导是正确的,您正在使用:=-,并且您应该使用:-=。您不是更新 theta,而是将其重新分配给 - (alpha * gradient)

编辑(在代码中修复了上述问题之后):

我在我认为正确的数据集上运行了代码,并且能够通过设置 alpha=1e-7 获得行为成本。如果您运行 1e6 迭代,您应该会看到它收敛。此数据集上的这种方法似乎对学习率非常敏感。

关于Python梯度下降-成本不断增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39771075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com