Wednesday, September 25, 2024
Google search engine
HomeLanguagesHow to Compute the Logistic Sigmoid Function of Tensor Elements in PyTorch

How to Compute the Logistic Sigmoid Function of Tensor Elements in PyTorch

In this article, we will see how to compute the logistic sigmoid function of Tensor Elements in PyTorch.

The torch.special.expit() & torch.sigmoid() methods are logistic functions in a tensor. torch.sigmoid() is an alias of torch.special.expit() method.  So, these methods will take the torch tensor as input and compute the logistic function element-wise of the tensor.

Syntax:

torch.special.expit(tensor)
torch.sigmoid(tensor)

Parameter:

  • tensor is the input tensor

Return: Return the logistic function of elements with new tensor.

Example 1:

In this example, we are creating a one-dimensional tensor with 6 elements and returning the logistic sigmoid function of elements using the sigmoid() method.

Python3




import torch
  
# create 1D tensor with 6 elements
t1 = torch.arange(1, 13)
  
# display
print(t1)
  
# Compute the logistic sigmoid 
# function of elements in the
# above tensor
print(torch.sigmoid(t1))


Output:

tensor([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

tensor([0.7311, 0.8808, 0.9526, 0.9820, 0.9933, 0.9975, 0.9991, 0.9997, 0.9999,

        1.0000, 1.0000, 1.0000])

Example 2:

In this example, we are creating a one-dimensional tensor with 5 elements and returning the logistic sigmoid function of elements using torch.special.expit() method.

Python3




import torch
  
# create 1D tensor with 5 elements
t1 = torch.arange(1, 6)
  
# display
print(t1)
  
# Compute the logistic sigmoid 
# function of elements in the
# above tensor
print(torch.special.expit(t1))


Output:

tensor([1, 2, 3, 4, 5])

tensor([0.7311, 0.8808, 0.9526, 0.9820, 0.9933])

Example 3:

In this example, we are creating a two-dimensional tensor with 3×3 elements, and returning the logistic sigmoid function of elements using sigmoid() method.

Python3




import torch
  
# create 2D tensor with 3 elements each
t1 = torch.tensor([[-20, 34, 56], [6, -9, 8]])
  
# display
print(t1)
  
# Compute the logistic sigmoid function
# of elements in the above tensor
print(torch.sigmoid(t1))


Output:

tensor([[-20,  34,  56],
        [  6,  -9,   8]])
tensor([[2.0612e-09, 1.0000e+00, 1.0000e+00],
        [9.9753e-01, 1.2339e-04, 9.9966e-01]])

Example 4:

In this example, we are creating a two-dimensional tensor with 3×3 elements each and, returning the logistic sigmoid function of elements using torch.special.expit() method.

Python3




import torch
  
# create 2D tensor with 3 elements each
t1 = torch.tensor([[-20, 34, 56, ], [78, 90, 8]])
  
# display
print(t1)
  
# Compute the logistic sigmoid
# function of elements in the
# above tensor
print(torch.special.expit(t1))


Output:

tensor([[-20,  34,  56],
        [  6,  -9,   8]])
tensor([[2.0612e-09, 1.0000e+00, 1.0000e+00],
        [9.9753e-01, 1.2339e-04, 9.9966e-01]])

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments