Gradient Checking

~15 mincode completion

Implement numerical_gradient(w, eps) returning the numerical gradient of as an array the same shape as .

Nudge one component at a time. Copy before modifying it, or you will be differentiating a moving target.

Examples

The gradient of sum of squares is twice the weights

Input
numerical_gradient([1, 2], 0.00001)
Output
[2, 4]

At the origin every partial derivative is zero

Input
numerical_gradient([0, 0, 0], 0.00001)
Output
[0, 0, 0]

Negative weights give negative partials

Input
numerical_gradient([-3, 0.5], 0.00001)
Output
[-6, 1]

Hints

Hint 1

Sum with , and check which axis you are summing over.

Hint 2

Watch for this: mutates w in place so later components see a changed vector.

Requirements

  • : parameter vector, shape (n,)

  • eps: nudge size

  • Return array of shape (n,), the numerical gradient

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~15 min

4 employers weight this skill

4 quant funds. Top match scores 92.

Python
import numpy as np


def numerical_gradient(w, eps):
    """
    Central-difference gradient of L(w) = sum(w**2).

    Args:
        w:   parameter vector, shape (n,)
        eps: nudge size

    Returns:
        array of shape (n,), the numerical gradient
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.