Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

87. Backpropagation through linear layer

easy
GeneralGeneral
senior

Backpropagation through a linear (fully-connected) layer is a core deep learning building block, and you’ll implement its backward pass from scratch. Given an upstream gradient, compute gradients for the weights, bias, and inputs using the chain rule: Y=XW+bY = XW + b.

Requirements

Implement the function

python

Rules:

  • Use the formulas: (dX = dY W^\top), (dW = X^\top dY), (db = \sum_{i=1}^{n} dY_i) (sum over the batch).
  • Return each gradient as a NumPy array.
  • Don’t use any autograd libraries (no PyTorch, JAX, TensorFlow).
  • Use only NumPy and built-in Python libraries.
  • Keep it in a single function.

Example

python

Output:

python
Input Signature
ArgumentType
Wnp.ndarray
Xnp.ndarray
bnp.ndarray
dYnp.ndarray
Output Signature
Return NameType
valuetuple

Constraints

  • Return NumPy arrays

  • Use only NumPy; no autograd libraries

  • Single function; no helper modules

Hint 1

Use the given identities directly: dX = dY @ W.T and dW = X.T @ dY. Check shapes to avoid transpose mistakes.

Hint 2

For the bias gradient, remember bias is broadcast across the batch: db is the sum of dY over the batch dimension (axis=0).

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
backpropagation
linear-layer
matrix-calculus
numpy
18 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit