2

Is it possible to pass the iterating index of scan to the function I am calling from scan? For eg -

def step(x,i):
   # i is the current scan index. Use it for some conditional expressions

for i in range(0,10):
    step(x,i)

I want to do something similar using theano. Any clues?

Thanks

dragster
  • 448
  • 1
  • 4
  • 20

1 Answers1

2

This is shown in the theano tutorials, for example here.

The first argument of the function is automatically taken from the sequences argument of scan, if provided. For example, suppose you want to add to every element of a vector x its corresponding index. You could do it with theano.scan like this:

import theano
import theano.tensor as T
x = T.dvector('x')
def step(idx, array):
    return array[idx] + idx
results, updates = theano.scan(fn=step,
                               sequences=T.arange(x.shape[0]),
                               non_sequences=[x])
f = theano.function([x], results)
f([1., 0., 2.13])
# array([ 1.  ,  1.  ,  4.13])

so basically: mind the order of the arguments of the function you give to scan, as they are passed in a specific order. You can see exactly what order in the relevant documentation page of scan.

glS
  • 1,209
  • 6
  • 18
  • 45
  • thank you for the reply. That works! I next want to compare this idx variable to a 'int' variable and based on that return something. But, here idx is a tensor variable. Any clues on how can I do the comparison. Simply doing `if T.lt(idx,seqLen-1):` always returns true. May be it is comparing the first index value only everytime. Any idea on how to do this? – dragster Dec 19 '16 at 14:48
  • I'm not sure what you are asking. If it is not related to this problem you should open another question for it – glS Dec 19 '16 at 17:02