为什么我的梯度下降算法不能正常工作?
Why is my gradient descent algorithm not working correctly?
我正在尝试模仿 Andrew NG 的机器学习课程中线性回归的梯度下降算法到 Python,但由于某些原因,我的实现无法正常工作。
这是我在 Octave 中的实现,它工作正常:
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
prediction = X*theta;
margin_error = prediction - y;
gradient = 1/m * (alpha * (X' * margin_error));
theta = theta - gradient;
J_history(iter) = computeCost(X, y, theta);
end
end
但是,当我出于某种原因将其翻译成 Python 时,它没有给我准确的结果。成本似乎在上升而不是下降。
这是我在 Python 中的实现:
def gradientDescent(x, y, theta, alpha, iters):
m = len(y)
J_history = np.matrix(np.zeros((iters,1)))
for i in range(iters):
prediction = x*theta.T
margin_error = prediction - y
gradient = 1/m * (alpha * (x.T * margin_error))
theta = theta - gradient
J_history[i] = computeCost(x,y,theta)
return theta,J_history
我的代码正在编译,没有任何错误。请注意这是 theta:
theta = np.matrix(np.array([0,0]))
Alpha 和 iters 设置为:
alpha = 0.01
iters = 1000
当我 运行 它、opt_theta, cost = gradientDescent(x, y, theta, alpha, iters)
并打印出 opt_theta 时,我得到这个:
matrix([[ 2.36890383e+16, -1.40798902e+16],
[ 2.47503758e+17, -2.36890383e+16]])
我什么时候应该得到这个:
matrix([[-3.24140214, 1.1272942 ]])
我做错了什么?
编辑:
代价函数
def computeCost(x, y, theta):
# Get length of data set
m = len(y)
# We get theta transpose because we are working with a numpy array [0,0] for example
prediction = x * theta.T
J = 1/(2*m) * np.sum(np.power((prediction - y), 2))
return J
看那里:
>>> A = np.matrix([3,3,3])
>>> B = np.matrix([[1,1,1], [2,2,2]])
>>> A-B
matrix([[2, 2, 2],
[1, 1, 1]])
矩阵一起广播。
"it's because np.matrix inherits from np.array. np.matrix overrides multiplication, but not addition and subtraction"
在你的情况下,theta(1x2) 减去梯度(2x1),结果是 2x2。尝试在减去之前转置渐变。
theta = theta - gradient.T
我正在尝试模仿 Andrew NG 的机器学习课程中线性回归的梯度下降算法到 Python,但由于某些原因,我的实现无法正常工作。
这是我在 Octave 中的实现,它工作正常:
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
prediction = X*theta;
margin_error = prediction - y;
gradient = 1/m * (alpha * (X' * margin_error));
theta = theta - gradient;
J_history(iter) = computeCost(X, y, theta);
end
end
但是,当我出于某种原因将其翻译成 Python 时,它没有给我准确的结果。成本似乎在上升而不是下降。
这是我在 Python 中的实现:
def gradientDescent(x, y, theta, alpha, iters):
m = len(y)
J_history = np.matrix(np.zeros((iters,1)))
for i in range(iters):
prediction = x*theta.T
margin_error = prediction - y
gradient = 1/m * (alpha * (x.T * margin_error))
theta = theta - gradient
J_history[i] = computeCost(x,y,theta)
return theta,J_history
我的代码正在编译,没有任何错误。请注意这是 theta:
theta = np.matrix(np.array([0,0]))
Alpha 和 iters 设置为:
alpha = 0.01
iters = 1000
当我 运行 它、opt_theta, cost = gradientDescent(x, y, theta, alpha, iters)
并打印出 opt_theta 时,我得到这个:
matrix([[ 2.36890383e+16, -1.40798902e+16],
[ 2.47503758e+17, -2.36890383e+16]])
我什么时候应该得到这个:
matrix([[-3.24140214, 1.1272942 ]])
我做错了什么?
编辑:
代价函数
def computeCost(x, y, theta):
# Get length of data set
m = len(y)
# We get theta transpose because we are working with a numpy array [0,0] for example
prediction = x * theta.T
J = 1/(2*m) * np.sum(np.power((prediction - y), 2))
return J
看那里:
>>> A = np.matrix([3,3,3])
>>> B = np.matrix([[1,1,1], [2,2,2]])
>>> A-B
matrix([[2, 2, 2],
[1, 1, 1]])
矩阵一起广播。
"it's because np.matrix inherits from np.array. np.matrix overrides multiplication, but not addition and subtraction"
在你的情况下,theta(1x2) 减去梯度(2x1),结果是 2x2。尝试在减去之前转置渐变。
theta = theta - gradient.T