You could use advanced indexing where you create the index you want to use and they will be copied over. I think this would be more performant then using scan.
On Wed, Aug 17, 2016 at 11:44 AM, Franck Dernoncourt < [email protected]> wrote: > I couldn't find any dedicated function for that purpose, so I simply ended > up using theano.scan > <http://deeplearning.net/software/theano/tutorial/loop.html>: > > import theanoimport theano.tensor > from theano import sharedimport numpy as np > > y = shared(np.array([2,1])) > x = > shared(np.arange(24).reshape((2,3,4)))print('x.eval():\n{0}\n'.format(x.eval())) > def shift_and_reverse_row(matrix, y): > ''' > Shift and reverse the matrix in the direction of the first dimension > (i.e., rows) > matrix: matrix > y: scalar > ''' > new_matrix = theano.tensor.zeros_like(matrix) > new_matrix = theano.tensor.set_subtensor(new_matrix[:y,:], > matrix[y-1::-1,:]) > return new_matrix > > new_x, updates = theano.scan(shift_and_reverse_row, outputs_info=None, > sequences=[x, y[::-1]] ) > new_x = new_x[:, ::-1, :]print('new_x.eval(): \n{0}'.format(new_x.eval())) > > output: > > x.eval():[[[ 0 1 2 3] > [ 4 5 6 7] > [ 8 9 10 11]] > > [[12 13 14 15] > [16 17 18 19] > [20 21 22 23]]] > > new_x.eval():[[[ 0 0 0 0] > [ 0 0 0 0] > [ 0 1 2 3]] > > [[ 0 0 0 0] > [12 13 14 15] > [16 17 18 19]]] > > > > > > > On 15 August 2016 at 12:23, Franck Dernoncourt < > [email protected]> wrote: > >> Hi all, >> >> >> I have a Theano tensor3 (i.e., a 3-dimensional array) x: >> >> >> [[[ 0 1 2 3] >> [ 4 5 6 7] >> [ 8 9 10 11]] >> >> [[12 13 14 15] >> [16 17 18 19] >> [20 21 22 23]]] >> >> >> as well as a Theano vector (i.e., a 1-dimensional array) y, which we >> will refer as an "offset" vector, since it specifies the desired offset: >> >> [2, 1] >> >> >> I want to shift the location of elements of x based on vector y, so that >> the output be as follows (the shift is performed on the second dimension): >> >> [[[ a b c d] >> [ e f g h] >> [ 0 1 2 3]] >> >> [[ i j k l] >> [12 13 14 15] >> [16 17 18 19]]] >> >> where the a, b, …, l could be any number. >> >> >> For example, a valid output could be: >> >> [[[ 0 0 0 0] >> [ 0 0 0 0] >> [ 0 1 2 3]] >> >> [[ 0 0 0 0] >> [12 13 14 15] >> [16 17 18 19]]] >> >> >> Another valid output could be: >> >> [[[ 4 5 6 7] >> [ 8 9 10 11] >> [ 0 1 2 3]] >> >> [[20 21 22 23] >> [12 13 14 15] >> [16 17 18 19]]] >> >> >> >> >> ------------------------------ >> >> >> I am aware of the function theano.tensor.roll(x, shift, axis=None) >> <http://deeplearning.net/software/theano/library/tensor/basic.html#theano.tensor.roll>, >> however the shift can only take a scalar as input, i.e. it shifts all >> elements with the same offset. >> >> >> E.g., the code: >> >> import theano.tensorfrom theano import sharedimport numpy as np >> >> x = shared(np.arange(24).reshape((2,3,4)))print('theano.tensor.roll(x, 2, >> axis=1).eval(): \n{0}'. >> format(theano.tensor.roll(x, 2, axis=1).eval())) >> >> >> outputs: >> >> theano.tensor.roll(x, 2, axis=1).eval():[[[ 4 5 6 7] >> [ 8 9 10 11] >> [ 0 1 2 3]] >> >> [[16 17 18 19] >> [20 21 22 23] >> [12 13 14 15]]] >> >> >> which is not what I want. >> >> >> How can I shift the location of tensor3 elements based on an offset >> vector? (note that in the code provided in this example, the tensor3 is >> a shared variable for convenience, but in my actual code it will be a >> symbolic variable) >> >> >> Thanks, >> >> Franck >> >> -- >> >> --- >> You received this message because you are subscribed to a topic in the >> Google Groups "theano-users" group. >> To unsubscribe from this topic, visit https://groups.google.com/d/to >> pic/theano-users/sl1K1LjZ9QY/unsubscribe. >> To unsubscribe from this group and all its topics, send an email to >> [email protected]. >> For more options, visit https://groups.google.com/d/optout. >> > > -- > > --- > You received this message because you are subscribed to the Google Groups > "theano-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- --- You received this message because you are subscribed to the Google Groups "theano-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
