DEV Community

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

Posted on • Updated on

expm1 and sigmoid in PyTorch

Buy Me a Coffee

*Memos:

expm1() can get the 0D or more D tensor of the zero or more elements by ex - 1 from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • expm1() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • *A float tensor is returned unless an input tensor is complex tensor.
  • torch.expm1() is the alias of torch.special.expm1().
  • The formula is y = ex - 1.
  • The graph in Desmos: Image description
import torch

my_tensor = torch.tensor([-2., -1., 0., 1., 2., 3.])

torch.expm1(input=my_tensor)
my_tensor.expm1()
# tensor([-0.8647, -0.6321, 0.0000, 1.7183, 6.3891, 19.0855])

my_tensor = torch.tensor([[-2., -1., 0.],
                          [1., 2., 3.]])
torch.expm1(input=my_tensor)
# tensor([[-0.8647, -0.6321, 0.0000],
#         [1.7183, 6.3891, 19.0855]])

my_tensor = torch.tensor([[-2, -1, 0],
                          [1, 2, 3]])
torch.expm1(input=my_tensor)
# tensor([[-0.8647, -0.6321, 0.0000],
#         [1.7183, 6.3891, 19.0855]])

my_tensor = torch.tensor([[-2.+0.j, -1.+0.j, 0.+0.j],
                          [1.+0.j, 2.+0.j, 3.+0.j]])
torch.expm1(input=my_tensor)
# tensor([[-0.8647+0.j, -0.6321+0.j, 0.0000+0.j],
#         [1.7183+0.j, 6.3891+0.j, 19.0855+0.j]])

my_tensor = torch.tensor([[True, False, True],
                          [False, True, False]])
torch.expm1(input=my_tensor)
# tensor([[1.7183, 0.0000, 1.7183],
#         [0.0000, 1.7183, 0.0000]])
Enter fullscreen mode Exit fullscreen mode

sigmoid() can get the 0D or more D tensor of the zero or more elements by Sigmoid function from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • sigmoid() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • *A float tensor is returned unless an input tensor is complex tensor.
  • torch.sigmoid() is the alias of torch.special.expit().
  • You can also use torch.nn.Sigmoid().
  • The formula is y = 1 / (1 + e-x).
  • The graph in Desmos: Image description
import torch

my_tensor = torch.tensor([-2., -1., 0., 1., 2., 3.])

torch.sigmoid(input=my_tensor)
my_tensor.sigmoid()
# tensor([0.1192, 0.2689, 0.5000, 0.7311, 0.8808, 0.9526])

my_tensor = torch.tensor([[-2., -1., 0.],
                          [1., 2., 3.]])
torch.sigmoid(input=my_tensor)
# tensor([[0.1192, 0.2689, 0.5000],
#         [0.7311, 0.8808, 0.9526]])

my_tensor = torch.tensor([[-2, -1, 0],
                          [1, 2, 3]])
torch.sigmoid(input=my_tensor)
# tensor([[0.1192, 0.2689, 0.5000],
#         [0.7311, 0.8808, 0.9526]])

my_tensor = torch.tensor([[-2.+0.j, -1.+0.j, 0.+0.j],
                          [1.+0.j, 2.+0.j, 3.+0.j]])
torch.sigmoid(input=my_tensor)
# tensor([[0.1192+0.j, 0.2689+0.j, 0.5000+0.j],
#         [0.7311+0.j, 0.8808+0.j, 0.9526+0.j]])

my_tensor = torch.tensor([[True, False, True],
                          [False, True, False]])
torch.sigmoid(input=my_tensor)
# tensor([[0.7311, 0.5000, 0.7311],
#         [0.5000, 0.7311, 0.5000]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)