Skip to content

← Neural Network Layers step 1 of 9

Medium Primitives

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.