Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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
Python3如何在numpy linalg norm中使用多线程_Python_Python 3.x_Multithreading_Numpy_Linear Algebra - Fatal编程技术网

Python3如何在numpy linalg norm中使用多线程

Python3如何在numpy linalg norm中使用多线程,python,python-3.x,multithreading,numpy,linear-algebra,Python,Python 3.x,Multithreading,Numpy,Linear Algebra,我使用python3 with来计算矩阵normaxis=1中的行的范数,是否有一种简单的方法,仅使用np使其使用多线程或多线程运行 我们可以使用支持多核处理的- import numexpr as ne def linalg_norm(a): sq_norm = ne.evaluate('sum(a**2,1)') return ne.evaluate('sqrt(sq_norm)') 若要沿任何其他轴执行范数缩减,请在求值表达式“suma**2,1”中将1替换为该轴编号

我使用python3 with来计算矩阵normaxis=1中的行的范数,是否有一种简单的方法,仅使用np使其使用多线程或多线程运行

我们可以使用支持多核处理的-

import numexpr as ne

def linalg_norm(a):
    sq_norm = ne.evaluate('sum(a**2,1)')
    return ne.evaluate('sqrt(sq_norm)')
若要沿任何其他轴执行范数缩减,请在求值表达式“suma**2,1”中将1替换为该轴编号

样本运行-

In [34]: np.random.seed(0)
    ...: a = np.random.rand(4,5)

In [35]: np.linalg.norm(a,axis=1)
Out[35]: array([1.28545589, 1.57467272, 1.4460319 , 1.43656019])

In [36]: linalg_norm(a)
Out[36]: array([1.28545589, 1.57467272, 1.4460319 , 1.43656019])
关于如何控制多核功能

为了完整性,可以提出的备选方案很少

一个有效的解决方案是使用np.einsum-

在Python3.x上使用np.matmul/@运算符-


我想您可以尝试使用numba-lib支持请求的方法。这是一个可靠的想法,但我的目标是一个纯粹的numpy,它在很多地方都是多线程的。@如果你正在寻找一个内置的numpy,则没有任何AFAIK。
In [39]: np.sqrt(np.einsum('ij,ij->i',a, a))
Out[39]: array([1.28545589, 1.57467272, 1.4460319 , 1.43656019])
In [6]: np.sqrt(np.matmul(a[:,None],a[:,:,None])[:,0,0])
Out[6]: array([1.28545589, 1.57467272, 1.4460319 , 1.43656019])

In [7]: np.sqrt((a[:,None] @ a[:,:,None])[:,0,0])
Out[7]: array([1.28545589, 1.57467272, 1.4460319 , 1.43656019])