Element-wise activation
Apply a bounded nonlinear transformation to tensor values while preserving shape.
PyTorch · Class
torch.nn.Sigmoid applies the sigmoid function independently to every input element. It accepts tensors of any dimensionality and returns a tensor with the same shape.
When called, the module applies the sigmoid transformation independently to every input element, mapping each value through the reciprocal of one plus the exponential of its negation. It accepts tensors with any number of dimensions and preserves the input shape.
class torch.nn.Sigmoid(*args, **kwargs)forward(*input*) returns a Tensor with the same shape as the input.
import torch
from torch import nn
m = nn.Sigmoid()
input = torch.randn(2)
output = m(input)
print(output.shape)Creates the module, applies it element-wise to a two-element tensor, and prints the unchanged shape.
Apply a bounded nonlinear transformation to tensor values while preserving shape.
Use a module object inside a torch.nn.Module model definition.
Transform a vector or higher-dimensional tensor without reshaping it.
nn.Sigmoid do?nn.Sigmoid applies the sigmoid function element-wise. Its output has the same shape as its input.
Follow-up: What is the transformation? It is (x) = 1 / (1 + exp(-x)), so each input element is independently mapped to a bounded value.
The module accepts an input with any number of dimensions, represented by (*) in the documentation. The result also has shape (*), matching the input.
Follow-up: How would a two-dimensional input behave? Sigmoid would transform every element while retaining both dimensions.
nn.Sigmoid used in code?Instantiate it with nn.Sigmoid() and invoke the resulting module on a tensor. The documented example uses a random tensor with shape (2) and assigns the result of the module call to output.
Follow-up: What does the module's forward operation return? It returns a Tensor containing the element-wise transformed values.
torch.nn.functional.sigmoidtorch.nn.LogSigmoidtorch.nn.Tanh