- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
PyTorch Tutorial
- PyTorch Tutorial
- PyTorch Introduction
- PyTorch Installation
- PyTorch Basics
- PyTorch Tensors
- PyTorch Neural Network Basics
- PyTorch First Neural Network
- PyTorch Data Processing and Loading
- PyTorch Linear Regression
- PyTorch Convolutional Neural Network
- PyTorch Recurrent Neural Network
- PyTorch Datasets
- PyTorch Data Transforms
- Pytorch torch Reference
- PyTorch torch.nn Reference
- Transformer Model
- PyTorch Build Transformer Model
- PyTorch torch.optim Optimizer Module
- PyTorch torchvision Computer Vision Module
- PyTorch Model Deployment
- PyTorch Model Save and Load
- PyTorch Image Classification
- PyTorch Text Sentiment Analysis
- PyTorch Autograd
- PyTorch GPU / CUDA Acceleration
- PyTorch Loss Functions
- PyTorch Learning Rate Scheduler
- PyTorch Transfer Learning
- PyTorch Batch Normalization and Dropout
- PyTorch LSTM / GRU
- PyTorch Word Embedding (Embedding)
- PyTorch Generative Adversarial Network (GAN)
- PyTorch Autoencoder (Autoencoder)
- PyTorch Model Evaluation and Debugging
- PyTorch torchtext
- PyTorch Mixed Precision Training (AMP)
- PyTorch TorchScript/ONNX Export
- PyTorch Distributed Training
- PyTorch Attention Mechanism
PyTorch torch.nn.Tanh Function
The torch.nn.Tanh function is a hyperbolic tangent activation function commonly used in neural networks. It maps input values to the range (-1, 1), making it useful for centering the output around zero.
Syntax
torch.nn.Tanh()
Example
import torch
import torch.nn as nn
# Create a Tanh activation function
tanh = nn.Tanh()
# Input tensor
input = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
# Apply Tanh
output = tanh(input)
print(output)
Output
tensor([-0.9640, -0.7616, 0.0000, 0.7616, 0.9640])
Description
The torch.nn.Tanh function computes the hyperbolic tangent of each element in the input tensor. The formula is:
f(x) = tanh(x) = (e^x - e^(-x)) / (e^x + e^(-x))
This function is differentiable and smooth, making it suitable for gradient-based optimization in deep learning models.
Notes
- Unlike ReLU, Tanh outputs both positive and negative values, which can help with gradient flow.
- It suffers from vanishing gradients for large absolute input values.
- Often used in hidden layers of neural networks, especially in RNNs and older architectures.
YouTip