nn.CrossEntropyLoss()
用于多分类,直接写标签序号就可以:0,1,2. 预测需要维度与标签长度一致。 import torch import torch.nn as nn import math criterion = nn.CrossEntropyLoss() output = torch.randn(3, 5, requires_grad=True) label = torch.empty(3, dtype=torch.long).random_(5) loss = criterion(output, label) print(网络输出为3个5类:) print(output) print(要计算loss
下载地址
用户评论