DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

heaviside and Identity in PyTorch

Buy Me a Coffee

*Memos:

heaviside() can get the 0D or more D tensor of the zero or more values computed by Heaviside step function from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • heaviside() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or bool).
  • The 2nd argument with torch or the 1st argument with a tensor is values(Required-Type:tensor of int, float or bool). Image description
import torch from torch import nn my_tensor = torch.tensor([8, -3, 0, 1, 5, -2, -1, 4]) torch.heaviside(input=my_tensor, values=torch.tensor(0)) my_tensor.heaviside(values=torch.tensor(0)) # tensor([1, 0, 0, 1, 1, 0, 0, 1])  torch.heaviside(input=my_tensor, values=torch.tensor([0, 1, 2, 3, 4, 5, 6, 7])) # tensor([1, 0, 2, 1, 1, 0, 0, 1])  my_tensor = torch.tensor([[8, -3, 0, 1], [5, 0, -1, 4]]) torch.heaviside(input=my_tensor, values=torch.tensor(0)) # tensor([[1, 0, 0, 1], # [1, 0, 0, 1]])  torch.heaviside(input=my_tensor, values=torch.tensor([[0, 1, 2, 3], [4, 5, 6, 7]])) # tensor([[1, 0, 2, 1], # [1, 5, 0, 1]])  my_tensor = torch.tensor([[[8, -3], [0, 1]], [[5, 0], [-1, 4]]]) torch.heaviside(input=my_tensor, values=torch.tensor(0)) # tensor([[[1, 0], [0, 1]], # [[1, 0], [0, 1]]])  torch.heaviside(input=my_tensor, values=torch.tensor([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])) # tensor([[[1, 0], [2, 1]], # [[1, 5], [0, 1]]])  my_tensor = torch.tensor([[[8., -3.], [0., 1.]], [[5., 0.], [-1., 4.]]]) torch.heaviside(input=my_tensor, values=torch.tensor([[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]])) # tensor([[[1., 0.], [2., 1.]], # [[1., 5.], [0., 1.]]])  my_tensor = torch.tensor([[[True, False], [True, False]], [[False, True], [False, True]]]) torch.heaviside(input=my_tensor, values=torch.tensor([[[True, False], [True, False]], [[False, True], [False, True]]])) # tensor([[[True, False], [True, False]], # [[False, True], [False, True]]]) 
Enter fullscreen mode Exit fullscreen mode

Identity() can just get the same tensor as the input tensor which is the 0D or more D tensor of zero or more elements as shown below:
*Memos:

  • For initialization, you can set 0 or more arguments but there is no influence.
  • The 1st argument is input(Required-Type:tensor of int or float).

Image description

import torch from torch import nn my_tensor = torch.tensor([8, -3, 0, 1, 5, -2, -1, 4]) identity = nn.Identity() identity(input=my_tensor) # tensor([8, -3, 0, 1, 5, -2, -1, 4])  identity # Identity()  identity = nn.Identity(num1=3, num2=5) identity(input=my_tensor) # tensor([8, -3, 0, 1, 5, -2, -1, 4])  my_tensor = torch.tensor([[8, -3, 0, 1], [5, -2, -1, 4]]) identity = nn.Identity() identity(input=my_tensor) # tensor([[8, -3, 0, 1], # [5, -2, -1, 4]])  my_tensor = torch.tensor([[[8, -3], [0, 1]], [[5, -2], [-1, 4]]]) identity = nn.Identity() identity(input=my_tensor) # tensor([[[8, -3], [0, 1]], # [[5, -2], [-1, 4]]])  my_tensor = torch.tensor([[[8., -3.], [0., 1.]], [[5., -2.], [-1., 4.]]]) identity = nn.Identity() identity(input=my_tensor) # tensor([[[8., -3.], [0., 1.]], # [[5., -2.], [-1., 4.]]]) 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)