LMS随机梯度下降的结果不佳

时间:2017-05-30 18:47:19

标签: python numpy linear-regression least-squares gradient-descent

我正在尝试从a previous question调整批量梯度下降算法来做随机梯度下降,我的成本似乎与最小值相差很远(在这个例子中,大约在1750年,当最小值出现时) 1450)。看起来,一旦达到该值,它就会在那里开始振荡。我还尝试每range(0, x.shape[0]-1)l洗牌,但它没有任何区别。我预计振荡会在最佳值附近出现,但这似乎太过分了,所以我认为一定有错误。

import numpy as np

y = np.asfarray([[400], [330], [369], [232], [540]])
x = np.asfarray([[2104,3], [1600,3], [2400,3], [1416,2], [3000,4]])
x = np.concatenate((np.ones((5,1)), x), axis=1)
theta = np.asfarray([[0], [.5], [.5]])

fscale = np.sum(x, axis=0)
x /= fscale

alpha = .1

for l in range(1,100000):
    for i in range(0, x.shape[0]-1):
        h = np.dot(x, theta)
        gradient = ((h[i:i+1] - y[i:i+1]) * x[i:i+1]).T
        theta -= alpha * gradient
        print ((h - y)**2).sum(), theta.squeeze() / fscale

0 个答案:

没有答案
相关问题