title: Theano Tutorial Computing the Jacobian with Scan
body: <p>In the Theano Tutorial: </p>
  
  <p><a href="http://deeplearning.net/software/theano/tutorial/gradients.html" rel="nofollow">http://deeplearning.net/software/theano/tutorial/gradients.html</a></p>
  
  <p>or </p>
  
  <pre><code>&gt;&gt;&gt; import theano
  &gt;&gt;&gt; import theano.tensor as T
  &gt;&gt;&gt; x = T.dvector('x')
  &gt;&gt;&gt; y = x ** 2
  &gt;&gt;&gt; J, updates = theano.scan(lambda i, y,x : T.grad(y[i], x), sequences=T.arange(y.shape[0]), non_sequences=[y,x])
  &gt;&gt;&gt; f = theano.function([x], J, updates=updates)
  &gt;&gt;&gt; f([4, 4])
  array([[ 8.,  0.],
         [ 0.,  8.]])
  </code></pre>
  
  <p>"theano.scan" is used to compute the Jacobian. I understand how to compute the Jacobian by hand but I do not quite understand how the function scan is calculating it iteratively; specifically, I don't understand how the "zeroes" are obtained in the Jacobian.</p>
  
  <p>Can someone show me, for each iteration, how this is calculated? Thanks!</p>