The gradient (2d) search function returns None. What is the problem?

Tags: , , ,



I have a function that should return a gradient.

def numerical_derivative_2d(func, epsilon):
    def grad_func(x):
        grad_func = (func(x + np.array([epsilon, 0])) - func(x)) / epsilon, (func(x + np.array([0, epsilon])) - func(x)) / epsilon
    return grad_func

But when calculating the gradient at a specific point, I get None.

t1 = lambda x: (
            -1 / ((x[0] - 1)**2 + (x[1] - 1.5)**2 + 1)
            * np.cos(2 * (x[0] - 1)**2 + 2 * (x[1] - 1.5)**2))
t2 = numerical_derivative_2d(t, 0.1)
t3 = t2([3, 4])

Object classes: t1-function, t2-function, t3-None (I want to get a two-dimensional point)

Please tell me how to change this line

    grad_func = (func(x + np.array([epsilon, 0])) - func(x)) / epsilon, (func(x + np.array([0, epsilon])) - func(x)) / epsilon

to make it work?

PS. I understand that there are built-in methods for calculating the gradient. But this is a homework assignment in one of the courses. I would like to understand the error and do it according to the template.

PPS.This may be unnecessary, but I will give an example of a task for a single variable function that works as I expect.

def numerical_derivative_1d(func, epsilon):
    def deriv_func(x):
        return (func(x + epsilon) - func(x)) / epsilon      
    return deriv_func

def polynom_to_prime(x):
    return 20 * x**5 + x**3 - 5 * x**2 + 2 * x + 2.0

t2 = numerical_derivative_1d(polynom_to_prime, 1e-5)
t3 = t2(3)

and t3 is a number (dot), not None

Answer

In def grad_func(x), you don’t return anything. Inside that function, grad_func is a local variable. Consider writing that like:

def numerical_derivative_2d(func, epsilon):
    def grad_func(x):
        return (func(x + np.array([epsilon, 0])) - func(x)) / epsilon, (func(x + np.array([0, epsilon])) - func(x)) / epsilon
    return grad_func


Source: stackoverflow