Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在高阶张量中找到第一匹配张量的索引_Python_Numpy_Machine Learning_Tensorflow - Fatal编程技术网

Python 如何在高阶张量中找到第一匹配张量的索引

Python 如何在高阶张量中找到第一匹配张量的索引,python,numpy,machine-learning,tensorflow,Python,Numpy,Machine Learning,Tensorflow,很像这个问题 我试着解决这个问题 但区别在于val不是一个单一的数字,而是一个张量 以身作则 np.array([1, 1, 1], [1, 0, 1], [0, 0, 1]) val = np.array([1, 0, 1]) some tensorflow magic happens here! result = 1 我知道我可以使用一个while循环,但这看起来很混乱。 我可以尝试一个映射函数,但还有更优雅的吗?这里有一种方法- (arr ==

很像这个问题

我试着解决这个问题 但区别在于val不是一个单一的数字,而是一个张量

以身作则

np.array([1, 1, 1],
         [1, 0, 1],
         [0, 0, 1])
val = np.array([1, 0, 1])


some tensorflow magic happens here!

result = 1
我知道我可以使用一个while循环,但这看起来很混乱。 我可以尝试一个映射函数,但还有更优雅的吗?

这里有一种方法-

(arr == val).all(axis=-1).argmax()
样本运行-

In [977]: arr
Out[977]: 
array([[1, 1, 1],
       [1, 0, 1],
       [0, 0, 1]])

In [978]: val
Out[978]: array([1, 0, 1])

In [979]: (arr == val).all(axis=1).argmax()
Out[979]: 1
使用
视图可能会更有效
-

# https://stackoverflow.com/a/44999009/ @Divakar
def view1D(a): # a is array
    a = np.ascontiguousarray(a)
    void_dt = np.dtype((np.void, a.dtype.itemsize * a.shape[1]))
    return a.view(void_dt).ravel()

out = (view1D(arr) == view1D(val[None])).argmax()
扩展到n-dim案例

扩展到n-dim阵列情况将需要更多的步骤-

def first_match_index_along_axis(arr, val, axis):    
    s = [None]*arr.ndim
    s[axis] = Ellipsis
    mask = val[np.s_[s]] == arr
    idx = mask.all(axis=axis,keepdims=True).argmax()
    shp = list(arr.shape)
    del shp[axis]
    return np.unravel_index(idx, shp)
样本运行-

In [74]: arr = np.random.randint(0,9,(4,5,6,7))

In [75]: first_match_index_along_axis(arr, arr[2,:,1,0], axis=1)
Out[75]: (2, 1, 0)

In [76]: first_match_index_along_axis(arr, arr[2,1,3,:], axis=3)
Out[76]: (2, 1, 3)
这里有一条路-

(arr == val).all(axis=-1).argmax()
样本运行-

In [977]: arr
Out[977]: 
array([[1, 1, 1],
       [1, 0, 1],
       [0, 0, 1]])

In [978]: val
Out[978]: array([1, 0, 1])

In [979]: (arr == val).all(axis=1).argmax()
Out[979]: 1
使用
视图可能会更有效
-

# https://stackoverflow.com/a/44999009/ @Divakar
def view1D(a): # a is array
    a = np.ascontiguousarray(a)
    void_dt = np.dtype((np.void, a.dtype.itemsize * a.shape[1]))
    return a.view(void_dt).ravel()

out = (view1D(arr) == view1D(val[None])).argmax()
扩展到n-dim案例

扩展到n-dim阵列情况将需要更多的步骤-

def first_match_index_along_axis(arr, val, axis):    
    s = [None]*arr.ndim
    s[axis] = Ellipsis
    mask = val[np.s_[s]] == arr
    idx = mask.all(axis=axis,keepdims=True).argmax()
    shp = list(arr.shape)
    del shp[axis]
    return np.unravel_index(idx, shp)
样本运行-

In [74]: arr = np.random.randint(0,9,(4,5,6,7))

In [75]: first_match_index_along_axis(arr, arr[2,:,1,0], axis=1)
Out[75]: (2, 1, 0)

In [76]: first_match_index_along_axis(arr, arr[2,1,3,:], axis=3)
Out[76]: (2, 1, 3)

你总是在二维张量中搜索一维张量吗?或者它需要推广到nD吗?总是2D中的1D,但推广后的解决方案对你有用吗?考虑接受如果它did。不,它没有我最终没有一个解决方案是性能。噢,对不起,我的意思是说,接受的解决方案是一个我尝试。但是我的目标实现得非常慢,所以我最终完全放弃了这种方法你总是在二维张量中寻找一维张量吗?或者它需要推广到nD吗?总是2D中的1D,但推广后的解决方案对你有用吗?考虑接受如果它did。不,它没有我最终没有一个解决方案是性能。噢,对不起,我的意思是说,接受的解决方案是一个我尝试。但它最终对我的目标来说非常缓慢,所以我最终完全放弃了这种方法或使用TensorFlow,Numpy one liner将是
tf.argmax(tf.cast(tf.reduce_all(tf.equal(arr,val),axis=1),tf.int32))
(解决==未重载的事实,.all未实现,argmax不在bools上工作)。请注意,这两个版本都有些危险,因为如果找不到模式,它们将返回0。@AllenLavoie如果找不到,我们可以使用:
np.where
,如本文所述-。或者使用TensorFlow,Numpy一行将是
tf.argmax(tf.cast(tf.reduce_all(tf.equal(arr,val),axis=1),tf.int32))
(解决==未重载的问题,.all未实现,argmax在bools上不工作)。请注意,这两个版本都有些危险,因为如果找不到模式,它们将返回0。@AllenLavoie如果找不到,我们可以使用:
np.where
,如本文所述-。