I have a vector v and a matrix z, say
v = theano.shared(rng.normal(0, 1, 10))
z = theano.shared(rng.normal(0, 1, (10, 10)))
I want to create a new vector y given by v + sum of elements in each row of z. Basically: y[i] = v[i] + T.sum(z[:,i]) that I can do, for each entry, by:
y[i] = v[i] + theano.tensor.sum(z[:][i])
My question is: is there a way, without doing a loop, to write y = v + T.sum(rows of z) in one single line?
You can obtain it like this
y = v + z.sum(axis=1)
In numpy, and thus in theano, many aggregator functions, such as sum
, mean
, var
, std
, any
, all
, ... have an axis
keyword argument, and sometimes even an axes
keyword argument, with which you can specify exactly in which direction you array is to be traversed.