简体   繁体   English

Pytorch 相当于 Numpy 的 logical_and 和 kin 吗?

[英]Pytorch equivalent of Numpy's logical_and and kin?

Does Pytorch have an equivalent of Numpy's element-wise logical operators ( logical_and , logical_or , logical_not , and logical_xor )? Pytorch 是否与 Numpy 的元素逻辑运算符logical_andlogical_orlogical_notlogical_xor )等效? Calling the Numpy functions on Pytorch tensors seems to work well enough when using the CPU, even producing a Pytorch tensor as output.在使用 CPU 时,在 Pytorch 张量上调用 Numpy 函数似乎工作得很好,甚至可以生成 Pytorch 张量作为输出。 I mainly ask because I assume this would not work so well if the pytorch calculation were running in the GPU.我主要问是因为我认为如果 pytorch 计算在 GPU 中运行,这不会很好地工作。

I've looked through Pytorch's documentation index at all functions containing the string "and" and none seem relevant.我已经查看了包含字符串“and”的所有函数的Pytorch 文档索引,但似乎没有一个相关。

PyTorch supports logical operations on ByteTensor . PyTorch支持对ByteTensor逻辑操作。 You can use logical operations using & , | 您可以使用&|使用逻辑运算| , ^ , ~ operators as follows: ^~运算符如下:

>>> a = torch.ByteTensor([0, 1, 1, 0])
>>> b = torch.ByteTensor([1, 1, 0, 0])

>>> a & b  # logical and
tensor([0, 1, 0, 0], dtype=torch.uint8)

>>> a | b  # logical or
tensor([1, 1, 1, 0], dtype=torch.uint8)

>>> a ^ b  # logical xor
tensor([1, 0, 1, 0], dtype=torch.uint8)

>>> ~a  # logical not
tensor([1, 0, 0, 1], dtype=torch.uint8)

logic and:逻辑和:

a * b

logic or:逻辑或:

a + b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM