当前位置:网站首页>【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
边栏推荐
- My experience from technology to product manager
- [code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
- 编辑器-vi、vim的使用
- Multiple linear regression (sklearn method)
- Halcon snap, get the area and position of coins
- Solution to the problem of the 10th Programming Competition (synchronized competition) of Harbin University of technology "Colin Minglun Cup"
- Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
- 我从技术到产品经理的几点体会
- Mengxin summary of LCs (longest identical subsequence) topics
- Array,Date,String 对象方法
猜你喜欢
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
Redis实现高性能的全文搜索引擎---RediSearch
ROS learning 4 custom message
IT冷知识(更新ing~)
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)
Halcon shape_ trans
Applet (subcontracting)
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
随机推荐
How many checks does kubedm series-01-preflight have
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
3D reconstruction open source code summary [keep updated]
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
容易混淆的基本概念 成员变量 局部变量 全局变量
驾驶证体检医院(114---2 挂对应的医院司机体检)
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
ABC#237 C
Beautiful soup parsing and extracting data
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
Blogger article navigation (classified, real-time update, permanent top)
2311. 小于等于 K 的最长二进制子序列
Multiple linear regression (gradient descent method)
Introduction Guide to stereo vision (3): Zhang calibration method of camera calibration [ultra detailed and worthy of collection]
uni-app 实现全局变量
优先级队列(堆)
Codeforces Round #648 (Div. 2) D. Solve The Maze
Halcon snap, get the area and position of coins
.NET服务治理之限流中间件-FireflySoft.RateLimit
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition