I want to multiple the elements in a tensor of shape [a,b,c], each in the 3rd dimension with a scalar from a tensor of shape [a,b].
For example,
x =
|[1,2][3,4][5,6]|
|[1,2][3,4][5,6]|
|[1,2][3,4][5,6]|
y =
|0 0 1|
|1 0 0|
|0 1 0|
I want to multiply x and y to obtain:
res =
|[0,0][0,0][5,6]|
|[1,2][0,0][0,0]|
|[0,0][3,4][0,0]|
Could you tell me any method to achieve this?
P.S: maybe I can make a y'
from y
which is
y' =
|[0,0][0,0][1,1]|
|[1,1][0,0][0,0]|
|[0,0][1,1][0,0]|
then I can use tf.mul()
to obtain the result. But I don't find anything to duplicate elements in a tensor in this way.
You are right, you can just use tf.mul()
. In fact, the multiply operation in TensorFlow handles broadcasting, so you don't need to create another vector of shape [a, b, c]
.
To make sure that the broadcasting is on the expected dimension, you can add a third dimension to your second vector y
:
x = tf.random_normal([3, 3, 2])
y = tf.constant([[0., 0., 1.], [1., 0., 0.], [0., 1., 0.]])
y = tf.expand_dims(y, 2) # y will now have a matching shape of [3, 3, 1]
res = tf.mul(x, y)
sess = tf.Session()
sess.run(res)
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