Python:将numpy默认为float32 [英] Python: Making numpy default to float32

查看:891
本文介绍了Python:将numpy默认为float32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何干净的方法将numpy设置为使用float32值,而不是全局使用float64?

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

推荐答案

我不知道.在调用任何数组的构造函数时,您要么需要显式指定dtype,要么将数组传递给float32(使用ndarray.astype方法),然后再将其传递给GPU代码(我认为这是问题所在)? ).如果您真正担心的是GPU情况,那么我赞成后者-在没有对numpy广播规则和非常精心设计的代码非常透彻理解的情况下,尝试使所有内容保持单一精度可能会变得非常烦人.

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.

另一种替代方法可能是创建自己的方法,这些方法会使标准numpy构造函数(例如numpy.zeros,numpy.ones,numpy.empty)超载.这应该非常接近于将所有内容都保持在float32中.

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.

这篇关于Python:将numpy默认为float32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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