[英]Dynamically indexing/choosing the dimension of numpy array
只是在CNN上工作並陷入張量算法。
我希望能夠遍歷維度的列表或元組,並從該維度中選擇X(多維數組)的元素范圍,而無需考慮其他維度。
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
np.take
應該適合您(請閱讀其文檔)
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)
你可以嘗試切片
arr1 = x[:,:,i:i+3,:]
和
arr2 = arr1[:,:,:,i:i+3]
形狀是
>>> x[:,:,i:i+3,:].shape
(10, 3, 3, 32)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.