title: theano.scan output gradient is different if calculation is done inside or outside scan
body: <p>So I have the following code</p>
  
  <pre><code>def step(i, rng, prev, A, B):
      a = A*prev
      b = B*a
      return b, a, a+rng
  
  rng = RandomStreams(lasagne.random.get_rng().randint(1,2147462579))
  
  A = T.scalar("A")
  B = T.scalar("B")
  
  outputs, _ = theano.scan(step, 
                           sequences=[T.arange(1,5),rng.normal([4])], 
                           outputs_info=[T.ones_like(A),None, None], 
                           non_sequences=[A,B])
  res, re, samp = outputs
  grad = T.grad(cost=re.sum(), wrt=A)
  func = theano.function([A,B],[grad])
  
  print func(3.0,2.0)
  </code></pre>
  
  <p>The code above gives and output of [array(985.0)], which is correct. 
  However, if I do the following gradient instead </p>
  
  <pre><code>grad = T.grad(cost=(re-samp).sum(), wrt=A, consider_constant=[samp])
  </code></pre>
  
  <p>the gradient comes out 0. 
  More interesting, if I do the <code>a+rng</code> step after outside of scan, i.e., using the <code>re</code> value to generate <code>samp</code>, the gradient comes out [array(985.0)] correctly again (when using <code>consider_constant=[samp]</code>, which is what I'm interested in).</p>
  
  <p>Any hint to why this is the case? I absolutely need to perform the random computation inside the scan, 
  so it would be nice to know if there's way to get the gradient working correctly in this circumstance.</p>
  
  <p>Thanks</p>