numpy 中 Float16 比 Float32 和 Float64 慢得多[重复]

2024-04-27

我试图运行一个代码片段,看起来像,

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,希望看到一些改进,但是float16float32 and float64.

X = np.random.rand(12000, 1200).astype('float16')
Y = np.random.rand(12000, 10).astype('float16')

将它们更改为float16结果执行时间为84.57 seconds, 然而float64 and float32被执行为36.27 seconds and 33.25 seconds分别。我不确定是什么导致了这种糟糕的表现flaot16。我的处理器是64 bit, using python3.7 and numpy-1.16.2。我认为 64 位处理器不会对所有 16 位、32 位和 64 位处理器无动于衷。非常感谢任何纠正和见解。


最可能的解释是您的处理器本身不支持 FP16 算法,因此这一切都是通过软件完成的,当然速度要慢得多。

一般来说,消费类英特尔处理器不支持 FP16 操作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

numpy 中 Float16 比 Float32 和 Float64 慢得多[重复] 的相关文章

随机推荐