当前位置:网站首页>【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
2022-07-05 09:01:00 【aelum】
Case 1
将含有两个及以上的布尔值的张量用在了 if
判断条件里:
a = torch.tensor([True, False])
if a:
pass
出现这种错误的可能原因之一是想判断 a
不为 None
,此时应改为如下语句
if a is not None:
需要注意的是,如果 a
只含一个布尔值,则判断不会出现错误:
a = torch.tensor([True])
if a:
print(1)
# 1
Case 2
使用交叉熵损失时没有先实例化:
inputs = torch.randn(6, 4)
target = torch.randint(4, (6, ))
loss = nn.CrossEntropyLoss(inputs, target)
应先实例化再计算损失:
criterion = nn.CrossEntropyLoss()
loss = criterion(inputs, target)
Case 3
对含有两个及以上的布尔值张量执行了 or
、and
、not
这样的操作:
a = torch.tensor([True, False])
b = torch.tensor([False, True])
""" 以下三种操作都会报错 """
print(a or b)
print(a and b)
print(not a)
需要注意的是,如果 a
和 b
都只含一个布尔值,则不会出现错误:
a = torch.tensor([True])
b = torch.tensor([False])
print(a or b)
# tensor([True])
print(a and b)
# tensor([False])
print(not a)
# False
边栏推荐
- Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
- [牛客网刷题 Day4] JZ55 二叉树的深度
- uni-app 实现全局变量
- 2309. 兼具大小写的最好英文字母
- 特征工程
- Nodemon installation and use
- File server migration scheme of a company
- Applet (subcontracting)
- kubeadm系列-00-overview
- Bit operation related operations
猜你喜欢
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
Applet (subcontracting)
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
Attention is all you need
AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解
Programming implementation of ROS learning 2 publisher node
My university
Solutions of ordinary differential equations (2) examples
Nodejs modularization
Halcon affine transformations to regions
随机推荐
Task failed task_ 1641530057069_ 0002_ m_ 000000
多元线性回归(sklearn法)
ROS learning 4 custom message
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
asp. Net (c)
Halcon Chinese character recognition
TF coordinate transformation of common components of ros-9 ROS
Codeworks round 639 (Div. 2) cute new problem solution
Jenkins Pipeline 方法(函数)定义及调用
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
深度学习模型与湿实验的结合,有望用于代谢通量分析
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
Halcon affine transformations to regions
Introduction Guide to stereo vision (1): coordinate system and camera parameters
Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)
Halcon blob analysis (ball.hdev)
Rebuild my 3D world [open source] [serialization-1]
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
Illustrated network: what is gateway load balancing protocol GLBP?