在 numpy 中,Float16 比 Float32 和 Float64 慢得多

Vig*_*n C 5 python performance numpy

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

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")
Run Code Online (Sandbox Code Playgroud)

为了提高速度,我使用了float16,希望看到一些改进,但float16float32和慢得多float64

X = np.random.rand(12000, 1200).astype('float16')
Y = np.random.rand(12000, 10).astype('float16')
Run Code Online (Sandbox Code Playgroud)

它们改变到float16导致的执行时间84.57 seconds,而float64float32用于执行36.27 seconds33.25 seconds分别。我不确定,是什么导致flaot16. 我的处理器正在64 bit使用python3.7numpy-1.16.2。我不认为 64 位处理器对所有 16 位、32 位和 64 位都无动于衷。任何更正和洞察力都非常感谢。

gmd*_*mds 10

最可能的解释是您的处理器本身并不支持 FP16 算术,所以这一切都是在软件中完成的,当然,这要慢得多。

通常,消费类英特尔处理器不支持 FP16 操作。