np.log2
而不是 math.log2
。例如,在此 PyTorch 代码中:
num_pools = int(np.log2(spatial))
(其中
spatial
是 Python 数字)
math.log2
是np.log2
- 可能是遵循惯例,还是因为np.log2
被认为更快?
如果我们有 numpy 数组,那么应该使用
np.log2
,因为它适用于数组,而 math.log2
则不行。如果我们有一个标量,math.log2
速度更快,因此是首选。至于为什么人们使用 np.log2
来表示标量,我只能推测。这很可能是因为他们已经导入了它,并且不关心通过使用 math.log2
获得的微小速度改进。