当前位置:网站首页>【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
边栏推荐
- nodejs_ 01_ fs. readFile
- ABC#237 C
- . Net service governance flow limiting middleware -fireflysoft RateLimit
- Adaboost使用
- [beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
- File server migration scheme of a company
- [beauty of algebra] solution method of linear equations ax=0
- Confusing basic concepts member variables local variables global variables
- 3D reconstruction open source code summary [keep updated]
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
猜你喜欢
![Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]](/img/68/6bfa390b0bedcdbc4afba2f9bd9c0f.jpg)
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]

容易混淆的基本概念 成员变量 局部变量 全局变量

Ros-11 common visualization tools

Halcon snap, get the area and position of coins

Halcon color recognition_ fuses. hdev:classify fuses by color

Programming implementation of subscriber node of ROS learning 3 subscriber

Halcon blob analysis (ball.hdev)

C# LINQ源码分析之Count
![[code practice] [stereo matching series] Classic ad census: (5) scan line optimization](/img/54/cb1373fbe7b21c5383580e8b638a2c.jpg)
[code practice] [stereo matching series] Classic ad census: (5) scan line optimization
![3D reconstruction open source code summary [keep updated]](/img/ec/984aede7ef9e758abd52fb5ff4e144.jpg)
3D reconstruction open source code summary [keep updated]
随机推荐
Applet (use of NPM package)
scipy. misc. imread()
C [essential skills] use of configurationmanager class (use of file app.config)
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
Introduction Guide to stereo vision (7): stereo matching
Kubedm series-00-overview
Adaboost使用
Basic number theory - fast power
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
Shift operation of complement
某公司文件服务器迁移方案
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
多元线性回归(sklearn法)
520 diamond Championship 7-4 7-7 solution
容易混淆的基本概念 成员变量 局部变量 全局变量
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
Confusing basic concepts member variables local variables global variables
Rebuild my 3D world [open source] [serialization-2]
Blue Bridge Cup provincial match simulation question 9 (MST)
Oracle advanced (III) detailed explanation of data dictionary