Suppose I have a vector x = [0.1, 0.2, 0.1, 0.1, 0.2, 0.4]
with length 30. This vector is wrapped by a theano Tensor.
I want to create a new vector, with same size as x
, but setting each 3 size slices elements to the sum of them and then do the same for the next 3 elements and so forth.
In the example, the first 3-length slice sum to 0.4, and the second 0.7 so r = [0.4, 0.4, 0.4, 0.7, 0.7, 0.7]
.
How can I do that? is using scan
the only way?
You can do the following in numpy:
import numpy as np
x = np.array([0.1, 0.2, 0.1, 0.1, 0.2, 0.4])
y = (x.reshape(-1, 3).mean(1)[:, np.newaxis] * np.ones(3)).ravel()
print(y)
In theano, you can proceed in a very similar way:
import theano
import theano.tensor as T
xx = T.fvector()
yy = (xx.reshape((-1, 3)).mean(axis=1).reshape((-1, 1)) * T.ones(3)).ravel()
print(yy.eval({xx: x.astype('float32')}))