【问题标题】:Implementing the loss function using mse使用 mse 实现损失函数
【发布时间】:2021-01-24 00:43:12
【问题描述】:

我正在使用 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。我不明白我在哪里做错了。

【问题讨论】:

    标签: python python-3.x machine-learning data-science naivebayes


    【解决方案1】:

    您是乘以length,而不是除以。

    试试

    1/(2*length) * np.sum(np.square(predictions - y))
    

    对于给定的输入,这将导致 146,您的意思是否可能是 1/(4*length)

    【讨论】:

    • 我刚刚使用了教科书上给出的公式,得到了 146,但根据提供的测试用例,我需要得到 73。我不明白为什么我们需要这样做?
    • 通用的 MSE 公式是 (1/length) * np.sum(np.square(predictions - y)),所以我不确定他们到底在寻找什么
    猜你喜欢
    • 2021-04-23
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多