i have 1 tensor in shape (2, g) , in shape (n, 2).
i need add them in such way output (n, 2, g), meaning first tensor replicated (n, 2, g) , second tensor added each matrix along third dimension. (or vice versa: second tensor replicated (n, 2, g) , first 1 added every sub-tensor along first dimension).
how can done efficiently in theano? thanks.
in attempt understand problem, following example assumed representative.
if
a = [[1, 2, 3], [4, 5, 6]]
and
b = [[1, 2], [3, 4], [5, 6], [7, 8]]
then result should be
c = [[[ 2. 3. 4.] [ 6. 7. 8.]] [[ 4. 5. 6.] [ 8. 9. 10.]] [[ 6. 7. 8.] [ 10. 11. 12.]] [[ 8. 9. 10.] [ 12. 13. 14.]]]
here g=3
, n=4
.
to achieve in theano, 1 need add new broadcastable dimensions , rely on broadcasting desired result.
import numpy import theano import theano.tensor tt x = tt.matrix() y = tt.matrix() z = x.dimshuffle('x', 0, 1) + y.dimshuffle(0, 1, 'x') f = theano.function([x, y], outputs=z) print f(numpy.array([[1, 2, 3], [4, 5, 6]]), numpy.array([[1, 2], [3, 4], [5, 6], [7, 8]]))
Comments
Post a Comment