当前位置:网站首页>【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
边栏推荐
- Rebuild my 3D world [open source] [serialization-2]
- LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
- Applet (use of NPM package)
- Ecmascript6 introduction and environment construction
- Shift operation of complement
- Pearson correlation coefficient
- ABC#237 C
- 容易混淆的基本概念 成员变量 局部变量 全局变量
- 图解网络:什么是网关负载均衡协议GLBP?
- AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解
猜你喜欢
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
Solutions of ordinary differential equations (2) examples
编辑器-vi、vim的使用
Add discount recharge and discount shadow ticket plug-ins to the resource realization applet
优先级队列(堆)
Halcon: check of blob analysis_ Blister capsule detection
我从技术到产品经理的几点体会
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
Nodejs modularization
nodejs_ 01_ fs. readFile
随机推荐
File server migration scheme of a company
Oracle advanced (III) detailed explanation of data dictionary
notepad++
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
uni-app 实现全局变量
皮尔森相关系数
[Niuke brush questions day4] jz55 depth of binary tree
Solution to the problem of the 10th Programming Competition (synchronized competition) of Harbin University of technology "Colin Minglun Cup"
Halcon clolor_ pieces. Hedv: classifier_ Color recognition
牛顿迭代法(解非线性方程)
Basic number theory - fast power
Count of C # LINQ source code analysis
深度学习模型与湿实验的结合,有望用于代谢通量分析
Halcon wood texture recognition
[daiy4] copy of JZ35 complex linked list
OpenFeign
[code practice] [stereo matching series] Classic ad census: (5) scan line optimization
Numpy pit: after the addition of dimension (n, 1) and dimension (n,) array, the dimension becomes (n, n)
Configuration and startup of kubedm series-02-kubelet
Programming implementation of ROS learning 5-client node