使用 mse 实现损失函数
Implementing the loss function using mse
我正在使用 MSE 来衡量损失。在下面的代码中,我实现了 loss_mse 函数,该函数应该为具有给定 theta
的输入集计算 MSE
def loss_mse(X,y,theta):
length = len(y)
predictions = X.dot(theta)
error = (1/2*length) * np.sum(np.square(predictions - y))
return error
为了测试上面的功能,我写了如下测试用例:
X = np.array([[2.0, 1.0, 3.0], [3.0, 6.0, 2.0]])
y = np.array([1.0, 1.0])
theta = np.array([[1.0], [2.0],[1.0]])
error = loss_mse(X, y, theta)
print(error)
我必须得到 73
的答案,但我得到 584
。我不明白我哪里做错了。
你乘以 length
而不是除法。
尝试
1/(2*length) * np.sum(np.square(predictions - y))
对于给定的输入,结果为 146
,您的意思是 1/(4*length)
吗?
我正在使用 MSE 来衡量损失。在下面的代码中,我实现了 loss_mse 函数,该函数应该为具有给定 theta
的输入集计算 MSEdef loss_mse(X,y,theta):
length = len(y)
predictions = X.dot(theta)
error = (1/2*length) * np.sum(np.square(predictions - y))
return error
为了测试上面的功能,我写了如下测试用例:
X = np.array([[2.0, 1.0, 3.0], [3.0, 6.0, 2.0]])
y = np.array([1.0, 1.0])
theta = np.array([[1.0], [2.0],[1.0]])
error = loss_mse(X, y, theta)
print(error)
我必须得到 73
的答案,但我得到 584
。我不明白我哪里做错了。
你乘以 length
而不是除法。
尝试
1/(2*length) * np.sum(np.square(predictions - y))
对于给定的输入,结果为 146
,您的意思是 1/(4*length)
吗?