0

Just working on a CNN and am stuck on a tensor algorithm.

I want to be able to iterate through a list, or tuple, of dimensions and choose a range of elements of X (a multi dimensional array) from that dimension, while leaving the other dimensions alone.

x = np.random.random((10,3,32,32)) #some multi dimensional array
dims = [2,3] #aka the 32s

#for a dimension in dims
#I want the array of numbers from i:i+window in that dimension

#something like
arr1 = x.index(i:i+3,axis = dim[0]) 
#returns shape 10,3,3,32

arr2 = arr1.index(i:i+3,axis = dim[1]) 
#returns shape 10,3,3,3
2

2 Answers 2

1

np.take should work for you (read its docs)

In [237]: x=np.ones((10,3,32,32),int)
In [238]: dims=[2,3]
In [239]: arr1=x.take(range(1,1+3), axis=dims[0])
In [240]: arr1.shape
Out[240]: (10, 3, 3, 32)
In [241]: arr2=x.take(range(1,1+3), axis=dims[1])
In [242]: arr2.shape
Out[242]: (10, 3, 32, 3)
Sign up to request clarification or add additional context in comments.

Comments

-1

You can try slicing with

arr1 = x[:,:,i:i+3,:]

and

arr2 = arr1[:,:,:,i:i+3]

Shape is then

>>> x[:,:,i:i+3,:].shape
(10, 3, 3, 32)

2 Comments

What if one doesn't know the number of dimensions in advance?
Ah, right, solution in your comment on question above is much better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.