Reputation: 5161
Does tensorflow offer any function for element-wise multiplication broadcasting on the last dimension?
Here is an example of what I'm trying to do and what does not work:
import tensorflow as tf
x = tf.constant(5, shape=(1, 200, 175, 6), dtype=tf.float32)
y = tf.constant(1, shape=(1, 200, 175), dtype=tf.float32)
tf.math.multiply(x, y)
Essentially, I would like for each one of x
's slice along the last dimension, do an element-wise matrix multiplication with y
.
I have found this question asking similar operation: Efficient element-wise multiplication of a matrix and a vector in TensorFlow
Unfortunately, the suggested approach (using tf.multiply()
) no longer work now. The corresponding tf.math.multiply
also does not work, as the code above gives me the error below:
Traceback (most recent call last):
File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1864, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 175 and 200 for 'Mul' (op: 'Mul') with input shapes: [1,200,175,6], [1,200,175].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py", line 180, in wrapper
return target(*args, **kwargs)
File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py", line 322, in multiply
return gen_math_ops.mul(x, y, name)
File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 6490, in mul
"Mul", x=x, y=y, name=name)
File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3616, in create_op
op_def=op_def)
File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 2027, in __init__
control_input_ops)
File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1867, in _create_c_op
raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 175 and 200 for 'Mul' (op: 'Mul') with input shapes: [1,200,175,6], [1,200,175].
I can think of a working approach: duplicate y
6 times so it has the exact same shape as x
, then do element-wise multiplication.
But are there faster and memory efficient way of doing this in tensorflow?
Upvotes: 1
Views: 1242
Reputation: 5555
This should achieve what you want:
x = np.array([[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]])
# [[[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [10 11 12]]]
y = np.array([[1,2,3,4]])
# [[1 2 3 4]]
y = tf.expand_dims(y, axis=-1)
mul = tf.multiply(x, y)
# [[[ 1 2 3]
# [ 8 10 12]
# [21 24 27]
# [40 44 48]]]
Finally, using the shapes you need:
x = np.random.rand(1, 200, 175, 6)
y = np.random.rand(1, 200, 175)
y = tf.expand_dims(y, axis=-1)
mul = tf.multiply(x, y)
with tf.Session() as sess:
print(sess.run(mul).shape)
# (1, 200, 175, 6)
Upvotes: 3