从 2 个张量计算 MSE

发布于 2025-01-19 00:46:56 字数 843 浏览 1 评论 0原文

我正在尝试计算两个 numpy 数组的均方误差(MSE),然后将其转换为张量。我返回的值是 36909.2148 这看起来很高……这看起来准确吗?

import numpy as np
import torch

w = torch.randn(2, 3, requires_grad=True) #total weight
b = torch.randn(2, requires_grad=True) #total bias

def model(x):
    return x @ w.t() + b

inputs = np.array([[73, 67, 43],
              [91, 88, 64],
              [87, 134, 58],
              [102, 43, 37],
              [69, 96, 70]], dtype='float32')

preds = model(torch.from_numpy(inputs))

targets = np.array([[56, 70],
                [81, 101],
                [119, 133],
                [22, 37],
                [103, 119]], dtype='float32')

targets_ = torch.from_numpy(targets)

def mse(preds, targets_):
    loss = ((targets_ - preds)**2).mean()

    return loss

# Uncomment the following lines to test
print(mse(preds, targets_))

I'm trying to calculate the mean squared error (MSE) from two numpy arrays that are later turned into tensors. I'm getting back a value of 36909.2148
This seems very high... Does this look accurate?

import numpy as np
import torch

w = torch.randn(2, 3, requires_grad=True) #total weight
b = torch.randn(2, requires_grad=True) #total bias

def model(x):
    return x @ w.t() + b

inputs = np.array([[73, 67, 43],
              [91, 88, 64],
              [87, 134, 58],
              [102, 43, 37],
              [69, 96, 70]], dtype='float32')

preds = model(torch.from_numpy(inputs))

targets = np.array([[56, 70],
                [81, 101],
                [119, 133],
                [22, 37],
                [103, 119]], dtype='float32')

targets_ = torch.from_numpy(targets)

def mse(preds, targets_):
    loss = ((targets_ - preds)**2).mean()

    return loss

# Uncomment the following lines to test
print(mse(preds, targets_))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文