0

Is there any method to output like this?

x = (30, 64, 36, 1)
y = (30, 64, 36, 2048)

Is there some operation that can make the output of shape (30, 64, 2048, 1) with x and y

Innat
  • 16,113
  • 6
  • 53
  • 101
puhuk
  • 464
  • 5
  • 15

1 Answers1

1

If I Understand Correctly (IIUC) you want this:

enter image description here

You can use einsum in numpy or tensorflow like below:

Numpy Version

import numpy as np
x = np.random.rand(30, 64, 36, 1)
y = np.random.rand(30, 64, 36, 2048)
z = np.einsum('ijkl,ijkm->ijml', x, y)
# output[i,j,m,l] = sum_k x[i,j,k,l] * y[i,j,k,m]
print(z.shape)

Tensorflow Version

import tensorflow as tf
x = tf.random.normal(shape=[30, 64, 36, 1])
y = tf.random.normal(shape=[30, 64, 36, 2048])
z = tf.einsum('ijkl,ijkm->ijml', x, y)
# output[i,j,m,l] = sum_k x[i,j,k,l] * y[i,j,k,m]
print(z.shape)

Output:

(30, 64, 2048, 1)
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • 1
    Wow, that is exactly what I want. Is there any method that I can think the output and input shape in matrix operation? – puhuk Jun 05 '22 at 10:06
  • @puhuk, I don't understand your question. Do you want another method? – I'mahdi Jun 05 '22 at 10:08
  • No, I just ask how do you think about the method so fast. Because I can not ask whenever I have question with operation shape. – puhuk Jun 05 '22 at 10:21
  • @puhuk, thanks, I read and learn before and when you ask I think this will be best answer for you. for reading about einsum you can read [here](https://stackoverflow.com/questions/26089893/understanding-numpys-einsum), or about see different performance and how is fast you can read [here](https://stackoverflow.com/questions/72489466/what-is-the-most-efficient-way-to-deal-with-a-loop-on-numpy-arrays). – I'mahdi Jun 05 '22 at 10:28