对数下降曲线在我想为代表的对数下降曲线上运行梯度下降:梯度下降在Python
Y = Y0 - A * LN(B + X)。
我的这个例子Y0:800
我试图做到这一点使用的偏导数相对于A和B,但在这种明显减少了误差平方,它不收敛。我知道这不是矢量化的,我可能会完全采用错误的方法。我是否犯了一个简单的错误,或完全解决这个问题?
import numpy as np
# constants my gradient descent model should find:
a = 4
b = 4
# function to fit on!
def function(x, a, b):
y0 = 800
return y0 - a * np.log(b + x)
# Generates data
def gen_data(numpoints):
a = 4
b = 4
x = np.array(range(0, numpoints))
y = function(x, a, b)
return x, y
x, y = gen_data(600)
def grad_model(x, y, iterations):
converged = False
# length of dataset
m = len(x)
# guess a , b
theta = [0.1, 0.1]
alpha = 0.001
# initial error
e = np.sum((np.square(function(x, theta[0], theta[1])) - y))
for iteration in range(iterations):
hypothesis = function(x, theta[0], theta[1])
loss = hypothesis - y
# compute partial deritaves to find slope to "fall" into
theta0_grad = (np.mean(np.sum(-np.log(x + y))))/(m)
theta1_grad = (np.mean((((np.log(theta[1] + x))/theta[0]) - (x*(np.log(theta[1] + x))/theta[0]))))/(2*m)
theta0 = theta[0] - (alpha * theta0_grad)
theta1 = theta[1] - (alpha * theta1_grad)
theta[1] = theta1
theta[0] = theta0
new_e = np.sum(np.square((function(x, theta[0], theta[1])) - y))
if new_e > e:
print "AHHHH!"
print "Iteration: "+ str(iteration)
break
print theta
return theta[0], theta[1]
是的,每当我通过标准线性渐变下降并且不太清楚如何解决这个问题时,我遇到了麻烦。 –
还没有真正读过代码,但是,它是什么意思,它不会收敛?错误是否越来越大,因此它是分歧的?或者它收敛太久了?假设你确实编码了衍生物,那可能就是你选择了错误的“alpha”,或者梯度的方向有符号翻转('+'而不是'-')。 –
如果我的错误分歧,我在代码中放了一个休息时间。我相信我的theta [0](a)变量的偏导数是正确的,但不是我的theta [1](b)变量。它似乎正确收敛,但只有theta [0]。 –