当前位置:网站首页>【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
边栏推荐
- Attention is all you need
- Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
- Codeworks round 639 (Div. 2) cute new problem solution
- ROS learning 4 custom message
- Nodemon installation and use
- Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
- [牛客网刷题 Day4] JZ55 二叉树的深度
- Programming implementation of ROS learning 6 -service node
- Bit operation related operations
- MPSoC QSPI Flash 升级办法
猜你喜欢
3D reconstruction open source code summary [keep updated]
Halcon snap, get the area and position of coins
Applet (use of NPM package)
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
深度学习模型与湿实验的结合,有望用于代谢通量分析
牛顿迭代法(解非线性方程)
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
C#【必备技能篇】ConfigurationManager 类的使用(文件App.config的使用)
Codeworks round 639 (Div. 2) cute new problem solution
My experience from technology to product manager
随机推荐
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
多元线性回归(梯度下降法)
My experience from technology to product manager
资源变现小程序添加折扣充值和折扣影票插件
图解网络:什么是网关负载均衡协议GLBP?
Use arm neon operation to improve memory copy speed
ROS learning 4 custom message
AdaBoost use
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
Nodejs modularization
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
Ros-10 roslaunch summary
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
12、动态链接库,dll
Shift operation of complement
Meta标签详解
Introduction Guide to stereo vision (7): stereo matching