Reputation: 1012
I have 2 tensors:
I want to add them together, but dimensions are different so by analogy with 2D matrices I want to multiply tensor B with some other Tensor C, such that tensor product B * C will have dimension [128,24,24,256].
How can I do that in tensorflow? Which dimensionality should be C Tensor?
Upvotes: 1
Views: 863
Reputation: 11333
It really depends on what you're trying to do. There's two ways I can think of,
You keep all the other dimensions except last two exactly the same as a
and b
. You can think of n-d dimensional matrix multiplication as doing matrix multiplication on the last two dimensions and prefixing the rest of the dimensions as they are.
a = tf.random.normal(shape=[128,24,24,256])
b = tf.random.normal(shape=[128,24,24,64])
c = tf.random.normal(shape=[128,24,64,256])
# Essentially performing a [24, 64] . [64, 256]
# This is dot product
bc = tf.matmul(b,c)
# Res would be of size `128,24,24,256`
# This is element-wise
res = a * bc
You can also leverage the power of broadcasting by keeping the dimensions other than the last two equal to 1. Any of the first two dimensions (or all of them) can be 1 and TensorFlow will take care of the rest.
a = tf.random.normal(shape=[128,24,24,256])
b = tf.random.normal(shape=[128,24,24,64])
# Note how the beginning dimensions are 1
c = tf.random.normal(shape=[1,1,64,256])
bc = tf.matmul(b,c)
res = a * bc
But as I said earlier, the size of c
really depends on your usecase. There's no correct universal answer.
Upvotes: 2