我想通过屏蔽一些我知道在计算中不需要的值来优化Python中的矩阵乘法(加权回归)。它们仍然存在,因为我不想改变矩阵的大小。矩阵是浮点数。
Python (keras/tensorflow?)以不同的方式处理这些乘法,并显著加快过程,或者它将花费类似的时间,使这种掩蔽毫无意义?
发布于 2019-03-13 14:17:08
不,乘以零与乘以任何其他数字相同
>>> def times_zero(x):
... return x * 0
...
>>> import dis
>>> dis.dis(times_zero)
2 0 LOAD_FAST 0 (x)
3 LOAD_CONST 1 (0)
6 BINARY_MULTIPLY
7 RETURN_VALUE
>>> def times_four(x):
... return x * 4
...
>>> dis.dis(times_four)
2 0 LOAD_FAST 0 (x)
3 LOAD_CONST 1 (4)
6 BINARY_MULTIPLY
7 RETURN_VALUE
发布于 2019-03-13 14:20:54
我给他们计时:
from timeit import default_timer as timer
import itertools
my_toggle = itertools.cycle(range(2))
for x in range(20):
current_number = my_toggle.__next__()
start = timer()
y = 1 * current_number
end = timer()
print(f"{end - start:.10f} seconds for {current_number}")
但我不确定结果是什么:
0.0000002555 seconds for 0
0.0000002555 seconds for 1
0.0000002555 seconds for 0
0.0000002555 seconds for 1
0.0000002555 seconds for 0
0.0000000000 seconds for 1
0.0000000000 seconds for 0
0.0000002555 seconds for 1
0.0000000000 seconds for 0
0.0000000000 seconds for 1
0.0000000000 seconds for 0
0.0000002555 seconds for 1
0.0000002555 seconds for 0
0.0000000000 seconds for 1
0.0000002555 seconds for 0
0.0000000000 seconds for 1
0.0000000000 seconds for 0
0.0000002555 seconds for 1
0.0000000000 seconds for 0
0.0000000000 seconds for 1
https://stackoverflow.com/questions/55143968
复制相似问题