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

Back to Questions

177. Gradient checking

medium
GeneralGeneral
staff

Gradient checking is a simple way to verify your backprop gradients by comparing them to numerical gradients from finite differences. In this task, you’ll implement gradient checking for a tiny neural network layer so you can catch sign/shape mistakes early.

Requirements

Implement the function

python

Rules:

  • Compute analytical gradients ( \frac{\partial L}{\partial W} ) for the model above.
  • Compute numerical gradients using centered differences:
    gnum(i)=L(Wi+ϵ)āˆ’L(Wiāˆ’Ļµ)2ϵg_{\text{num}}(i) = \frac{L(W_i + \epsilon) - L(W_i - \epsilon)}{2\epsilon}
  • For each weight entry, return the relative error:
    rel_err=∣ganalāˆ’gnum∣max⁔(1eāˆ’8, ∣ganal∣+∣gnum∣)\text{rel\_err} = \frac{|g_{\text{anal}} - g_{\text{num}}|}{\max(1e-8,\ |g_{\text{anal}}| + |g_{\text{num}}|)}
  • Use a numerically stable sigmoid and avoid taking log(0) by clipping probabilities (e.g., to [1e-12, 1-1e-12]).
  • Do not use autograd frameworks (no PyTorch/TensorFlow/JAX); only NumPy + built-ins.

Example

python

Output:

python
Input Signature
ArgumentType
Wnp.ndarray
Xnp.ndarray
bnp.ndarray
ynp.ndarray
epsfloat
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • Use NumPy only; no autograd frameworks.

  • Return relative error as (d,1) np.ndarray.

  • Use stable sigmoid; clip probabilities for log.

Hint 1

Convert inputs to NumPy arrays with consistent shapes: X:(n,d), y:(n,1), W:(d,1), b:(1,) so z = X @ W + b works without broadcasting surprises.

Hint 2

For sigmoid + binary cross-entropy, the gradient simplifies: dz = (a - y)/n where a = sigmoid(z). Then dW = X.T @ dz gives the analytical gradient.

Hint 3

Write a forward_loss(W_local) helper that returns a scalar loss. For each weight W[i,0], compute centered-difference g_num = (L(W+eps)-L(W-eps))/(2*eps), then return relative error |g_anal-g_num|/max(1e-8,|g_anal|+|g_num|). Clip a to avoid log(0) and use a stable sigmoid.

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
staff
senior
entry
Tags
gradient-checking
finite-differences
backpropagation
binary-cross-entropy
27 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