Numpy fusing multiply and add to avoid wasting memory

落爺英雄遲暮 提交于 2019-12-23 09:57:24

问题


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?

Numpy has the out keyword parameter for the case of C = A times B:

numpy.multiply(A, B, out=C)

How about the case of C += A times B?


回答1:


Numpy only supports operations one at a time. With that said, there are several workarounds.

In place operations

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

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

Another option is to use some package like numexpr which allows you to compile expressions:

import numexpr

z = numexpr.evaluate('b * x + y')

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)



回答2:


As far as I understand, NumPy array operations can only be done one at a time, but by putting it inside of a function you can ensure it is not in memory, as the commenter suggested.



来源:https://stackoverflow.com/questions/45200278/numpy-fusing-multiply-and-add-to-avoid-wasting-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!