如何避免 Numpy 自动类型转换为 float64?

问题描述 投票:0回答:2

是否可以避免或发出从整数和

32 bit float arrays
64 bit float arrays
的自动 Numpy 类型转换的警告?

我的用例是,我正在开发一个大型分析包(20k 行 Python 和 Numpy),并且当前混合了 float 32 和 64 以及一些 int 数据类型,很可能导致性能次优和浪费内存,基本上我想在任何地方都一致地使用 float32。

我知道在 Tensorflow 中组合两个不同 dtype 的数组会产生错误 - 正是因为隐式转换为 float64 会导致性能不佳,并且对所有计算张量都是“传染性的”,并且如果隐式完成则很难找到引入它的位置。

在 Numpy 中寻找一个选项或一种对 Numpy 进行猴子修补的方法,使其在这方面的行为类似于 Tensorflow,即在

np.add
np.mul
等操作上的隐式类型转换时发出错误,甚至更好,发出带有打印回溯的警告,以便继续执行,但我知道它发生在哪里。可能吗?

python numpy tensorflow floating-point
2个回答
0
投票

免责声明:我没有以任何认真的方式对此进行测试,但这似乎是一条很有前途的路线。

操纵 ufunc 行为的一种相对轻松的方法似乎是 子类化

ndarray
和重写
__array_ufunc__
。例如,如果您满足于捕捉任何产生
float64

的东西
class no64(np.ndarray):
    def __array_ufunc__(self, ufunc, method, *inputs, **kwds):
        ret = getattr(ufunc, method)(*map(np.asarray,inputs), **kwds)
        # some ufuncs return multiple arrays:
        if isinstance(ret,tuple):
            if any(x.dtype == np.float64 for x in ret):
                raise ValueError
            return (*(x.view(no64) for x in ret),)
        if ret.dtype == np.float64:
             raise ValueError
        return ret.view(no64)

x = np.arange(6,dtype=np.float32).view(no64)

现在让我们看看我们的班级可以做什么:

x*x
# no64([ 0.,  1.,  4.,  9., 16., 25.], dtype=float32)
np.sin(x)
# no64([ 0.        ,  0.84147096,  0.9092974 ,  0.14112   , -0.7568025 ,
#       -0.9589243 ], dtype=float32)
np.frexp(x)
# (no64([0.   , 0.5  , 0.5  , 0.75 , 0.5  , 0.625], dtype=float32), no64([0, 1, 2, 2, 3, 3], dtype=int32))

现在让我们将它与 64 位参数配对:

x + np.arange(6)
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   File "<stdin>", line 9, in __array_ufunc__
# ValueError

np.multiply.outer(x, np.arange(2.0))
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   File "<stdin>", line 9, in __array_ufunc__
# ValueError

什么不起作用(我确信还有更多)

np.outer(x, np.arange(2.0))  # not a ufunc, so slips through
# array([[0., 0.],
#        [0., 1.],
#        [0., 2.],
#        [0., 3.],
#        [0., 4.],
#        [0., 5.]])

__array_function__
似乎是抓住那些的。


0
投票

类 no64(np.ndarray): def array_ufunc(自身,ufunc,方法,*输入,**kwds): ret = getattr(ufunc, 方法)(map(np.asarray,输入), **kwds) # 一些 ufunc 返回多个数组: if isinstance(ret,元组): 如果有(ret中的x,x.dtype == np.float64): 引发值错误 return ((x.view(no64) for x in ret),) 如果 ret.dtype == np.float64: 引发值错误 返回 ret.view(no64)

x = np.arange(6,dtype=np.float32).view(no64)

© www.soinside.com 2019 - 2024. All rights reserved.