tensor loss function 采坑

这种DIY的函数不管用,应该每次返回的都是刚构建的tonsor。

他的梯度保存不下来。

# import math
# # L2 function  mean squaril error(MAE)
# error = 0
# if pred.shape[0] == target.shape[0]:
#     for i in range(pred.shape[0]):
#         x = pred[i].item()
#         y = target[i].item()
#         e = math.pow((x-y),2)
#         error += e
#
#     meanError = error*1.0/pred.shape[0]
#     # print(meanError)
#     return torch.tensor(meanError, requires_grad = True)

这也是不行的

# L1 mean absolute error (MAE)

# error = 0
# if pred.shape[0] == target.shape[0]:
#     for i in range(pred.shape[0]):
#         x = pred[i].item()
#         y = target[i].item()
#         e = abs(x-y)
#         error += e
# meanError = error*1.0/pred.shape[0]
# return torch.tensor(meanError, requires_grad = True)

应该这样写:

return torch.mean((pred - target)**2)

通过torch.mean的方法,计算tensor

以上的笔记,记录于HW01

https://speech.ee.ntu.edu.tw/~hylee/ml/ml2021-course-data/hw/Pytorch/Pytorch_Tutorial_1.pdf

posted @ 2022-03-20 21:37  bH1pJ  阅读(39)  评论(0编辑  收藏  举报