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
Alexander Telfar
  • 559
  • 1
  • 6
  • 7

2 Answers2

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)
hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

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)
hruske
  • 2,205
  • 19
  • 27