RuntimeError: Boolean value of Tensor with more than one value is ambiguous
of is with value One more than Tensor
2023-09-27 14:19:49 时间
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
相关文章
- terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr
- Leetcode: Substring with Concatenation of All Words
- POJ 3083:Children of the Candy Corn(DFS+BFS)
- Neor Profile SQL is the solution for developers wanting to profile the MySQL queries. This software allows the tracking of all queries to MySQL server from your application or website.
- Ajax request returns 200 OK, but an error event is fired instead of success
- What is the purpose of FormsAuthenticationTicket isPersistent property?
- Benefits of Cold Showers: 7 Reasons Why Taking Cool Showers Is Good For Your Health
- Google Earth Engine(GEE)——Output of image computation is too large (29 bands for 828828 pixels = 96.
- 异常Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- The Future Of the Software Development
- this compilation unit is not on the build path of a java project
- Version 1.8.0_201 of the JVM is not suitable for this product. Version: 11 or greater is required.
- MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决方法
- Input and Output-The input is all the sources of action for your app
- Swift protocol extension method is called instead of method implemented in subclass
- 解决【npm ERR! Unexpected end of JSON input while parsing near '...sh_time":141072930277'】方案
- 再探 游戏 《 2048 》 —— AI方法—— 缘起、缘灭(4) —— state-of-the-art
- 安卓开发 报错 错误:This version of android studio is incompatible with the gradle version used. 的解决
- ORA-12514 TNS:LISTENER DOES NOT CURRENTLY KNOW OF SERVICE REQUESTED IN CONNE
- 【AD错误】原理图编译后出现“Extra Pin…in Normal of part ”警告的解决方法
- Cadence错误之 Part xxx is out of date with respect to the design cache.
- Effective C++ Item 38 通过复合塑模出 has-a 或 is-implemented-in-terms-of
- 解决eclipse中出现Resource is out of sync with the file system问题
- [LeetCode] 813. Largest Sum of Averages 最大的平均数之和
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing--转
- 项目报错This is often the result of over-eager type matching