We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Activations step 9 of 9
Custom Activation with Gradient
Compute the SiLU activation and its derivative at each position in a vector. SiLU, also called Swish with coefficient 1, multiplies a value by its sigmoid:
sigmoid(t) = 1 / (1 + exp(-t))
silu(t) = t * sigmoid(t)
Your task
Implement silu_with_grad(x) for a floating-point vector of shape (n,).
Return a dictionary with exactly these two keys:
"output": the SiLU values, shape(n,)."gradient": the derivative of each output with respect to its own input, also shape(n,).
Preserve x's dtype and device in both tensors and leave x unchanged. Return the dictionary itself. No custom backward class or full Jacobian is required.
At x=0, sigmoid is 0.5, so the output is 0 and the derivative is 0.5.
For checking other values, write s=sigmoid(x); the elementwise derivative is
s + x*s*(1-s).
Differentiate a scalar total
Build the activation from multiplication and sigmoid. To use automatic differentiation, sum the activation values and differentiate that scalar total with respect to x. Each output depends only on the input at the same position, so this gradient contains the individual derivatives. That shortcut does not give the Jacobian of a function whose outputs mix input positions.
In PyTorch, enable gradient tracking on a copy of x and use
torch.autograd.grad(output.sum(), x_copy). In JAX, use jax.grad on a
function that returns jnp.sum(x * jax.nn.sigmoid(x)). Return the unsummed
activation values under "output"; the sum is only used for differentiation.
An empty input returns two empty vectors.
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?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.