Neural Mastery
← Practice
Dense Layer Forward Passeasy

Dense Layer Forward Pass

easy Neural Networks by Hand 15 min

NumPy allowed · plain Python earns 10 bonus points

Compute the fundamental affine transformation for a fully connected (dense) neural network layer across a mini-batch.

Task

Implement dense_forward(x, w, b) which computes the forward pass of a linear layer: y = x @ w + b. Given x of shape (N, D_in), weight matrix w of shape (D_in, D_out), and bias vector b of length D_out, return the output matrix of shape (N, D_out) where each element is rounded to 4 decimal places.

Function Signature

dense_forward(x: list[list[float]], w: list[list[float]], b: list[float]) -> list[list[float]]

Examples

Example 1: Single sample 2D to 2D
Input: x = [[1, 2]] w = [[0.5, -0.5], [1, 1.5]] b = [0.1, -0.2]
Output: [[2.6, 2.3]]
Example 2: Batch of 2 samples
Input: x = [[0, 1], [2, -1]] w = [[1, 2, 3], [-1, 0.5, 0]] b = [1, 0, -1]
Output: [[0, 0.5, -1], [4, 3.5, 5]]
Example 3: Zero weights and bias
Input: x = [[3, 4]] w = [[0], [0]] b = [5]
Output: [[5]]

Constraints

  • 1 <= N <= 100
  • 1 <= D_in, D_out <= 64
  • Inputs contain valid finite floating-point numbers
Python3Saved ✓
def dense_forward(x: list[list[float]], w: list[list[float]], b: list[float]) -> list[list[float]]:
"""Compute the linear affine transformation out = x @ w + b for batch input x.

Args:
x: 2D matrix of shape (N, D_in).
w: 2D weight matrix of shape (D_in, D_out).
b: 1D bias vector of length D_out.

Returns:
2D list of shape (N, D_out) rounded to 4 decimal places.
"""
pass

Case 1: Single sample 2D to 2D
Input: x = [[1, 2]] w = [[0.5, -0.5], [1, 1.5]] b = [0.1, -0.2]
Expected: [[2.6, 2.3]]
Case 2: Batch of 2 samples
Input: x = [[0, 1], [2, -1]] w = [[1, 2, 3], [-1, 0.5, 0]] b = [1, 0, -1]
Expected: [[0, 0.5, -1], [4, 3.5, 5]]
Case 3: Zero weights and bias
Input: x = [[3, 4]] w = [[0], [0]] b = [5]
Expected: [[5]]