I got above error when trying to pass weighted class to BCELoss (using pytorch). As you can see below. My model is Resnet with Sigmoid. I guess the model expect one class value instead of two becouse its Sigmoid. But which one of the value percentage, I should pass. The percentage of postive value (with 1) or negative (with 0)
class_weights2=[postive/(negtive postive),negtive/(negtive postive)]
print(class_weights2)
# [0.3135668226071564, 0.6864331773928436]
class_weights=torch.tensor(class_weights2,dtype=torch.float)
lossFunc= torch.nn.BCELoss(class_weights)
and this the model:
model = torchvision.models.resnet50(pretrained=False)
model.fc = torch.nn.Sequential(
torch.nn.Linear(
in_features=2048,
out_features=1
),
torch.nn.Sigmoid()
)
CodePudding user response:
The weights passed to BCELoss are not class weights. They rescale the contribution of each element in the batch. From the docs:
a manual rescaling weight given to the loss of each batch element. If given, has to be a Tensor of size nbatch.