Search code examples
numpyparallel-processinggpgputheano

Scan over every other element of a tensor variable in Theano


it should produce something like:

input = [1,2,3,4,5,6]
output = scan_every_other(lambda x:x, input)
// output should be [1,3,5]

I have briefly read the theano.scan tutorial but I didn't find what I'm looking for. Thank you.:)


Solution

  • You don't need to use theano.scan. Just use ordinary index/slice notation as in numpy:

    In numpy, if

    input = [1,2,3,4,5,6]
    

    then

    print input[::2]
    

    will display

    [1, 3, 5]
    

    In Theano, this can be achieved by doing the same thing:

    import theano
    import theano.tensor as tt
    input = tt.vector()
    f = theano.function([input], input[::2])
    print f([1,2,3,4,5,6])