当数据为空时,loss会为nan
import torch
a = torch.FloatTensor([])
b = torch.FloatTensor([])
loss_fn = torch.nn.BCELoss() # reduce=False, size_average=False)
if a.size(0)>0:
x = loss_fn(a, b).item()
print(x)
这个loss为0
import torch
a = torch.FloatTensor([])
b = torch.FloatTensor([])
loss_fn = torch.nn.BCELoss(reduction='sum') # reduce=False, size_average=False)
# if a.size(0) > 0:
x = loss_fn(a, b)
print(x.item())
这个loss也为nan
import torch
a = torch.FloatTensor([]).cuda()
b = torch.FloatTensor([]).cuda()
loss_fn = torch.nn.BCEWithLogitsLoss().cuda() # reduce=False, size_average=False)
# if a.size(0) > 0:
x = loss_fn(a, b)
print(x.item())