我正在尝试实现LMS算法,我正在阅读这篇论文:http://cs229.stanford.edu/notes/cs229-notes1.pdf我被困在theta的更新步骤(论文中的第5页)。特别是,我要做的是把两个数字相加。我给出了一个testX,它是一个nx2矩阵,还有一个向量testY,它是一个n维向量。learnTheta算法的目的是找到这样的θ,以便使用该特定θ最小化成本函数。在这种情况下,最优theta是1,1,所以算法应该使theta收敛到这个向量。不幸的是,事实并非如此。如果有任何帮助,我将不胜感激。
import numpy as np
import math
def hypothesis(x, theta):
"""
x is training set
theta is weight parameter
"""
return np.transpose(np.array(theta)).dot(np.array(x))
def costFunction(theta, x, y):
"""
x is training set => (j, 2)
y is a vector of
theta is weight parameter
"""
factor = 1 / 2
sum = 0
for i in range(0, len(x)):
sum += math.pow((hypothesis(x[i], theta) - y[i]), 2)
return factor * sum
def learnThetaSingle(theta, x, y, alpha):
return theta + alpha * (y - hypothesis(x, theta)) * x
def learnTheta(theta, x, y, alpha):
f = theta
for i in range(0, len(x)):
f = learnThetaSingle(f, x[i], y[i], alpha)
return f
testX = np.array([[1, 2],
[4, 6],
[5, 123],
[41, -14],
[-413, 0],
[0, 0],
[5, 12],
[-3, -14],
[1, 1004],
[51, 51]])
testY = np.array([3, 10, 128, 27, -413, 0, 17, -17, 1005, 102])
theta = [2, 3]
print(costFunction(theta, testX, testY))
# 2147656.0
theta = learnTheta(theta, testX, testY, 0.0001)
print(theta)
#[-29.59330648 68.71968433] , this is far from true发布于 2019-10-11 08:33:26
修复后,问题是旧算法只遍历整个数据集一次,但现在它每次更新theta中的值i from (0,len(theta))时都会遍历所有数据集。这将是O(len(Theta)*len(数据集))的复杂度。这是一个批量梯度下降(根据Andrew Ng)
https://stackoverflow.com/questions/58291173
复制相似问题