numpy 的 float16 数据类型是否功能失调? [英] Is float16 datatype for numpy disfunctional?

查看:84
本文介绍了numpy 的 float16 数据类型是否功能失调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个主要问题的例子:

c=np.array([2600.0])

In [3]: c=c.astype(np.float16)

In [4]: c

Out[4]: array([ 2600.], dtype=float16)

一切都好,那么:

In [5]: c=np.array([2583.0])

In [6]: c=c.astype(np.float16)

In [7]: c

Out[7]: array([ 2584.], dtype=float16)

所以2583.0变成了2584.0,很酷,不会造成任何问题!!!!我也看到过把大数组转换成np.float16时,2583.0反方向转换成2582.0.

So 2583.0 turns to 2584.0, cool that's not going to cause any problems!!!! I have also seen 2583.0 convert in the opposite direction to 2582.0 when I converted a large array to np.float16.

这是怎么回事我花了一整天的时间试图找到这个错误我从没想过这会发生在我的数据上?我正在使用 float16以限制我的内存消耗,但如果它如此危险,我将继续使用最多 32 位.

What is going on I spent a literal full day trying to find this bug and I never imagined this was happening to my data? I am using float16 to limit my memory consumption but if its this dangerous I will move on up to 32 bits.

有人能解释一下吗?

非常感谢大家.

推荐答案

16 位浮点数不是一种非常精确的数据类型.它只有 11 位精度.它不能精确表示大于 2048 的所有整数.在 2048 和 4096 之间,只有偶数整数才有精确表示.奇数将四舍五入为最接近的偶数之一.

A 16-bit float is not a very precise data type. It has only 11 bits of precision. It cannot exactly represent all integers greater than 2048. Between 2048 and 4096, only the even integers have exact representations. Odd integers will be rounded to one of the nearest even integers.

您可以在维基百科页面中阅读有关半宽的更多信息漂浮.以下是针对您的问题最重要的一段话:

You can read more about this in the Wikipedia page about half-width floats. Here's the most important passage for your issue:

整数值的精度限制

  • 可以精确表示 0 到 2048 之间的整数
  • 2049 到 4096 之间的整数四舍五入为 2 的倍数(偶数)
  • 4097 到 8192 之间的整数四舍五入为 4 的倍数

这篇关于numpy 的 float16 数据类型是否功能失调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