How to check that my analytical gradient computation is correct?

68 Views Asked by At

I have a rather difficult multivariable function $f(x)$ with $x \in \mathbb{R}^N$. I computed (paper and pen) what I think is the gradient of the function w.r.t. $x$. However, I want to check if the computation is correct. Which are some ways to check it? For instance software/toolboxes?

What I would like is to to define my function in a object-oriented programming way and let the software give me the gradient in symbolic terms. Notice that my function is highly non-linear, and the variable appears with element-wise powers and other reshaping operations, so I am not sure whether a software would be actually able to recognize all the function dependencies on the variable.

1

There are 1 best solutions below

0
On

You can also use finite difference to check your analytical gradient It holds $$ \frac{\partial f}{\partial x_n} \simeq \frac{\partial f(\mathbf{x}+h\mathbf{e}_n)-f(\mathbf{x}-h\mathbf{e}_n)}{2h} $$ where $\mathbf{e}_n$ is the null vecor except at the $n$-th position.