I have a matrix W and two vectors y1 and y2. I want to extract rows from W. The rows I am interested in are in the range [y1:y2]. What is the best way of doing this in Theano? Can this be done without using theano.map or tensor.switch method? This obtained matrix will be used somewhere in grad computation. For e.g.:
W = [[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[ 13., 21., 33., 41.],
[ 55., 66., 74., 83.],
[ 92., 106., 711., 142.],
[ 19., 27., 33., 24.],
[ 54., 66., 74., 38.],
[ 29., 210., 131., 412.]]
y1 = [[0],
[0],
[6],
[3]]
y2 = [[3],
[3],
[9],
[6]]
I want w[y1:y2,:] ., i.e.
newW = [[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[ 19., 27., 33., 24.],
[ 54., 66., 74., 38.],
[ 29., 210., 131., 412.],
[ 13., 21., 33., 41.],
[ 55., 66., 74., 83.],
[ 92., 106., 711., 142.]]
So tell otherwise what you want is:
out=[]
for i,j in zip(y1,y2):
out.append(W[i:j])
numpy.asarray(out)
Is the lenght of y1 and y2 constant? If so, you can unroll the loop like this:
out=[]
for i in range(LEN):
out.append(W[y1[i]:y2[j]])
theano.stack(*out)
Theano support all the power of NumPy advanced indexing. If you can find how to do it with NumPy without the stack, you can do it the same way in Theano.