神经网络与深度学习(邱锡鹏)编程练习4 FNN 均方误差 numpy vs. pytorch

import numpy as np
import torch


def mean_squared_error(y, pred):
    return 0.5 * np.sum((y-pred)**2)

# numpy
y = [1., 2]
pred = [6., 7]
print(mean_squared_error(np.array(y), np.array(pred)))
# pytorch
a = torch.tensor([1., 2])
b = torch.tensor([6., 7])
loss_f = torch.nn.MSELoss()  # 使用自定义pytorch的MSE
print(loss_f(a, b))

# https://zhuanlan.zhihu.com/p/145533813

二者输出结果相同,证明自己写的函数是对的

posted on 2022-06-05 23:01  HBU_DAVID  阅读(169)  评论(0编辑  收藏  举报

导航