多分类数据不均衡:交叉熵CrossEntropyLoss加入权重

criterion = nn.CrossEntropyLoss
(
	weight=torch.from_numpy(np.array([10.0,5.0,1.0,5.0,10.0])).float(),
	size_average=True
)
criterion.cuda()

不加第二句会报错:

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

weight(Tensor, optional) - 每个类别class 的权重. 默认为值为 1 的 Tensor.
size_average(bool, optional) – 默认为 True.
— size_average=True, 则 losses 在 minibatch 结合 weight 求平均average.
— size_average=False, 则losses 在 minibatch 求相加和sum.

参考:
https://blog.csdn.net/zziahgf/article/details/80196376
https://blog.csdn.net/yuekangwei/article/details/111491256

你可能感兴趣的:(NLP,深度学习,pytorch,分类)