代码拉取完成,页面将自动刷新
"""
损失函数
# L1Loss:
# MSELoss:
# CrossEntropyLoss:
"""
import torch
import torchvision
from torch.nn import Conv2d, MaxPool2d, ReLU, Sigmoid, Linear, Flatten, Sequential, L1Loss
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from torch import nn
writer = SummaryWriter("logs/015")
dataset = torchvision.datasets.CIFAR10(root="./visionData", train=False, transform=torchvision.transforms.ToTensor(),
download=True)
dataloader = DataLoader(dataset, batch_size=64)
# 输入(预测值)
inputs = torch.tensor([1, 2, 3], dtype=torch.float32)
# 目标(真实值)
targets = torch.tensor([1, 2, 5], dtype=torch.float32)
inputs = torch.reshape(inputs, (1, 1, 1, 3))
targets = torch.reshape(targets, (1, 1, 1, 3))
# 1 L1Loss
loss = L1Loss(reduction='sum')
result = loss(inputs, targets)
print(result)
# 2 MSELoss
loss_mse = nn.MSELoss()
result_mse = loss_mse(inputs, targets)
print(result_mse)
# 3 CrossEntropyLoss
x = torch.tensor([0.1, 0.2, 0.3])
x_re = torch.reshape(x, (1, 3))
y = torch.tensor([1]) # target
loss_cross = nn.CrossEntropyLoss()
result_cross = loss_cross(x_re, y)
print(result_cross)
writer.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。