当前位置:网站首页>torch. logical_ And() method

torch. logical_ And() method

2022-06-12 08:51:00 Hey, it's me

torch.logical_and() Element logic for computing a given input tensor AND. Zero is treated as False, Nonzero is considered True

Official documentation : https://pytorch.org/docs/stable/generated/torch.logical_and.html#torch.logical_and
torch.logical_and(input, other, *, out=None) → Return tensor

  • input( tensor ) – Input tensor
  • other( tensor ) – Used to calculate AND Tensor
  • out ( tensor , Optional )– Output tensor
    Specific examples : Enter two tensor Variable of type input and other Sum the corresponding position , If it is a tensor internal number type , Not 0 Turn into True, 0 Turn into False

1. No out Parameter type

import torch
#  No out Parameter type 
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)   #  Not 0 True, 0 False
b = torch.tensor([4, 0, 1, 0], dtype=torch.int8)    #  Not 0 True, 0 False
print(torch.logical_and(a, b))

Output results :
 Insert picture description here

2. belt out Parameter type

Output one more suffix

import torch
#  belt out Parameter type 
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)))    #  return boolean type 
# tensor([False, False, True, False])

print(torch.logical_and(a, b, out=torch.empty(4, dtype=torch.int)))     #  return int32 type 
# tensor([0, 0, 1, 0], dtype=torch.int32)

print(torch.logical_and(a, b, out=torch.empty(4, dtype=torch.double)))  #  return double type 
# tensor([0., 0., 1., 0.], dtype=torch.float64)

Output results :
 Insert picture description here

3. Two dimensional and other examples

import torch
#  Other examples 
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)
#  Other examples 
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]])

Output results :
 Insert picture description here
For note taking
My level is limited If you have any mistakes, please correct them

原网站

版权声明
本文为[Hey, it's me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120843417716.html