Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy astype from float32 to float16

I would like to know how numpy casts from float32 to float16, because when I cast some number like 8193 from float32 to float16 using astype, it will output 8192 while 10000 of float32 casted into 10000 of float16.

import numpy as np
a = np.array([8193], dtype=np.float32)
b = a.astype(np.float16)
like image 236
Nan Avatar asked Oct 12 '17 09:10

Nan


People also ask

How do you use Astype NumPy?

We have a method called astype(data_type) to change the data type of a numpy array. If we have a numpy array of type float64, then we can change it to int32 by giving the data type to the astype() method of numpy array.

Is Python float 32 or 64?

Python's floating-point numbers are usually 64-bit floating-point numbers, nearly equivalent to np.

Is Float16 faster than float32?

python - Float16 is much slower than Float32 and Float64 in numpy - Stack Overflow.

What is Astype NumPy?

To modify the data type of a NumPy array, use the astype(data type) method. It is a popular function in Python used to modify the dtype of the NumPy array we've been provided with. We'll use the numpy. astype() function to modify the dtype of the specified array object.


1 Answers

The IEEE 754-2008 16-bit base 2 format, aka binary16, doesn't give you a lot of precision. What do you expect from 16 bits? :) 1 bit is the sign bit, 5 bits are used for the exponent, and that leaves 10 bits to store the normalised 11 bit mantissa, so anything > 2**11 == 2048 has to be quantized.

According to Wikipedia, integers between 4097 and 8192 round to a multiple of 4, and integers between 8193 and 16384 round to a multiple of 8.

like image 94
PM 2Ring Avatar answered Oct 12 '22 10:10

PM 2Ring