'how to multiply two tensor on an axis
let say I got two tensor where tensor A has shape (100,7), tensor B has shape (100,7,64). I want to pick the first item from A and B and multiply them by tf.matmul that result in shape (1,64) and then the next item as so on.then finally combine all tensor and get a tensor with shape (100,64).I can't find any function to do this... any helps?
edit: i can do this with the code below but very slow any tensorflow function for this?
outputs = []
for i in range(A.shape[0]):
outputs = outputs + [tf.matmul(tf.expand_dims(A[i],0),B[i])[0]]
outputs = tf.stack(outputs,axis=0)
Solution 1:[1]
That is because I like squeezes nothing else
[ Sample ]:
Matrix_A = tf.linspace(tf.zeros([100, 7], tf.float32), tf.math.multiply(tf.ones([100, 7], tf.float32), tf.constant([10.0], tf.float32)), 1, axis=2)
Matrix_B = tf.linspace(tf.zeros([100, 7], tf.float32), tf.math.multiply(tf.ones([100, 7], tf.float32), tf.constant([10.0], tf.float32)), 64, axis=2)
Matrix_A = tf.linspace(tf.zeros([100, 7], tf.float32), tf.math.multiply(tf.ones([100, 7], tf.float32), tf.constant([10.0], tf.float32)), 1, axis=2)
Matrix_B = tf.linspace(tf.zeros([100, 7], tf.float32), tf.math.multiply(tf.ones([100, 7], tf.float32), tf.constant([10.0], tf.float32)), 64, axis=2)
tf.slice(Matrix_A, [0, 0, 0], [100, 1, 1]).shape) + " and " + str(tf.slice(Matrix_B, [0, 0, 0], [100, 1, 64])
tf.math.multiply( tf.slice(Matrix_A, [0, 0, 0], [100, 1, 1]), tf.slice(Matrix_B, [0, 0, 0], [100, 1, 64]) )
tf.squeeze(tf.slice(tf.math.multiply( tf.slice(Matrix_A, [0, 0, 0], [100, 1, 1]), tf.slice(Matrix_B, [0, 0, 0], [100, 1, 64]) ), [0, 0, 0], [100, 1, 64])))
[ Output ]:
* Slice or slice windows
1. Create constants Matrix A and Matrix B with shape: (100, 7, 1) and (100, 7, 64)
Matrix_A = tf.linspace(tf.zeros([100, 7], tf.float32), tf.math.multiply(tf.ones([100, 7], tf.float32), tf.constant([10.0], tf.float32)), 1, axis=2)
Matrix_B = tf.linspace(tf.zeros([100, 7], tf.float32), tf.math.multiply(tf.ones([100, 7], tf.float32), tf.constant([10.0], tf.float32)), 64, axis=2)
___________________________________________________________________________________________________________________________________________________________________________
2. Slice Matrix A and Matrix B target shape: (100, 1, 1) and (100, 1, 64)
tf.slice(Matrix_A, [0, 0, 0], [100, 1, 1])
tf.slice(Matrix_B, [0, 0, 0], [100, 1, 64])
___________________________________________________________________________________________________________________________________________________________________________
3. Multiply them into target shape: (100, 1, 64)
tf.math.multiply( tf.slice(Matrix_A, [0, 0, 0], [100, 1, 1]), tf.slice(Matrix_B, [0, 0, 0], [100, 1, 64]) )
___________________________________________________________________________________________________________________________________________________________________________
4. Squeeze dimension: tf.Tensor([100 64], shape=(2,), dtype=int32)
tf.squeeze(tf.slice(tf.math.multiply( tf.slice(Matrix_A, [0, 0, 0], [100, 1, 1]), tf.slice(Matrix_B, [0, 0, 0], [100, 1, 64]) ), [0, 0, 0], [100, 1, 64])))
___________________________________________________________________________________________________________________________________________________________________________
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Martijn Pieters |