首页 > 其他分享 >CrossEntropyLoss

CrossEntropyLoss

时间:2023-10-10 23:23:49浏览次数:28  
标签:tensor torch CrossEntropyLoss 64 import 224

输入x多一个维度,输出差距怎么那么大?

import torch
import numpy as np

x = torch.randn((64,224,224))

y = torch.rand((64,224,224))

y = (y > 0.5).float()
y = torch.tensor(y)
fun = torch.nn.CrossEntropyLoss()
print(fun(x,y))   //输出:tensor(661.5663)
import torch
import numpy as np

x = torch.randn((64,2,224,224))

y = torch.rand((64,224,224))

y = (y > 0.5).float()
y = torch.tensor(y).to(torch.long)
fun = torch.nn.CrossEntropyLoss()
print(fun(x,y))  //输出:tensor(0.9030)

标签:tensor,torch,CrossEntropyLoss,64,import,224
From: https://www.cnblogs.com/gitLab/p/17756013.html

相关文章

  • 深度学习之PyTorch实战(5)——对CrossEntropyLoss损失函数的理解与学习
     其实这个笔记起源于一个报错,报错内容也很简单,希望传入一个三维的tensor,但是得到了一个四维。RuntimeError:onlybatchesofspatialtargetssupported(3Dtensors)butgottargetsofdimension:4查看代码报错点,是出现在pytorch计算交叉熵损失的代码。其实在......
  • torch.nn.CrossEntropyLoss
    文章目录​​交叉熵损失函数`torch.nn.CrossEntropyLoss`​​​​F.cross_entropy​​​​F.nll_loss​​交叉熵损失函数​​torch.nn.CrossEntropyLoss​​weight(Tensor......