形状为 [64, 1] 的输出与广播形状 [64, 2] 不匹配
当尝试将加权类传递给 BCELoss (使用 pytorch)时,我遇到了上述错误。正如你在下面看到的。我的模型是带有 Sigmoid 的 Resnet。我猜该模型期望一个类值而不是两个,因为它是 Sigmoid。 但哪一项价值百分比,我应该通过。正值(1)或负值(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)
,这是模型:
model = torchvision.models.resnet50(pretrained=False)
model.fc = torch.nn.Sequential(
torch.nn.Linear(
in_features=2048,
out_features=1
),
torch.nn.Sigmoid()
)
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()
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
传递给 BCELoss 的权重不是类别权重。他们重新调整批次中每个元素的贡献。
来自 文档:
The weights passed to BCELoss are not class weights. They rescale the contribution of each element in the batch.
From the docs: