对比浮点数矩阵运算测试。
a = np.array([
[
[0.12,0.23,0.34,0.45],
[0.56,0.67,0.78,0.89],
[0.91,0.1011,0.1112,0.1213],
[0.1314,0.1415,0.1516,0.1617]
],
[
[0.12,0.23,0.34,0.45],
[0.56,0.67,0.78,0.89],
[0.91,0.1011,0.1112,0.1213],
[0.1314,0.1415,0.1516,0.1617]
],
[
[0.12,0.23,0.34,0.45],
[0.56,0.67,0.78,0.89],
[0.91,0.1011,0.1112,0.1213],
[0.1314,0.1415,0.1516,0.1617]
],
[
[0.12,0.23,0.34,0.45],
[0.56,0.67,0.78,0.89],
[0.91,0.1011,0.1112,0.1213],
[0.1314,0.1415,0.1516,0.1617]
],
])
b = np.array([
[
[0.12,0.23,0.34,0.45],
[0.56,0.67,0.78,0.89],
[0.91,0.1011,0.1112,0.1213],
[0.1314,0.1415,0.1516,0.1617]
],
[
[0.12,0.23,0.34,0.45],
[0.56,0.67,0.78,0.89],
[0.91,0.1011,0.1112,0.1213],
[0.1314,0.1415,0.1516,0.1617]
],
[
[0.12,0.23,0.34,0.45],
[0.56,0.67,0.78,0.89],
[0.91,0.1011,0.1112,0.1213],
[0.1314,0.1415,0.1516,0.1617]
],
[
[0.12,0.23,0.34,0.45],
[0.56,0.67,0.78,0.89],
[0.91,0.1011,0.1112,0.1213],
[0.1314,0.1415,0.1516,0.1617]
],
])
A: c = a * b
B: c = np.matmul(a,b)
C: c = a @ b
测试下来耗时 A < B < C ???
这是啥情况,原生*运算比 numpy 优化过的速度还快???
1
silkriver 2019-03-25 10:30:45 +08:00 1
你这全都是 numpy 优化过的速度…… ufunc 了解一下
* 和 np.multiply 等价 @ 和 np.matmul 等价 |
2
enenaaa 2019-03-25 10:39:43 +08:00 1
一看就是没学过 c++的, 重载运算符啊。
|
3
SeaRecluse OP @silkriver 另一个问题是,在进行如下操作时:
a = a * 1000 b = b * 1000 α:c = a * b / 1000000 β:c = a @ b /1000000 为什么 time(A) < time(α),time(C) > time(β)呢😂? 其实本来是想验证浮点数计算的优化的,后者才是正式表达的矩阵乘法运算吧~ |
4
SeaRecluse OP @enenaaa 你这样让我的写的第一个 cout << "Hello World" << endl; 很尴尬😂
|
5
silhouette 2019-03-25 11:11:19 +08:00 via Android
兄弟,运算符重载了解一下
|
6
silkriver 2019-03-25 12:28:55 +08:00
这很正常因为矩阵乘法运算量更大
|
7
menc 2019-03-25 17:14:17 +08:00
@SeaRecluse 写过 CPP 还不知道重载运算符不是更尴尬么。。
|
8
SeaRecluse OP @menc 知道啊,不过写动态语言写多了完全忽略掉了 [捂脸
|
9
menc 2019-03-26 15:46:13 +08:00
@SeaRecluse python 的运算符重载也是很常用的东西
|
10
necomancer 2019-03-29 21:47:35 +08:00
* 不是矩阵乘法,计算量不一样。
In [1]: ...: a = random.random((1000,1000)) In [2]: ...: b = random.random((1000,1000)) In [3]: np.allclose(a@b, np.matmul(a,b)) Out[3]: True In [4]: np.allclose(a*b, np.matmul(a,b)) Out[4]: False In [5]: %timeit a@b 34.7 ms ± 1.91 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) In [6]: %timeit np.matmul(a,b) 34.6 ms ± 1.14 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) In [7]: np.__version__ Out[7]: '1.15.1' |