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

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay