【解决】RuntimeError: Boolean value of Tensor with more than one value is ambiguous

在用pytorch进行损失函数计算时,报错误:

RuntimeError: Boolean value of Tensor with more than one value is ambiguous

翻译过来就是说:

具有多个值的张量的布尔值不明确 

我是这报错:

x = Variable(x_data).cuda()
y = Variable(y_data).cuda()
out = model(x)
loss = criterion(out, y)

啥意思?,你问我,我也不知道呀!、、、

 错误原因分析:

其实是,因为我损失函数调用时没有初始化,所以导致报错

其实我是初始化了,但是因为没有+(),所以报错了

criterion = nn.BCELoss

在后面补上()就OK了

参考博客:

https://stackoverflow.com/questions/52946920/bool-value-of-tensor-with-more-than-one-value-is-ambiguous-in-pytorch

猜你喜欢

转载自blog.csdn.net/qq_42792802/article/details/125921843
今日推荐