# PyTorch nn Sigmoid tutorial with example

The PyTorch nn sigmoid is defined as an S-shaped curved and it does not pass across the origin and generates an output that lies between 0 and 1. In detail, we will discuss nn Sigmoid using PyTorch in python.

And additionally, we will cover different examples related to PyTorch nn sigmoid. And we will cover these topics.

• What is PyTorch nn sigmoid
• PyTorch nn sigmoid example
• How to use PyTorch nn functional sigmoid
• What is PyTorch nn log sigmoid
• PyTorch nn sigmoid activation function
• PyTorch nn sigmoid cross entropy
• What is PyTorch logistic sigmoid

## What is PyTorch nn sigmoid

In this section, we will learn about the What is PyTorch nn sigmod in python.

The PyTorch nn sigmoid is defined as an S-shaped curved and it does not pass across the origin and generates an output that lies between 0 and 1. The sigmoid applies the elementwise function.

Syntax:

Syntax of the PyTorch nn sigmoid:

``torch.nn.Sigmoid()``

In the sigmoid() function we can input any number of the dimensions.

The sigmoid returns a tensor in the form of input with the same dimension and shape with values in the range of [0,1].

So, with this, we understood about the PyTorch nn sigmoid with the help of torch.nn.Sigmoid() function.

## PyTorch nn sigmoid example

In this section, we will learn about how to implement the PyTorch nn sigmoid with the help of an example in python.

In PyTorch sigmoid, the value is decreased between 0 and 1 and the graph is decreased to the shape of S. If the values of S move to positive then the output value is predicted as 1 and if the values of S move to negative then the output value is predicted as 0.

Code:

In the following code, firstly we will import the torch library such as import torch and import torch.nn as nn.

• s = nn.Sigmoid(): Here we are calling the sigmoid() function.
• input = torch.randn(4) is used to declare the input variable by using torch.randn() function.
• output = s(input): Here we are declaring the output variable.
• print(output) is used to print the output with the help of the print() function.
``````# Import library
import torch
import torch.nn as nn
# Calling the sigmoid function
s = nn.Sigmoid()
# Declaring the input variable
input = torch.randn(4)
# Decribing the output variable
output = s(input)
# Print the output
print(output)``````

Output:

After running the above code, we get the following output in which we can see that the PyTorch nn sigmoid values are printed on the screen.

This is how we understand PyTorch nn sigmoid with the help of an example.

## How to use PyTorch nn functional sigmoid

In this section, we will learn How to use PyTorch nn functional sigmoid in python.

The PyTorch nn functional sigmoid is defined as a function based on elements where the real number is decreased to a value between 0 and 1.

Syntax:

Syntax of the PyTorch nn functional sigmoid.

``torch.nn.functional.sigmoid(input)``

Parameter:

The following are the parameter of the PyTorch nn functional sigmoid:

• input: The input parameter is defined as an input tensor.

This is how we can understand the PyTorch functional sigmoid by using a torch.nn.functional.sigmoid().

## What is PyTorch nn log sigmoid

In this section, we will learn about What is PyTorch nn log sigmoid in python.

The PyTorch nn log sigmoid is defined as the value is decreased between 0 and 1 and the graph is decreased to the shape of S and it applies the element-wise function.

Code:

In the following code, we will import all the necessary libraries such as import torch and import torch.nn as nn.

• ls = nn.LogSigmoid(): Here we are calling the LogSigmoid() function.
• input = torch.randn(4) is used to describe the input variable by using torch.randn() function.
• output = ls(input): Here we are declaring the output variable.
• print(output) is used to print the output by using the print() function.
``````# Import library
import torch
import torch.nn as nn
# Calling the LogSigmoid() function
ls = nn.LogSigmoid()
# Describing the input variable
input = torch.randn(4)
# Declaring the output variable
output = ls(input)
# Print the output
print(output)``````

Output:

After running the above code, we get the following output in which we can see that the PyTorch nn log sigmoid values are printed on the screen.

So, with this, we understood the PyTorch nn log sigmoid by using nn.LogSigmoid() function.

