numpy高效矩阵自乘法(gram矩阵) [英] Numpy efficient matrix self-multiplication (gram matrix)

查看:186
本文介绍了numpy高效矩阵自乘法(gram矩阵)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将numpy中的B = A @ A.T乘以.显然,答案将是对称矩阵(即B[i, j] == B[j, i]).

I want to multiply B = A @ A.T in numpy. Obviously, the answer would be a symmetric matrix (i.e. B[i, j] == B[j, i]).

但是,我不清楚如何轻松地利用它来将计算时间减少一半(通过仅计算B的较低三角形,然后免费使用它来获取较高的三角形).

However, it is not clear to me how to leverage this easily to cut the computation time down in half (by only computing the lower triangle of B and then using that to get the upper triangle for free).

有没有一种方法可以最佳地执行此操作?

Is there a way to perform this optimally?

推荐答案

如@PaulPanzer的链接中所述,dot可以检测到这种情况.这是时间证明:

As noted in @PaulPanzer's link, dot can detect this case. Here's the timing proof:

In [355]: A = np.random.rand(1000,1000)
In [356]: timeit A.dot(A.T)
57.4 ms ± 960 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [357]: B = A.T.copy()
In [358]: timeit A.dot(B)
98.6 ms ± 805 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

对对称乘法过于聪明的数字点

这篇关于numpy高效矩阵自乘法(gram矩阵)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