numpy融合乘法和加法以避免浪费内存 [英] Numpy fusing multiply and add to avoid wasting memory
问题描述
是否可以将两个ndarray A和B相乘并将结果加到C,而无需为A乘以B生成大的中间数组?
Is it possible to multiply two ndarray A, and B and add the result to C, without creating a large intermediate array for A times B?
在C = A乘以B的情况下,Numpy具有out关键字参数:
Numpy has the out keyword parameter for the case of C = A times B:
numpy.multiply(A, B, out=C)
C + = A乘B的情况如何?
How about the case of C += A times B?
推荐答案
Numpy一次仅支持一次操作.话虽如此,有几种解决方法.
Numpy only supports operations one at a time. With that said, there are several workarounds.
最简单的解决方案是通过+=
和*=
The most simple solution is to use in-place operations via +=
and *=
import numpy as np
import scipy
n = 100
b = 5.0
x = np.random.rand(n)
y = np.random.rand(n)
z = b * x
z += y
BLAS
您可以访问基础的 BLAS 程序并手动应用它们.可悲的是,没有乘法加法指令,但是有执行的"AXPY"指令
BLAS
You can access the underlying BLAS programs and apply them manually. Sadly, there is no multiply add instruction, but there is the "AXPY" instruction, which performs
y <- a * x + y
这可以通过以下方式调用:
This can be called via:
import scipy
axpy = scipy.linalg.blas.get_blas_funcs('axpy', arrays=(x, y))
axpy(x, y, n, b)
Numexpr
另一种选择是使用某些软件包,例如 numexpr
,该软件包可用于编译表达式:
Numexpr
Another option is to use some package like numexpr
which allows you to compile expressions:
import numexpr
z = numexpr.evaluate('b * x + y')
Theano
最近有几个机器学习软件包已经开始支持编译表达式,其中一个就是theano.您可以执行以下操作:
Theano
Recently several machine-learning packages have started supporting compiled expressions, one such package is theano. You could do something like:
import theano
x = theano.tensor.vector() # declare variable
y = theano.tensor.vector() # declare variable
out = b * x + y # build symbolic expression
f = theano.function([x, y], out) # compile function
z = f(x, y)
这篇关于numpy融合乘法和加法以避免浪费内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!