[SciPy-User] Checking Gradients with Scipy

Paul Manta paulc.mnt@gmail....
Sat Feb 23 06:20:48 CST 2013


I want to use `scipy.optimize.check_grad` to check the gradient of my 
implementation of the sigmoid function; here's my Python function:

> def sigmoid(x, gradient=False):
>     y = 1 / (1 + numpy.exp(-x))
>     return numpy.multiply(y, 1 - y) if gradient else y


Here are the arguments and the call to `check_grad`:

> x0 = numpy.random.uniform(-30, 30, (4, 5))
> func = sigmoid
> grad = lambda x: sigmoid(x, gradient=True)
> error = scipy.optimize.check_grad(func, grad, x0)

I get the error below. The shape mismatch refers to the subtraction `f(*
((xk+d,)+args)) - f0`. Any idea what could be causing this and how I should fix 
it?

> File "scipy\optimize\optimize.py", line 597, in approx_fprime  
>    grad[k] = (f(*((xk+d,)+args)) - f0) / d[k]  
> ValueError: operands could not be broadcast together with shapes (4,5) (4)



More information about the SciPy-User mailing list