我试着运行一个代码片段,看起来
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
,希望看到一些改进,但是{float32
和{
将它们更改为float16
将导致84.57 seconds
的执行时间,而float64
和{flaot16
的性能下降。我的处理器是64 bit
,使用python3.7
和{
之所以发生这种情况,是因为在c中没有float16的等价物
由于python是基于c的,因为在c中没有等价的方法,numpy创建了一个方法来执行float16。在
(float是32位IEEE 754单精度浮点数字,符号1位(指数为8位,值为23*),即浮点有7位十进制数字)
因为这个过程(相当于float16上的工作)}慢
float16
比float32
或{最有可能的解释是,你的处理器本身并不支持FP16算法,所以这一切都是在软件中完成的,当然,这要慢得多。在
一般来说,消费者英特尔处理器不支持FP16操作。在
相关问题 更多 >
编程相关推荐