美文网首页
UserWarning: Using a target size

UserWarning: Using a target size

作者: 小黄不头秃 | 来源:发表于2023-06-12 01:23 被阅读0次

报错内容:
UserWarning: Using a target size (torch.Size([1, 224, 224])) that is different to the input size (torch.Size([1, 1, 224, 224])) is deprecated. Please ensure they have the same size.
return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)

报错原因:是因为在相关的函数中两个矩阵的维度不一样所导致的。

解决办法:
使用torch.unsqueeze()或者torch.squeeze()进行升降维。
例如:

    net = UNet().to(device)
    net.train()
    loss_fn = nn.BCELoss()

    for i,(img,target) in enumerate(train_loader):
        img, target = img.to(device), target.to(device)
        y = net(img)
        loss = loss_fn(y, target.unsqueeze(dim=0))

相关文章

网友评论

      本文标题:UserWarning: Using a target size

      本文链接:https://www.haomeiwen.com/subject/isdwedtx.html