title: Checking Gradients with Scipy
body: <p>I want to use <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.check_grad.html" rel="nofollow"><code>scipy.optimize.check_grad</code></a> to check the gradient of my implementation of the <a href="http://mathworld.wolfram.com/SigmoidFunction.html" rel="nofollow">sigmoid function</a>; here's my Python function:</p>
  
  <pre><code>def sigmoid(x, gradient=False):
      y = 1 / (1 + numpy.exp(-x))
      return numpy.multiply(y, 1 - y) if gradient else y
  </code></pre>
  
  <p>Here are the arguments and the call to <code>check_grad</code>:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I get the error below. The shape mismatch refers to the operation <code>xk+d</code>. Any idea what could be causing this?</p>
  
  <blockquote>
    <p>File "scipy\optimize\optimize.py", line 597, in approx_fprime<br>
       grad[k] = (f(*((xk+d,)+args)) - f0) / d[k]<br>
    ValueError: operands could not be broadcast together with shapes (4,5) (4)</p>
  </blockquote>