从 2 个张量计算 MSE
我正在尝试计算两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论