[SciPy-User] Checking Gradients with Scipy

Pauli Virtanen pav at iki.fi
Sat Feb 23 08:45:42 EST 2013


23.02.2013 14:20, Paul Manta kirjoitti:
> 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?

You are here telling check_grad that you have a function of 4*5 = 20
variables, as `check_grad` deduces the number of variables from the size
of `x0`. `x0` needs to be a scalar here.

Note that `numpy.multiply(a, b)` can be written as `a*b`.

-- 
Pauli Virtanen




More information about the SciPy-User mailing list