简体   繁体   English

如何在 Python 中以最快的方式计算矩阵指数?

[英]How can I calculate matrix exponential in the fastest way in Python?

I would like to calculate the matrix exponential in Python.我想用 Python 计算矩阵指数。 I found one way ("scipy.linalg.expm()"), but it takes long time (eg 30[sec] for 5000×5000 matrix).我找到了一种方法(“scipy.linalg.expm()”),但需要很长时间(例如 5000×5000 矩阵需要 30[sec])。

matrix_exponential = scipy.linalg.expm(matrix)

Is there any faster way to calculate matrix exponential in Python?有没有更快的方法来计算 Python 中的矩阵指数?

Thank you very much in advance.非常感谢您提前。

Not sure if this is too late and I hope you have found out the answer.不知道这是否为时已晚,我希望你已经找到了答案。

I was facing the same issue while trying to solve the Stochastic Liouville equation in physics with large matrix sizes.我在尝试用大矩阵求解物理学中的随机刘维尔方程时遇到了同样的问题。 It seems to me that the scipy implementation running on CPU is the fastest method.在我看来,在 CPU 上运行的 scipy 实现是最快的方法。

There are two readily available packages I have found so far.到目前为止,我发现了两个现成的软件包。 One is the scipy linalg library (scipy.linalg.expm(A)) and the other is tensorflow linalg library (tf.linalg.expm(A)).一个是 scipy linalg 库(scipy.linalg.expm(A)),另一个是 tensorflow linalg 库(tf.linalg.expm(A))。 Tensorflow will use GPUs to compute if appropriate cuda libraries have been installed. Tensorflow 将使用 GPU 来计算是否安装了适当的 cuda 库。 Both libraries can be incorporated without drastically changing the code structure.这两个库都可以在不彻底改变代码结构的情况下合并。 The tensorflow package requires converting between numpy arrays and tensorflow arrays. tensorflow 包需要在 numpy 数组和 tensorflow 数组之间进行转换。 These operations involve copying data between system RAM and GPU memory that tend to be expensive.这些操作涉及在系统 RAM 和 GPU 内存之间复制数据,而这往往很昂贵。

For square matrix with size below 12393, it took the same time to run expm() function for both packages, but the tensorflow code took a lot longer IO time when converting between numpy and tf arrays and is overall slower.对于大小小于 12393 的方阵,两个包运行 expm() 函数花费的时间相同,但 tensorflow 代码在 numpy 和 tf 数组之间转换时花费了更长的 IO 时间,并且总体上更慢。

CuPy also provides GPU computation capacity, however there is no readily available methods to run matrix exponentiation. CuPy 还提供 GPU 计算能力,但是没有现成的方法来运行矩阵求幂。 Matrix inversion is also faster running on CPU with numpy compared to on GPU with cupy.与在 GPU 上使用cupy 相比,矩阵求逆在使用numpy 的CPU 上运行速度也更快。

The above tests were run on a linux machine with Intel® Core™ i5-9400F CPU @ 2.90GHz × 6 and GeForce GTX 1050 Ti/PCIe/SSE2, using sparse matrix with sizes between 243 and 12393, based on 128-bit numpy complex numbers.上述测试是在 linux 机器上运行的,该机器具有 Intel® Core™ i5-9400F CPU @ 2.90GHz × 6 和 GeForce GTX 1050 Ti/PCIe/SSE2,使用大小介于 243 和 12393 之间的稀疏矩阵,基于 128 位 numpy 复杂数字。 Exponentiation of a 5000x5000 square matrix (64-bit float) takes 22 seconds on 6 cores, using scipy.linalg.expm(A).使用 scipy.linalg.expm(A) 在 6 个内核上对 5000x5000 方阵(64 位浮点数)求幂需要 22 秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM