PyTorch TanH

The PyTorch TanH is defined as a distinct and non-linear function with is same as a sigmoid function and the output value in the range from -1 to +1. In detail, we will discuss TahnH using PyTorch in Python.

And additionally, we will also cover different examples related to PyTorch TanH. And we will cover these topics.

  • What is PyTorch TanH
  • PyTorch TanH example
  • PyTorch TanH inplace
  • PyTorch TanH activation function
  • Pytorch TanH layer
  • PyTorch TanH inverse
  • What is PyTorch Tanhshrink
  • How to use PyTorch functional TanH
  • PyTorch TanH Vs ReLU

What is PyTorch TanH

In this section, we will learn about the PyTorch TanH in python.

The PyTorch TanH is defined as a distinct and non-linear function with is same as a sigmoid function and the output value in the range from -1 to +1. It is an S-shaped curve that passes through the origin.

Syntax:

Syntax of the PyTorch Tanh:

torch.nn.Tanh()

The Tanh returns the hyperbolic tangent function element-wise.

This is how we understand about PyTorch TanH with the help of torch.nn.Tanh() function.

Read PyTorch Reshape Tensor

PyTorch TanH example

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

Tanh’s function is similar to the sigmoid function. It is an S-shaped curve but it passes across the origin and the output value range of Tanh is from -1 to +1. The Tanh is also a non-linear and differentiable function.

Code:

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

  • a = nn.Tanh(): Here we are using the Tanh() function.
  • input = torch.randn(4) is used to describe the input variable by using torch.randn() function.
  • output = a(input) is used to 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
# Using Tanh() function
a = nn.Tanh()
# Describing the input variable
input = torch.randn(4)
# Declaring the output variable
output = a(input)
# Print output
print(output)

Output:

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

PyTorch TanH example
PyTorch TanH example

So, with this, we understood the PyTorch TanH implementation with the help of an example.

Read PyTorch Add Dimension

PyTorch TanH inplace

In this section, we will learn about the Pytorch TanH inplace with the help of an example in python.

The TanH is a good characteristic for the activation function. It is non-linear and differentiable and its output range lies between -1 to +1.

READ:  How to drop Unnamed column in Pandas DataFrame [3 Methods]

Syntax:

Syntax of the PyTorch Tanh inplace:

nn.Tanh(inplace=True)

Parameter:

The following is the parameter of PyTorch Tanh inplace:

inplace = True It means that it will alter the input directly without assigning any additional output and the default value of inplace is False.

So, with this, we understood about the PyTorch TanH inplace with the help of nn.Tanh() function.

PyTorch TanH activation function

In this section, we will learn about the Pytorch TanH activation function in python.

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

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

The TanH is an S-shaped curve that passes across the origin and the output value range lies in between -1 to +1.

Code:

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

  • th = nn.Tanh(): Here we are using the TanH function.
  • input = torch.Tensor([2,-4,5,-7]) is used to declare the input variable by using torch.tensor() function.
  • output = th(input): Here we are applying the TanH to the tensor.
  • print(output) is used to print the output with the help of the print() function.
# Importing libraries
import torch
import torch.nn as nn
 
# Here we are calling the Tanh function
th = nn.Tanh()
 
# Declaring the tensor
input = torch.Tensor([2,-4,5,-7])
 
