Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Class

nn.Sigmoid

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.

How it works

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.

Syntax

class torch.nn.Sigmoid(*args, **kwargs)

Returns

forward(*input*) returns a Tensor with the same shape as the input.

Key points

  • The transformation is performed independently on each tensor element.
  • Inputs may have any number of dimensions.
  • The output preserves the complete input shape.
  • Calling the module executes its forward computation.
  • The forward operation returns a tensor.

Example: Apply Sigmoid

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.

When to use it

Element-wise activation

Apply a bounded nonlinear transformation to tensor values while preserving shape.

Composable model layer

Use a module object inside a torch.nn.Module model definition.

Shape-preserving tensor processing

Transform a vector or higher-dimensional tensor without reshaping it.

Interview questions

QWhat does 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.

QWhat are the input and output shapes?

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.

QHow is 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.sigmoid
  • torch.nn.LogSigmoid
  • torch.nn.Tanh

Source: Sigmoid. Checked 2026-10-16.