Dense Layer Forward Pass

Medium
~18 min
code completion

Dense Layer Forward Pass

A single dense (fully connected) layer computes:

where:

  • is the input matrix of shape (m, n_in)
  • is the weight matrix of shape (n_in, n_out)
  • is the bias vector of shape (n_out,)
  • ReLU is applied element-wise to the result
  • This combines a linear transformation (the matrix multiply + bias) with a nonlinearity (ReLU) — the building block of deep networks.

    Your task:

    Implement dense_forward(X, W, b) that returns relu(X @ W + b).

    Example Tests

    Negative pre-activation clipped to 0

    Input: {"W":[[1,-1],[2,-2]],"X":[[1,2]],"b":[0,0]}

    Expected: [[5,0]]

    Identity-like weights with bias

    Input: {"W":[[1],[1]],"X":[[1,0],[0,1]],"b":[0]}

    Expected: [[1],[1]]

    Output shape is (m, n_out)

    Input: {"W":[[1,0],[0,1],[1,1]],"X":[[1,2,3],[4,5,6]],"b":[0,0]}

    Expected: [2,2]

    Sign in to solve this problem

    You can read the full problem statement above. Create a free account to run code in the browser, submit solutions, and track your progress.