Back to Practice
#0018
Dense Layer Forward Pass
MediumML18 min15 XP
Problem
Given input matrix X, weight matrix W, and bias vector b, return X @ W + b.
Why This Matters
A dense layer is just matrix multiplication plus bias. Once this clicks, neural networks become less like magic and more like repeated linear algebra.
Function Signature
def dense_forward(X, W, b):
Examples
Example 1
InputX shape (2, 3), W shape (3, 2), b shape (2,)
Outputoutput shape (2, 2)
Each input row becomes one output row, and each output unit has one bias.
Constraints
- X is a 2D NumPy array of shape (batch, input_features).
- W is a 2D NumPy array of shape (input_features, output_units).
- b is a 1D NumPy array of shape (output_units,).
CodePython
Visible browser tests run here when available.
Testcases1 visible / 4 hidden categories
Two samples, two output units
InputX = [[1,2,3],[4,5,6]]
Expected[[5, 7], [11, 13]]
The bias is added to every row after matrix multiplication.
Hidden Test Categories
Single sample batchOne output unitNegative weightsFloating point inputs