Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Making numpy default to float32

Is there any clean way of setting numpy to use float32 values instead of float64 globally?

like image 870
Bolster Avatar asked Apr 19 '11 19:04

Bolster


People also ask

Is Python float 32 or 64?

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

Is float32 the same as float?

float is one of the available numeric data types in Go used to store decimal numbers. float32 is a version of float that stores decimal values composed of 32 bits of data.

Is float32 faster than float64?

float64 is much slower than Python's float, and numpy. float32 is even slower (even though I'm on a 32-bit machine).

Is float16 faster than float32?

Peak float16 matrix multiplication and convolution performance is 16x faster than peak float32 performance on A100 GPUs.


2 Answers

Not that I am aware of. You either need to specify the dtype explicitly when you call the constructor for any array, or cast an array to float32 (use the ndarray.astype method) before passing it to your GPU code (I take it this is what the question pertains to?). If it is the GPU case you are really worried about, I favor the latter - it can become very annoying to try and keep everything in single precision without an extremely thorough understanding of the numpy broadcasting rules and very carefully designed code.

Another alternative might be to create your own methods which overload the standard numpy constructors (so numpy.zeros, numpy.ones, numpy.empty). That should go pretty close to keeping everything in float32.

like image 172
talonmies Avatar answered Oct 14 '22 01:10

talonmies


This question showed up on the NumPy issue tracker. The answer is:

There isn't, sorry. And I'm afraid we're unlikely to add such a thing[.]

like image 26
Mihai Capotă Avatar answered Oct 14 '22 01:10

Mihai Capotă