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()
)
x = torch.tensor([[1], [2], [3]])
is of shapetorch.Size([3, 1])
. If you usetorch.squeeze()
with argumentdim=-1
you will get a tensor[1, 2, 3]
of shapetorch.Size([3])
which has removed the last dimension which had no data. $\endgroup$