当前位置:网站首页>torch.logical_and()方法
torch.logical_and()方法
2022-06-12 08:43:00 【嘿 是我】
torch.logical_and()计算给定输入张量的元素逻辑 AND。零被视为False,非零被视为 True
官方文档说明: https://pytorch.org/docs/stable/generated/torch.logical_and.html#torch.logical_and
torch.logical_and(input, other, *, out=None) →返回张量
- input(张量) – 输入张量
- other(张量) – 用于计算 AND 的张量
- out (张量,可选)– 输出张量
具体例子: 输入两个tensor类型的变量input和other对应的位置进行求与操作,如果是张量内部数字类型,非0转化为True, 0转化为False
1.不带out参数类型
import torch
# 不带out参数类型
a = torch.tensor([True, False, True])
b = torch.tensor([True, False, False])
print(torch.logical_and(a, b))
# tensor([ True, False, False])
a = torch.tensor([0, 1, 10, 0], dtype=torch.int8) # 非0 True, 0 False
b = torch.tensor([4, 0, 1, 0], dtype=torch.int8) # 非0 True, 0 False
print(torch.logical_and(a, b))
输出结果:
2.带out参数类型
输出多一个后缀
import torch
# 带out参数类型
a = torch.tensor([0, 1, 10, 0], dtype=torch.int8)
b = torch.tensor([4, 0, 1, 0], dtype=torch.int8)
print(torch.logical_and(a, b, out=torch.empty(4, dtype=torch.bool))) # 返回boolean类型
# tensor([False, False, True, False])
print(torch.logical_and(a, b, out=torch.empty(4, dtype=torch.int))) # 返回int32类型
# tensor([0, 0, 1, 0], dtype=torch.int32)
print(torch.logical_and(a, b, out=torch.empty(4, dtype=torch.double))) # 返回double类型
# tensor([0., 0., 1., 0.], dtype=torch.float64)
输出结果:
3.二维和其他例子
import torch
# 其他例子
a = torch.tensor([0, 1, 3, 2, 2])
b = torch.tensor([1, 1, 0, 1, 2])
print(a == b) # tensor([False, True, False, False, True])
print(b != 2) # tensor([ True, True, True, True, False])
print(torch.logical_and(a==b, b != 2))
# tensor([False, True, False, False, False])
print("=" * 100)
# 其他例子
a = torch.tensor([[0, 1, 3, 2, 2],
[0, 3, 1, 0, 2]])
b = torch.tensor([[1, 1, 0, 1, 2],
[1, 1, 2, 0, 2]])
print(torch.logical_and(a, b))
# tensor([[False, True, False, True, True],
# [False, True, True, False, True]])
输出结果:
用于笔记记录
本人水平有限 如有错误欢迎指正交流
边栏推荐
- [compilation principle] understand BNF
- Regularization to limit the number of digits after the decimal point of an input number
- 2022.6.9-----leetcode.497
- Is it really expensive for enterprises to launch MES software?
- The residual pressure monitoring system ensures the smoothness of the fire evacuation passage in case of fire, and protects the safe operation of large high-rise buildings and the safety of people's l
- Shell基本语法--算数运算
- 了结非对称密钥
- Hypergeometric cumulative distribution test overlap
- Background attribute compound writing
- MES helps enterprises to transform intelligently and improve the transparency of enterprise production
猜你喜欢

【数据存储】浮点型数据在内存中的存储

报错:文件夹在另一个程序中打开无法删除怎么办

【指針進階三】實現C語言快排函數qsort&回調函數

Py & go programming skills: logic control to avoid if else

The Three Kingdoms kill the surrounding areas -------- explanation of the pig Kingdom kill problem

Error: clear the history in the search box in the website?

《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测

【 pointeur avancé Ⅲ】 mise en œuvre de la fonction de tri rapide qsort& fonction de rappel en langage C

Engineers learn music theory (II) scale and tendency
![[new planning]](/img/8e/0e15e0f3ee08002eaceea1fe8948ec.jpg)
[new planning]
随机推荐
API处理Android安全距离
2022.6.9-----leetcode. four hundred and ninety-seven
【字符集七】汉字的宽字符码和多字节码分别是多少
Method to limit the input box to only numbers
第三章 寄存器 (内存访问)
Handling abnormal data
【指针进阶三】实现C语言快排函数qsort&回调函数
Judge whether the object is empty
【字符集八】char8_t、char16_t、char32_t、wchar、char
Only use MES as a tool? Looks like you missed the most important thing
svg中viewbox图解分析
FDA审查人员称Moderna COVID疫苗对5岁以下儿童安全有效
【字符集九】gbk拷贝到Unicode会乱码?
在Tensorflow中把Tensor转换为ndarray时,循环中不断调用run或者eval函数,代码运行越来越慢!
The electrical fire detector monitors each power circuit in real time
Shell基本语法--数组
Background position position NOUN
In the era of intelligent manufacturing, how do enterprises carry out digital transformation
【 pointeur avancé Ⅲ】 mise en œuvre de la fonction de tri rapide qsort& fonction de rappel en langage C
Vscade debug TS