- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PyTorch – How to compute the logistic sigmoid function of tensor elements?
To compute the logistic function of elements of a tensor, we use torch.special.expit() method. It returns a new tensor with computed logistic function element-wise. It accepts torch tensor of any dimension. We could also apply torch.sigmoid() method to compute the logistic function of elements of the tensor. It is an alias of the torch.special.expit() method.
Syntax
torch.special.expit(input) torch.sigmoid(input)
Where input is a torch tensor of any dimension.
Steps
We could use the following steps to compute logistic sigmoid function of a tensor element-wise −
Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.
import torch
Define a torch tensor. Here we define a 2D tensor of random numbers.
tensor = torch.randn(2,3,3)
Compute the logistic sigmoid function of the tensor using torch.special.expit(input) or torch.sigmoid(input). input is torch tensor of any dimension. Optionally assign this value to a new variable
sig = torch.special.expit(tensor)
Print the computed logistic sigmoid function.
print("Entropy:", sig)
Example 1
In this program, we compute the sigmoid function of a 1D tensor using torch.sigmoid().
# import necessary libraries import torch # define a 1D tensor tensor1 = torch.tensor([-1,2,0,.4,5]) # print above created tensor print("Tensor:", tensor1) # Compute the logistic sigmoid function of elements sig = torch.sigmoid(tensor1) # Display the computed Logistic Sigmoid function print("Logistic Sigmoid:", sig)
Output
Tensor: tensor([-1.0000, 2.0000, 0.0000, 0.4000, 5.0000]) Logistic Sigmoid: tensor([0.2689, 0.8808, 0.5000, 0.5987, 0.9933])
Example 2
In this program, we compute the sigmoid function of 1D and 2D tensors using torch.special.expit()
# import torch library import torch # define a 1D tensor tensor1 = torch.tensor([-1,2,0,.4,5]) # print above created tensor print("Tensor 1:", tensor1) # compute the logistic sigmoid function of elements sig1 = torch.special.expit(tensor1) # Display the computed Logistic Sigmoid function print("Logistic Sigmoid Function:
", sig1) # define a 2D tensor tensor2 = torch.randn(2,3,3) # print above created tensor print("Tensor 2:", tensor2) # compute the logistic sigmoid function of elements sig2 = torch.special.expit(tensor2) # Display the computed logistic sigmoid function print("Logistic Sigmoid Function:
", sig2)
Output
Tensor 1: tensor([-1.0000, 2.0000, 0.0000, 0.4000, 5.0000]) Logistic Sigmoid Function: tensor([0.2689, 0.8808, 0.5000, 0.5987, 0.9933]) Tensor 2: tensor([[[-0.6318, -1.7586, 0.0252], [-0.0684, -0.4922, 1.7505], [-1.3301, 0.1333, -0.3744]], [[ 1.0607, -0.3999, 0.4564], [ 1.3029, 1.4259, 0.6266], [ 1.1038, 0.3965, 0.1522]]]) Logistic Sigmoid Function: tensor([[[0.3471, 0.1470, 0.5063], [0.4829, 0.3794, 0.8520], [0.2091, 0.5333, 0.4075]], [[0.7428, 0.4013, 0.6122], [0.7863, 0.8063, 0.6517], [0.7510, 0.5978, 0.5380]]])