1 Star 0 Fork 0

gisleung/py_pytorch_learn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
015 nn_loss.py 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
gisleung 提交于 2022-04-01 20:26 . 复习完毕
"""
损失函数
# 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()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gisleung/py_pytorch_learn.git
git@gitee.com:gisleung/py_pytorch_learn.git
gisleung
py_pytorch_learn
py_pytorch_learn
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385