为什么我的梯度下降算法无法正常工作?

时间:2019-05-09 04:59:49

标签: python python-3.x machine-learning linear-regression gradient-descent

我在Python中制作了梯度下降算法,但它不起作用。我的m和b值不断增加,直到遇到-inf错误或平方错误中遇到的溢出时,才停止。

   2.a Take whole data set and divide in to K-Folds.
   2.b Create a new model with the hyper parameter received after training on step 1.
   2.c Fit the newly created model on K-1 data set.
   2.d Test on Kth data set
   2.e Take average score.

我希望我的算法能为我的数据(x和y)返回最佳的m和b值,但是它不起作用。怎么了?

2 个答案:

答案 0 :(得分:2)

import numpy as np

x = np.array([2,3,4,5])
y = 0.3*x+0.6


m = np.random.randn()
b = np.random.randn()


lr = 0.001

for q in range(100000):
    ypred = m*x + b 
    error = (1./(2*len(x))) * np.sum(np.square(ypred - y)) #eq 1
    m = m - lr * np.sum((ypred - y)*x)/len(x) # eq 2 and eq 4
    b = b - lr * np.sum(ypred - y)/len(x)   # eq 3 and eq 5

print (m , b)

输出:

0.30007724168011807 0.599703981757188​​1

后面的数学

![enter image description here

使用numpy向量化操作来避免循环。

答案 1 :(得分:1)

我认为您错误地实施了该公式:

  • x - error上使用求和
  • 除以x的长度

请参见以下代码:

import numpy as np

x = np.array([2,3,4,5])

y = np.array([5,7,9,11])

m = np.random.randn()

b = np.random.randn()

error = 0

lr = 0.1
print(b, m)

for q in range(1000):
  ypred = []
  for i in range(len(x)):
    temp = m*x[i] + b
    ypred.append(temp)
    error += temp - y[i]
  m = m - np.sum(x * (ypred-y)) *lr/len(x)
  b = b - np.sum(lr * (ypred-y))/len(x)
print(b,m)

输出:

-1.198074371762264 0.058595039571115955   # initial weights
0.9997389097653074 2.0000681277214487     # Final weights