Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying along an arbitrary axis?

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!

like image 848
Shiania White Avatar asked Sep 11 '16 01:09

Shiania White


1 Answers

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))
like image 192
rvinas Avatar answered Oct 20 '22 20:10

rvinas