We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Implement Linear Layer
A fully connected layer combines an example's input features to produce new output features. Each output feature has its own row of weights and one bias value. For each example, take a dot product with that weight row, then add the corresponding bias.
For input [1, 2], weight rows [[2, -1], [0, 3]], and bias [0.5, -1]:
- The first output is
1*2 + 2*(-1) + 0.5 = 0.5. - The second output is
1*0 + 2*3 - 1 = 5.
The output for that example is [0.5, 5]. Apply the same weights and
biases independently to every row in the batch.
Your task
Implement linear(x, weight, bias) with these floating-point tensors:
| Input | Shape | Meaning |
|---|---|---|
| x | (N, I) |
N examples with I input features each |
| weight | (O, I) |
One row of I weights per output feature |
| bias | (O,) |
One bias per output feature |
All dimensions are positive. Return shape (N, O) by computing
x @ weight.T + bias. The transpose changes the weight shape to (I, O),
so the matrix product's inner dimensions match. Broadcast bias across
rows; do not add it once for every input feature.
Use matrix multiplication and addition rather than a built-in Linear layer. Do not add an activation function. The name linear layer is common, although a nonzero bias makes the transformation affine rather than strictly linear. The supplied weights are used directly; this task does not train them.
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Multiply rows by columns
For A with shape (M, K) and B with shape (K, N), the matrix product has
shape (M, N). Each output entry is a dot product of one row of A and one
column of B:
$$C_{ij}=\sum_{k=1}^{K}A_{ik}B_{kj}.$$
For example, multiplying row [1, 2] by column [3, 4] gives
1 * 3 + 2 * 4 = 11. Elementwise multiplication would instead give [3, 8];
it does not include the sum.
If examples are rows of X, with shape (B, D), and weights have shape (D, H),
then XW produces H features for each of the B examples.
Keep batch axes separate from matrix axes
For batched matrix multiplication, the last two axes hold each matrix. Leading axes identify batches of matrices. A general matrix-multiply operation may broadcast compatible batch axes. A stricter batched operation may require them to match exactly. Use the operation specified by the exercise.
Einstein notation, often used with einsum, names the axes explicitly:
bij,bjk->bikmultiplies corresponding matrices in batchband sums overj.bi,bj->bijforms an outer product for each example: output(b, i, j)is the product of input(b, i)and input(b, j).
An axis is summed out when its label is absent from the explicit output.
A label that appears in both inputs is not necessarily summed: b stays in both
outputs above.
Choose a multiplication order with small intermediate results
In exact arithmetic, (AB)C = A(BC), but the two orders can have very different
costs. If the shapes are (1000, 10), (10, 10), and (10, 1), forming BC
first needs a (10, 1) intermediate. Forming AB first needs a (1000, 10)
intermediate and many more operations.
Floating-point rounding can also differ between orders, so do not expect every bit of the two results to match.
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.