# Here we are applying the Tanh to the tensor
output = th(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 TanH activation function values are printed on the screen.

PyTorch TanH activation function
PyTorch TanH activation function

So, with this, we understood about the PyTorch TanH activation function in pyhton.

Read PyTorch nn Conv2d

Pytorch TanH layer

In this section, we will learn about the PyTorch TanH layer in python.

The PyTorch TanH layer is defined as a layer that calculated the hyperbolic tangent of the input.

Code:

In the following code we will import the torch module such as import torch and import torch.nn as nn.

  • l = nn.Tanh(): Here we are calling the Tanh() function.
  • input = torch.randn(9): Here we are declaring the input variable by using the torch.randn() function.
  • output = l(input): Here we are describing the output variable.
  • print(” The input:”,input) is used to print the input variable.
  • print(” The output:”,output) is used to print the output variable.
# Import library
import torch
import torch.nn as nn

# Calling the Tanh() function
l = nn.Tanh()

# Declaring the input variable
input = torch.randn(9)

# Describing the output variable
output = l(input)

# Print the input and output
print(" The input:",input)
print(" The output:",output)

Output:

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

PyTorch TanH layer
PyTorch TanH layer

This is how we understand about the PyTorch TanH layer with the help of nn.Tanh() function.

READ:  Matplotlib log log plot

Read PyTorch Hyperparameter Tuning

PyTorch TanH inverse

In this section, we will learn about the PyTorch TanH inverse in python.

The torch.aTanh() returns a new tensor with the inverse hyperbolic tangents of elements of input. The domain of PyTorch TanH inverse tangel is (-1,1).

Code:

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

  • inver = torch.randn(4).uniform_(-3, 3) is used to declare the inverse variable by suing torch.randn() function.
  • print(“The input:”,inver) is used to print the input by using print() function.
  • t= torch.atanh(inver): Here we are calling the inverse TanH.
  • print(“The Output:”, t) is used to print the output with the help of the print() function.
#Import library 
import torch
# Declaring the variable
inver = torch.randn(4).uniform_(-3, 3)
print("The input:",inver)
# Calling the inverse TanH
t= torch.atanh(inver)
# Print the output
print("The Output:", t)

Output:

After running the above code, we get the following output in which we can see that the PyTorch TanH inverse vales are printed on the screen.

PyTorch TanH inverse
PyTorch TanH inverse

So, with this, we understood about the PyTorch TanH inverse by using the atanh() function in Python.

Read Jax Vs PyTorch

What is PyTorch Tanhshrink

In this section, we will learn about the PyTorch Tanhshrink in python.

The PyTorch Tanhshrink applies the element-wise function that is Tanhshrink = x – tanh(X). The TanH applies the element-wise Hyperbolic tangent function.

Code:

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

  • s = nn.Tanhshrink(): Here we are calling the Tanhshrink() function.
  • input = torch.randn(4) is used to declare the input variable by using torch.randn() function.
  • output = s(input) is used to describing the output variable.
  • print(output) is used to print the output by using the print() function.
# Importing libraries
import torch
import torch.nn as nn
# Calling the Tanhshrink()
s = nn.Tanhshrink()
# Declaring the input variable
input = torch.randn(4)
# Describing the output variable
output = s(input)
# Print output
print(output)

Output:

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

PyTorch Tanhshrink
PyTorch Tanhshrink

This is how we understand the PyTorch Tanhshrink by using Tanhshrink() function by python.

Read PyTorch Linear Regression

How to use PyTorch functional TanH

In this section, we will learn about the PyTorch functional TanH in python.

The PyTorch functional TanH is defined as the nn,functional.tanh() function that applies element-wise. It is non-linear and differentiable and its output range lies between -1 to +1.

Syntax:

The Syntax of the PyTorch functional TanH is :

torch.nn.functional.tanh(input)

Parameter:

The following is the parameter of PyTorch functional Tanh:

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

So, with this, we understood about the PyTorch functional TanH by using torch.nn.functional.tanh() function.

READ:  Check if NumPy Array is Empty in Python [4 Methods]

PyTorch TanH Vs ReLU

In this section, we will discuss about the PyTorch TanH Vs ReLU in python.

Pytorch TanH

The PyTorch TanH is defined as a distinct and non-linear function with is same as a sigmoid function and the output value in the range from -1 to +1.

Code:

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

  • v = nn.Tanh(): Here we are defining the tanh function.
  • input = torch.Tensor([2,-4,5,-7]) is used to describe the input variable by using a torch.tensor() function.
  • output = v(input): Here we are applying Tanh to the tensor.
  • print(output) is used to print the output with the help of the print() function.
# Import library
import torch
import torch.nn as nn
 
# Here we are calling the Tanh function
v = nn.Tanh()
 
# Defining tensor
input = torch.Tensor([2,-4,5,-7])
 
# Here we are applying Tanh to the tensor
output = v(input)
print(output)

Output:

In the below output, we can see that the PyTorch TanH values are printed on the screen.

PyTorch TanH
PyTorch TanH

PyTorch ReLU

The PyTorch ReLU is also a non-linear and differentiable function. In ReLU if the inputs are negative then its derivative becomes zero and the learning rate of neurons stops.

Code:

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

  • v = nn.ReLU(): Here we are calling the relu() function.
  • input = torch.Tensor([2, -4, 5, -7]) is used to create a tensor with an array.
  • output = v(input): Here we are passing the array to relu function.
  • print(output) is used to print the output by using the print() function.
# Import library
import torch
import torch.nn as nn
 
# Here we are calling the relu
v = nn.ReLU()
 
# Creating a Tensor with an array
input = torch.Tensor([2, -4, 5, -7])
 
# Passing the array to relu function
output = v(input)

# Print the output
print(output)

Output:

In the below output, we can see that the PyTorch ReLU values are printed on the screen.

PyTorch ReLU
PyTorch ReLU

So, with this, we understood about the difference between the PyTorch TanH and ReLU in python.

You may like the following PyTorch tutorials:

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

  • What is PyTorch TanH
  • PyTorch TanH example
  • PyTorch TanH inplace
  • PyTorch TanH activation function
  • Pytorch TanH layer
  • PyTorch TanH inverse
  • What is PyTorch Tanhshrink
  • How to use PyTorch functional TanH
  • PyTorch TanH Vs ReLU