【发布时间】:2019-11-03 22:40:01
【问题描述】:
我试图运行一个看起来像的代码 sn-p,
import numpy as np
import time
def estimate_mutual_info(X, neurons, bins = 5):
xy = np.histogram2d(X, neurons, bins)[0]
x = np.histogram(X, bins)[0]
y = np.histogram(neurons, bins)[0]
ent_x = -1 * np.sum( x / np.sum(x) * np.log( x / np.sum(x)))
ent_y = -1 * np.sum( y / np.sum(y) * np.log( y / np.sum(y)))
ent_xy = -1 * np.sum( xy / np.sum(xy) * np.log( xy / np.sum(xy)))
return (ent_x + ent_y - ent_xy)
tic = time.time()
X = np.random.rand(12000, 1200)
Y = np.random.rand(12000, 10)
for j in Y.T:
mi = 0
for i in range(X.shape[1]):
mi += estimate_mutual_info(X.T[i], j, bins = 2)
print(mi)
toc = time.time()
print(str(toc - tic)+" seconds")
为了提高速度,我使用了float16,希望看到一些改进,但是float16比float32和float64慢很多。
X = np.random.rand(12000, 1200).astype('float16')
Y = np.random.rand(12000, 10).astype('float16')
将它们更改为float16 会导致84.57 seconds 的执行时间,而float64 和float32 分别为36.27 seconds 和33.25 seconds 执行。我不确定,是什么导致flaot16 表现不佳。我的处理器是64 bit,使用python3.7 和numpy-1.16.2。我不认为 64 位处理器对所有 16 位、32 位和 64 位都无动于衷。任何更正和洞察力都非常感谢。
【问题讨论】:
标签: python performance numpy