2016-04-27 11 views
0

私の質問は3次元を除いて、 Indexing tensor with index matrix in theano? と非常によく似ています。最初はそれをnumpyで動作させたい。 2次元の場合、問題はありません。高度な3d indexind in theano

>>> idx = np.random.randint(3, size=(4, 2, 3)) 
>>> d = np.random.rand(4*2*3).reshape((4, 2, 3)) 
>>> d[1] 
array([[ 0.37057415, 0.73066383, 0.76399376], 
     [ 0.12155831, 0.12552545, 0.87648523]]) 
>>> idx[1] 
array([[2, 0, 1], 
     [2, 2, 2]]) 
>>> d[1][np.arange(d.shape[1])[:, np.newaxis], idx[1]] 
array([[ 0.76399376, 0.37057415, 0.73066383], 
     [ 0.87648523, 0.87648523, 0.87648523]]) #All correct 

しかし、3次元すべてでどのように動作させるかわかりません。試行失敗の例:

>>> d[np.arange(d.shape[0])[:, np.newaxis], np.arange(d.shape[1]), idx] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (4,1) (2,) (4,2,3) 

答えて

0

これは機能しますか?あなたは、インデックスの配列をまとめbroadcastable寸法

+0

を持っている必要があります

d[ np.arange(d.shape[0])[:, np.newaxis, np.newaxis], np.arange(d.shape[1])[:, np.newaxis], idx ] 

はそれは正しいです、ありがとうございます。どのように動作するのか理解しようと約束します) –