当前位置:网站首页>【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
边栏推荐
- Codeworks round 638 (Div. 2) cute new problem solution
- Typescript hands-on tutorial, easy to understand
- Latex improve
- 驾驶证体检医院(114---2 挂对应的医院司机体检)
- Causes and appropriate analysis of possible errors in seq2seq code of "hands on learning in depth"
- C [essential skills] use of configurationmanager class (use of file app.config)
- Halcon blob analysis (ball.hdev)
- [daiy4] copy of JZ35 complex linked list
- Pearson correlation coefficient
- 资源变现小程序添加折扣充值和折扣影票插件
猜你喜欢

AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解

Wechat H5 official account to get openid climbing account

RT thread kernel quick start, kernel implementation and application development learning with notes

Programming implementation of ROS learning 2 publisher node

微信H5公众号获取openid爬坑记

2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition

Blogger article navigation (classified, real-time update, permanent top)

Beautiful soup parsing and extracting data

资源变现小程序添加折扣充值和折扣影票插件

The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
随机推荐
12、动态链接库,dll
One dimensional vector transpose point multiplication np dot
asp.net(c#)的货币格式化
Programming implementation of subscriber node of ROS learning 3 subscriber
Mengxin summary of LCs (longest identical subsequence) topics
ECMAScript6介绍及环境搭建
location search 属性获取登录用户名
我从技术到产品经理的几点体会
golang 基础 ——map、数组、切片 存放不同类型的数据
Blogger article navigation (classified, real-time update, permanent top)
ROS learning 1- create workspaces and function packs
多元线性回归(梯度下降法)
2309. 兼具大小写的最好英文字母
编辑器-vi、vim的使用
nodejs_ fs. writeFile
交通运输部、教育部:广泛开展水上交通安全宣传和防溺水安全提醒
某公司文件服务器迁移方案
Oracle advanced (III) detailed explanation of data dictionary
C [essential skills] use of configurationmanager class (use of file app.config)
golang 基础 —— golang 向 mysql 插入的时间数据和本地时间不一致