TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

233. Sigmoid (PyTorch)

Easy

Implement sigmoid in PyTorch using primitive tensor ops only.

Signature: def sigmoid(x: torch.Tensor) -> torch.Tensor

The rule: you may NOT call torch.sigmoid, F.sigmoid, or nn.Sigmoid. Implement it yourself with primitives. We verify your output matches torch.sigmoid(x).

Allowed primitives: .exp(), .where(), basic arithmetic.

Numerical stability: the naive 1 / (1 + exp(-x)) overflows exp for very negative x (e.g. -1000), giving inf in the denominator and (worse) nan gradients via the autograd graph. The standard fix is to dispatch on the sign of x (using torch.where) so the argument passed to exp is always ≤ 0 — pick the algebraically equivalent rearrangement on the negative branch yourself.

PyTorch idioms vs the NumPy version:

  • torch.where works exactly like np.where, but the predicate must be a tensor (not a Python bool).
  • (-x).exp() is preferred over torch.exp(-x) — both work, method form is more common.
  • The output is a real tensor; gradients flow through your primitive ops automatically (no @torch.no_grad() needed).

Math

σ(x)=1+e−x1​

Related problems

  • SigmoideasyNumPy

Asked at

NumPy

import numpy as np

 

def sigmoid(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?