## PyTorch nn sigmoid activation function

In this section, we will learn about the PyTorch nn sigmoid activation function in python.

Before moving forward we should have a piece of knowledge about the activation function.

The activation function is a function that performs computations to give an output that acts as an input for the next neuron.

Code:

In the following code firstly we will import all the necessary libraries such as import torch and import torch.nn as nn.

• input = torch.Tensor([4,-5,6,-8]) is used to create a tensor.
• sigmoid_fun = nn.Sigmoid(): Here we are calling the sigmoid function.
• output = sigmoid_fun(input) is used to applying sigmoid to the tensor.
• print(output) is used to print the output by using the print() function.
``````# Importing Libraries
import torch
import torch.nn as nn

# Create a tensor
input = torch.Tensor([4,-5,6,-8])

# Calling the sigmoid function
sigmoid_fun = nn.Sigmoid()

# Applying sigmoid to the tensor
output = sigmoid_fun(input)

# Print the output
print(output)``````

Output:

After running the above code, we get the following output in which we can see that the PyTorch nn sigmoid activation function values are printed on the screen.

So, with this, we understood the PyTorch nn sigmoid activation function.

## PyTorch nn sigmoid cross entropy

In this section, we will learn about the PyTorch nn sigmoid cross entropy in python.

Before moving forward we should have a piece of knowledge about cross-entropy.

The cross-entropy creates a criterion that calculates the cross entropy between the target and input probabilities.

Code:

In the following code, we will import all the necessary libraries such as import torch and import torch.nn as nn.

• sce = nn.Sigmoid(): Here we are calling the sigmoid function.
• loss = nn.BCELoss(): Here we are calling the BCELoss() function.
• input = torch.randn(5, requires_grad=True) is used to describe the input variable by using a torch.randn() function.
• target = torch.empty(5).random_(4) is used to describing the target variable by using torch.empty() function.

print(output) is used to print the output by using the print() function.

``````# Import library
import torch
import torch.nn as nn
# Calling the Sigmoid function
sce = nn.Sigmoid()
# Calling the BCELoss() function
loss = nn.BCELoss()
# Describing the input variable
# Declaring the target variable
target = torch.empty(5).random_(4)
output = loss(sce(input), target)
# Print output
print(output)``````

Output:

In the below output, we can see that Pytorch nn sigmoid cross entropy values are printed on the screen.

This is how we understand the Pytorch nn sigmoid cross entropy with the help of nn.sigmoid() function.

## What is PyTorch logistic sigmoid

In this section, we will learn about What is PyTorch logistic sigmoid in python.

The PyTorch logistic sigmoid is defined as a nonlinear function that does not pass through the origin because it is an S-Shaped curve and makes an output that lies between 0 and 1. It is also called a logistic sigmoid function.

Code:

In the following code, we will import the torch library such as import torch, import torch.nn as nn.

• los = nn.Sigmoid (): Here we are calling the sigmoid() function.
• inp = torch.randn (5) is used to declare the input variable by using the torch.randn() function.
• out= los (inp): Here we are declaring the input variable.
• print(out) is used to print the output by using the print() function.
``````# Importing libraries
import torch
import torch.nn as nn
# Calling the Sigmoid() function
los = nn.Sigmoid ()
# Declaring the input variable
inp = torch.randn (5)
# Describing the output variable
out= los (inp)
# Print the output
print(out)``````

Output:

After running the above code we get the following output in which we can see that the PyTorch logistic sigmoid values are printed on the screen.

So, with this, we understood the PyTorch logistic sigmoid by using nn.Sigmoid() function.

So, in this tutorial, we discussed PyTorch nn Sigmoid and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

• What is PyTorch nn sigmoid
• PyTorch nn sigmoid example
• How to use PyTorch nn functional sigmoid
• What is PyTorch nn log sigmoid
• PyTorch nn sigmoid activation function
• PyTorch nn sigmoid cross entropy
• What is PyTorch logistic sigmoid

You may like the following PyTorch tutorials: