We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Two-Layer MLP
A multilayer perceptron (MLP) combines fully connected layers with activation functions. This exercise has two weight transformations, with ReLU between them. ReLU replaces negative values with zero and leaves positive values unchanged.
The forward pass is
h = ReLU(x @ W1 + b1)
y = h @ W2 + b2
For x = [[1, -1]], identity W1, and zero b1, the first transformation
returns [[1, -1]] and ReLU gives [[1, 0]]. With
W2 = [[2], [-3]] and b2 = [-4], the final output is [[-2]].
That negative output is valid: there is no ReLU on the final layer.
Your task
Implement two_layer_mlp(x, W1, b1, W2, b2) for floating-point tensors:
| Input | Shape |
|---|---|
| x | (N, I) |
| W1 | (I, H) |
| b1 | (H,) |
| W2 | (H, O) |
| b2 | (O,) |
N is the batch size, I the input width, H the hidden width, and O the
output width. All are positive. The hidden result has shape (N, H);
return the final result with shape (N, O).
Here the weight matrices are already stored in multiplication order,
so do not transpose them. This differs from the (out_features, in_features) convention in the separate linear-layer exercise.
Broadcast each bias across batch rows. Add b1 before ReLU, and add b2
after the second matrix multiplication. Do not apply softmax, a final
activation, or any training update. Use the supplied parameters as given.
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.