How to Use nn.Sigmoid in PyTorch: Syntax and Example
In PyTorch, use
nn.Sigmoid() to apply the sigmoid activation function as a layer in your model. Instantiate it and call it on your input tensor to get output values between 0 and 1.Syntax
The nn.Sigmoid() class creates a sigmoid activation layer. You instantiate it once and then apply it to input tensors to transform values into the range (0, 1).
Usage pattern:
sigmoid = nn.Sigmoid()- creates the sigmoid layer.output = sigmoid(input_tensor)- applies sigmoid to the input tensor.
python
import torch import torch.nn as nn sigmoid = nn.Sigmoid() input_tensor = torch.tensor([-1.0, 0.0, 1.0, 2.0]) output = sigmoid(input_tensor) print(output)
Output
tensor([0.2689, 0.5000, 0.7311, 0.8808])
Example
This example shows how to use nn.Sigmoid() to convert raw scores into probabilities between 0 and 1. It demonstrates creating the sigmoid layer, applying it to a tensor, and printing the result.
python
import torch import torch.nn as nn # Create sigmoid activation layer sigmoid = nn.Sigmoid() # Example input tensor with positive and negative values input_tensor = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0]) # Apply sigmoid activation output = sigmoid(input_tensor) print('Input:', input_tensor) print('Output after sigmoid:', output)
Output
Input: tensor([-2., -1., 0., 1., 2.])
Output after sigmoid: tensor([0.1192, 0.2689, 0.5000, 0.7311, 0.8808])
Common Pitfalls
Common mistakes when using nn.Sigmoid() include:
- Trying to use
nn.Sigmoidwithout parentheses, which creates a class reference instead of an instance. - Applying sigmoid multiple times accidentally, which can distort outputs.
- Using sigmoid on already normalized data, which may not be necessary.
Always instantiate nn.Sigmoid() before calling it on tensors.
python
import torch import torch.nn as nn input_tensor = torch.tensor([0.5, 1.0, 1.5]) # Wrong: missing parentheses, sigmoid is a class, not an instance # sigmoid = nn.Sigmoid # output = sigmoid(input_tensor) # This will raise an error # Correct way: sigmoid = nn.Sigmoid() output = sigmoid(input_tensor) print(output)
Output
tensor([0.6225, 0.7311, 0.8176])
Quick Reference
| Usage | Description |
|---|---|
| nn.Sigmoid() | Creates a sigmoid activation layer |
| sigmoid(input_tensor) | Applies sigmoid to input tensor |
| Output range | Values between 0 and 1 |
| Common use | Final layer for binary classification |
Key Takeaways
Instantiate nn.Sigmoid() before applying it to tensors.
Sigmoid outputs values between 0 and 1, useful for probabilities.
Avoid calling nn.Sigmoid without parentheses to prevent errors.
Use sigmoid as an activation layer, often in binary classification models.
Applying sigmoid multiple times is usually a mistake.