4D Tensor Multiplication with Tensorflow

51 views Asked by At

I have a Tensor A with the shape: [1000,24,24,2] and I want to multiply it with its transpose so that I can get C = A^T.A

I tried:

B=tf.transpose(A)
C = tf.matmul(B, A)

and I get this error:

InvalidArgumentError: {{function_node _wrapped__BatchMatMulV2_device/job:localhost/replica:0/task:0/device:CPU:0}} In[0] and In[1] must have compatible batch dimensions: [2,24,24,1000] vs. [1000,24,24,2] [Op:BatchMatMulV2]

How can I do this in Tensorflow?

1

There are 1 answers

0
Alexey Tochin On

Probably you are wondering about elementwise matrix product keeping to the first (1000) and the last (2) dimensions. The matrix product is likely expected with respect to one of the 24 dimesions. This case I suggest tf.matmul

a = tf.ones([10,24,25,2])
a_perm = tf.transpose(a, perm=[0, 3, 1, 2])
a_perm.shape
# TensorShape([10, 2, 24, 25])

product_perm = tf.matmul(a_perm, a_perm, transpose_b=True)
product_perm.shape
# TensorShape([10, 2, 24, 24])

product = tf.transpose(product_perm, perm=[0, 2, 3, 1])
# TensorShape([10, 24, 24, 2])

I replaced one dimension from 24 by 25 in order to make it clear which of the dimensions is convoluted.