I want to understand the actual difference between float16
and float32
in terms of the result precision. For instance, Numpy
allows you to choose the range of the datatype you want (np.float16, np.float32, np.float64)
. My concern is that if I decide to go with float 16 to reserve memory and avoid possible overflow, would that create a loss of the final results comparing with float32 for instance?
Thank you
float64 is much slower than Python's float, and numpy. float32 is even slower (even though I'm on a 32-bit machine).
Looks float32 has a resolution of 1e-6 and the abs value is valid down to as small as 1.2e-38 . The relative error is at the order of 1e-8 for values above 1e-38, lower than 1e-6 proposed by np. finfo and the error is still acceptable even if the value if lower than the tiny value of np. finfo .
float64 are numpy specific 32 and 64-bit float types. Thus, when you do isinstance(2.0, np. float) , it is equivalent to isinstance(2.0, float) as 2.0 is a plain python built-in float type... and not the numpy type.
Floating Point Numbers Floats generally come in two flavours: “single” and “double” precision. Single precision floats are 32-bits in length while “doubles” are 64-bits. Due to the finite size of floats, they cannot represent all of the real numbers - there are limitations on both their precision and range.
a = np.array([0.123456789121212,2,3], dtype=np.float16) print("16bit: ", a[0]) a = np.array([0.123456789121212,2,3], dtype=np.float32) print("32bit: ", a[0]) b = np.array([0.123456789121212121212,2,3], dtype=np.float64) print("64bit: ", b[0])
float32 is a 32 bit number - float64 uses 64 bits.
That means that float64’s take up twice as much memory - and doing operations on them may be a lot slower in some machine architectures.
However, float64’s can represent numbers much more accurately than 32 bit floats.
They also allow much larger numbers to be stored.
For your Python-Numpy project I'm sure you know the input variables and their nature.
To make a decision we as programmers need to ask ourselves
A naive example would be if I store weather data of my city as [12.3, 14.5, 11.1, 9.9, 12.2, 8.2]
Next day Predicted Output could be of 11.5 or 11.5164374
do your think storing float 32 or float 64 would be necessary?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With