1

Given two tensors, A (m x n x q) and B (m x n x 1), how do you create a function which loops through rows of A, treating each element of B (n x 1) as a scalar and applying them to the vectors (q x 1) of the sub-matrices of A (n x q)?

e.g., A is (6000, 1000, 300) shape. B is (6000, 1000, 1). Loop through 6000 "slices" of A, for each vector of the 1000 sub-matrices of A (, 1000, 300), apply scalar multiplication of each element from the vectors the sub-matrices of B (, 1000, 1).

My wording may be absolutely terrible. I will adjust the phrasing accordingly as issues arise.

Sidenote: I am working with Python, so Theano is probably the best to do this in?

Andy
  • 175
  • 1
  • 7

1 Answers1

1

Use tf.mul as follows:

import tensorflow as tf
a = tf.constant([[[1,2,1,2],[3,4,1,2],[5,6,10,12]],[[7,8,1,2],[9,10,1,1],[11,12,0,3]]])
b= tf.constant([[[7],[8],[9]],[[1],[2],[3]]])
res=tf.mul(a,b)
sess=tf.Session()
print(sess.run(res)) 

which prints:

[[[  7  14   7  14]
  [ 24  32   8  16]
  [ 45  54  90 108]]

 [[  7   8   1   2]
  [ 18  20   2   2]
  [ 33  36   0   9]]]
Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
  • Wow this works. So I guess Tensorflow's tf.mul automatically does this. I though it was going to involve some nested theano.scan. Thanks! – Andy Apr 04 '17 at 22:28