0
$\begingroup$

I'm trying to build a Resnet model with Sigmoid with BCELoss lose. Since my data is imbalance, I guess I need to use "class weights" as an argument for the "BCELoss". But which weight I should pass, is it for the positive (with 1) or negative (with 0). Of course, when I tried to pass 2 weight, for Sigmoid model, I got above error: output with shape [64, 1] doesn't match the broadcast shape [64, 2].

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()
)
   
$\endgroup$
1
  • 1
    $\begingroup$ Can you print and share the two tensors that have a shape mismatch? Sometimes you end up with a 2D tensor that is actually a 1D tensor in disguise. For example x = torch.tensor([[1], [2], [3]]) is of shape torch.Size([3, 1]). If you use torch.squeeze() with argument dim=-1 you will get a tensor [1, 2, 3] of shape torch.Size([3]) which has removed the last dimension which had no data. $\endgroup$
    – Lars
    Mar 14, 2022 at 7:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.