我认为您的代码总体上是有效的。您观察到的很可能与您的 alpha 设置有关。它似乎太高了,所以theta发散了。在某个时候,它会得到inf 或-inf,然后在下一次迭代中得到NaNs。我认识到了同样的问题。
您可以使用简单的设置来验证:
# output theta in your function
def gradient(x, y, theta, alpha, iterations):
cost_history = [0] * iterations
for i in range(iterations):
h = theta.dot(x.T) #hypothesis
#print('h:', h)
loss = h - y
#print('loss:', loss)
g = loss.dot(x) / len(y)
#print('g:', g)
theta = theta - alpha * g
print('theta:', theta)
cost_history[i] = costfn(x, y, theta)
#print(theta)
return theta, cost_history
# set up example data with a simple linear relationship
# where we can play around with different numbers of parameters
# conveniently
# with some noise
num_params= 2 # how many params do you want to estimate (up to 5)
# take some fixed params (we only take num_params of them)
real_params= [2.3, -0.1, 8.5, -1.8, 3.2]
# now generate the data for the number of parameters chosen
x_train= np.random.randint(-100, 100, size=(80, num_params))
x_noise= np.random.randint(-100, 100, size=(80, num_params)) * 0.001
y_train= (x_train + x_noise).dot(np.array(real_params[:num_params]))
theta= np.zeros(num_params)
现在尝试高学习率
theta, cost_history = gradient(x_train, y_train, theta, 0.1, 1000)
您很可能会观察到,您的 theta 值的指数会越来越高,直到最终达到 inf 或 -inf。之后,您将获得 NaN 值。
但是,如果将其设置为 0.00001 之类的低值,您会看到它收敛:
theta: [ 0.07734451 -0.00357339]
theta: [ 0.15208803 -0.007018 ]
theta: [ 0.22431803 -0.01033852]
theta: [ 0.29411905 -0.01353942]
theta: [ 0.36157275 -0.01662507]
theta: [ 0.42675808 -0.01959962]
theta: [ 0.48975132 -0.02246712]
theta: [ 0.55062617 -0.02523144]
...
theta: [ 2.29993382 -0.09981407]
theta: [ 2.29993382 -0.09981407]
theta: [ 2.29993382 -0.09981407]
theta: [ 2.29993382 -0.09981407]
非常接近真实参数2.3和-0.1。
因此,您可以尝试使用适应学习率的代码,从而使值收敛得更快并且发散的风险更低。您还可以实现类似提前停止的功能,以便在错误未发生变化或变化低于阈值时停止迭代样本。
例如您可以对您的函数进行以下修改:
def gradient(
x,
y,
theta=None,
alpha=0.1,
alpha_factor=0.1 ** (1/5),
change_threshold=1e-10,
max_iterations=500,
verbose=False):
cost_history = list()
if theta is None:
# theta was not passed explicitely
# so initialize it
theta= np.zeros(x.shape[1])
last_loss_sum= float('inf')
len_y= len(y)
for i in range(1, max_iterations+1):
h = theta.dot(x.T) #hypothesis
loss = h - y
loss_sum= np.sum(np.abs(loss))
if last_loss_sum <= loss_sum:
# the loss didn't decrease
# so decrease alpha
alpha= alpha * alpha_factor
if verbose:
print(f'pass: {i:4d} loss: {loss_sum:.8f} / alpha: {alpha}')
theta_old= theta
g= loss.dot(x) / len_y
if loss_sum <= last_loss_sum and last_loss_sum < float('inf'):
# only apply the change if the loss is
# finite to avoid infinite entries in theta
theta = theta - alpha * g
theta_change= np.sum(np.abs(theta_old - theta))
if theta_change < change_threshold:
# Maybe this seems a bit awkward, but
# the comparison of change_threshold
# takes the relationship between theta and g
# into account. Note that g will not have
# an effect if theta is orders of magnitude
# larger than g, even if g itself is large.
# (I mean if you consider g and theta elementwise)
cost_history.append(costfn(x, y, theta))
break
cost_history.append(costfn(x, y, theta))
last_loss_sum= loss_sum
return theta, cost_history
更改解决了提前停止、alpha 的自动调整和避免theta 采用无限值。您只需要在最小情况下传递X 和y,所有其他参数都获得默认值。设置verbose=True如果你想看看,损失在每个迭代中是如何减少的。