The Logistic sigmoid function is a well-known function because it can be utilized as an activation function in the neural network. It computes the connection between the outcome i.e. dependent variables and features i.e. independent variables. PyTorch provides various methods/functions to calculate the logistic sigmoid function of the specific tensor elements. 

This blog will illustrate the methods to find the logistic sigmoid function of specific PyTorch tensor elements. 

How to Find/Calculate Logistic Sigmoid Function of PyTorch Tensor Elements?

To find/calculate the logistic sigmoid function of the tensor’s elements in PyTorch, two methods are used:

  • Method 1: Using “torch.special.expit()” Method
  • Method 2: Using “torch.sigmoid()” Method

Method 1: Find Logistic Sigmoid Function of Tensor Elements Using “torch.special.expit()” Method

To find the logistic sigmoid function of tensor elements using the “torch.special.expit()” method, check out the given-provided steps:

Step 1: Install PyTorch Library

First, install the “torch” library to find the logistic sigmoid function:

import torch

Step 2: Define a Tensor

Then, define and print a desired tensor. For example, we are defining a 1D “Tens1” tensor using the “torch.tensor()” function:

Tens1 = torch.tensor([9, -4, 2, 7, 6])

print(Tens1)

This has created a 1D tensor as seen below:

Step 3: Calculate Logistic Sigmoid Function Using “torch.special.expit()” Method

Now, use the “torch.special.expit()” method and pass the above-created tensor as an argument to find its logistic sigmoid function:

Log_sig = torch.special.expit(Tens1)

Step 4: Print Calculated Logistic Sigmoid Function

Finally, display the calculated logistic sigmoid function of the tensor elements:

print(Log_sig)

The below output shows the computed logistic sigmoid function of the “Tens1” tensor elements:

Similarly, users can define 2D or 3D tensors and find its logistic sigmoid function using the “torch.special.expit()” method.

Method 2: Find Logistic Sigmoid Function of Tensor Elements Using “torch.sigmoid()” Method

To find the logistic sigmoid function of tensor elements using the “torch.sigmoid()” method, follow the below-listed steps:

Step 1: Install PyTorch Library

First, install the “torch” library to find the logistic sigmoid function:

import torch

Step 2: Define a Tensor

After that, define a desired tensor and display its elements. Here, we are defining a 2D “Tens2” tensor using the “torch.tensor()” function:

Tens2 = torch.tensor([[3, 9, 7], [4, -2, 8]])

print(Tens2)

This has created a 2D tensor as seen below:

Step 3: Calculate Logistic Sigmoid Function Using “torch.sigmoid()” Method

Now, find the logistic sigmoid function of the above-created tensor using the “torch.sigmoid()” method and pass the tensor as an argument:

Log_sig = torch.sigmoid(Tens2)

Step 4: Print Calculated Logistic Sigmoid Function

Lastly, display the calculated logistic sigmoid function of the tensor elements:

print(Log_sig)

In the below output, the logistic sigmoid function of the “Tens2” tensor elements can be seen:

We have explained the efficient methods to calculate the logistic sigmoid function of the 1D and 2D tensor elements in PyTorch.

Note: Click on the provided link to access our Google Colab Notebook.

Conclusion

To calculate/find the logistic sigmoid function of particular tensor elements in PyTorch, first, import the “torch” library. Then, define the 1D or 2D tensor and view its elements. Next, use the “torch.special.expit()” or “torch.sigmoid()” method to calculate the tensor elements’ logistic sigmoid function. Finally, display the calculated logistic sigmoid function. This blog has illustrated different methods to find the logistic sigmoid function of specific PyTorch tensor elements.