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!
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With