【问题标题】:Float16 is much slower than Float32 and Float64 in numpy [duplicate]Float16 在 numpy 中比 Float32 和 Float64 慢得多 [重复]
【发布时间】: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,希望看到一些改进,但是float16float32float64慢很多。

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

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

【问题讨论】:

    标签: python performance numpy


    【解决方案1】:

    这是因为 c 中没有对应的 float16。

    由于python是基于c的,因为在c中没有等效的,numpy创建了一个方法来执行float16。

    (float是一个32位IEEE 754单精度浮点数,符号1位,(指数8位,值23*),即float有7位十进制精度)

    因此(相当于在 float16 上工作的过程)float16float32float64

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2018-03-24
        • 2019-10-24
        • 1970-01-01
        • 1970-01-01
        • 2017-10-21
        • 2022-08-13
        • 2016-11-16
        • 2019-08-17
        • 1970-01-01
        相关资源
        最近更新 更多