我正在遵循此 YouTube 视频 (https://www.youtube.com/watch?v=lCOHri09YmM) 中的教程,但我收到错误“在减法 coeff = coeff - der 中遇到无效值”,并且那么我的系数的最终值为[nan nan nan]。下面是我的代码
#Gradient Descent Algorithm
learning_rate = 0.01 #step size
epochs = 2000
N = y.size
np.random.seed(123)
coeff = np.random.rand(3)
print("Initial values of coefficients : ", coeff)
#Algorithm
def gD(x, y, coeff, epochs, learning_rate):
past_costs = []
past_coeff = [coeff]
for i in range(epochs):
prediction = np.dot(x, coeff)
error = prediction - y
cost = 1/(2*N) * np.dot(error.T,error)
past_costs.append(cost)
der = (1/N) * learning_rate * np.dot(x.T, error)
coeff = coeff - der
past_coeff.append(coeff)
return past_coeff, past_costs
past_coeff, past_costs = gD(x, y, coeff, epochs, learning_rate)
coeff = past_coeff[-1]
print("Find values of coefficients : ",coeff)
我试图看到系数的最终值。
这是因为你的系数趋于无穷大,然后经过一次计算给出 Nan 值。如果将学习率降低到 1e-6 并将纪元值降低到 300,您将看到系数结果。