We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Activations step 2 of 9
Implement Sigmoid
Sigmoid turns each input into a value between zero and one. It is often used to turn a binary classifier's score into a probability:
sigmoid(x) = 1 / (1 + exp(-x))
Zero maps to 0.5. Positive inputs give values above 0.5, and negative inputs give values below 0.5. For [-1.0, 0.0, 1.0], the result is approximately [0.268941, 0.5, 0.731059]. Apply the rule independently to every element.
Avoid an overflowing exponential
The direct formula computes exp(-x), which can overflow for a large negative input. An equivalent form keeps the exponent nonpositive:
- For x >= 0, use z = exp(-x), then return 1 / (1 + z).
- For x < 0, use z = exp(x), then return z / (1 + z).
Use a tensor selection to choose the exponent first, then compute exp. This avoids evaluating an overflowing exponential in a branch you discard. The selected formulas agree at zero.
Your task
Implement sigmoid(x) for a floating-point tensor of any shape. Preserve the shape and use basic arithmetic, exp and elementwise selection. Do not call torch.sigmoid or jax.nn.sigmoid.
In real arithmetic, finite inputs produce values strictly between zero and one. Floating-point results can round to exactly zero or one for large input magnitudes. The supplied tests include large positive and negative values; your output should not contain NaN for those finite inputs.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Nonlinear activations let a network learn more than a linear map
An affine layer applies weights and adds a bias. Two such layers without a nonlinear activation between them can be combined into one:
$$(xW_1+b_1)W_2+b_2=x(W_1W_2)+(b_1W_2+b_2).$$
An activation adds an input-dependent response that cannot generally be absorbed into a single set of weights and biases.
Common elementwise activations
ReLU, max(0, x), keeps positive values and sets negative values to zero.
For example, [-2, 0, 3] becomes [0, 0, 3]. Its derivative is zero for negative
inputs and one for positive inputs. At zero, the ordinary derivative does not
exist, so an implementation needs a convention.
Leaky ReLU uses a small slope on the negative side instead of setting it to zero. Sigmoid maps finite real inputs to probabilities between zero and one in exact arithmetic. Tanh maps them between negative one and one. For both sigmoid and tanh, large input magnitudes lead to small derivatives, which can slow learning.
SiLU and GELU use smooth gates
SiLU is x * sigmoid(x). Its derivative includes both the direct change in x
and the change in the sigmoid factor.
GELU is x * Phi(x), where Phi(x) is the probability that a standard normal
random variable is at most x. A common implementation approximates this using
tanh. The approximation is close but not identical, so use the version specified
in the exercise.
These activations act independently on each element and preserve shape. Softmax instead combines scores along an axis so the resulting probabilities sum to one. Applying sigmoid to every class score is not the same operation.
Use stable formulas where needed. For example, computing sigmoid directly as
1 / (1 + exp(-x)) can overflow in the exponential for a large negative x,
even though the desired output is close to zero.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
JAX reference solution
Sign in to track your attempts toward unlocking the reference solution.