Reputation: 455
If I have the output of a layer with shape [batch_size, height, width, depth]
and another tensor of shape [depth]
, how can I multiply the first tensor by the second such that each slice along the depth
direction is multiplied by the corresponding value in the second tensor. That is, if the second tensor is [4, 5, 6]
, then the multiplication is:
tensor1[:, :, :, 0] * 4
tensor1[:, :, :, 1] * 5
tensor1[:, :, :, 2] * 6
Also, is there a name for this kind of multiplication that I didn't know to search for? Thanks!
Upvotes: 8
Views: 6652
Reputation: 2190
To answer your second question, "is there a name for this kind of multiplication that I didn't know to search for?", it is an element-wise multiplication with broadcasting. Broadcasting refers to operations that implicitly replicate elements of a tensor to make it compatible with a second tensor used in the element-wise operation. Many Tensorflow operations use the same broadcasting methods used by Numpy, which are further described here
Upvotes: 0
Reputation: 11895
This is straightforward. Just multiply both tensors. For example:
import tensorflow as tf
tensor = tf.Variable(tf.ones([2, 2, 2, 3]))
depth = tf.constant([4, 5, 6], dtype=tf.float32)
result = tensor * depth
sess = tf.Session()
sess.run(tf.initialize_all_variables())
print(sess.run(result))
Upvotes: 7
Reputation: 3159
I've come up with the following:
a = tf.placeholder(tf.float32, shape = [5, 2])
b = tf.placeholder(tf.float32, shape = 2)
c = tf.concat(1, [tf.mul(x, y) for x, y in zip(tf.split(0, 2, b), tf.split(1, 2, a))])
sess = tf.Session()
print sess.run(c, feed_dict = {a: np.ones([5, 2]), b: [5, 6]})
Looks a little bit odd, but it seems to work fine for me. I've used 2d tensor, but you definetely could extend this to your case.
Upvotes: 0