girl-meets-world
girl-meets-world

Reputation: 17

Multiply each channel by different matrix?

Is there a way, in tensorflow, to multiply each channel by a different matrix?

Imagine you have a 2D array A of dimensions (N, D1). You can multiply it by an array B of size (D1, D2) to get output size (N, D2).

Now imagine you have a 3D array of dimensions (N, D1, 3). Suppose you had B1, B2, B3 all of size (D1, D2). Combining the outputs A * B1, A * B2, A * B3, you could form an array of size (N, D2, 3). But is there a way to get an output size of (N, D2, 3) by just doing multiplication once? I looked into transpose and matmul but it doesn't seem to work for this purpose.

Thank you!

Upvotes: 0

Views: 944

Answers (2)

Autonomous
Autonomous

Reputation: 9075

You could use tf.matmul here. Its just that you will have to transpose the dimensions.

Consider, N = 2, D1 = 4, D2 = 5. First create two matrices having shapes N x D1 x 3 and D1 x D2 x 3.

a = tf.constant(np.arange(1, 25, dtype=np.int32), shape=[2,4,3])
b = tf.constant(np.arange(1, 61, dtype=np.int32), shape=[4,5,3])

Transpose the matrices so that the first dimension is the same.

a = tf.transpose(a, (2, 0, 1))  # a.shape = (3, 2, 4)
b = tf.transpose(b, (2, 0, 1))  # b.shape = (3, 4, 5)

Perform the multiplication as usual.

r = tf.matmul(a,b)  # r.shape = (3, 2, 5)
r = tf.transpose(r, (1, 2, 0))  # r.shape = (2, 5, 3)

Hope this helps.

Upvotes: 0

benjaminplanche
benjaminplanche

Reputation: 15119

tf.einsum() could be applied here.

To make the code below easier to understand, I renamed D1 = O and D2 = P.

import tensorflow as tf

A = tf.random_normal([N, O, 3])
B = tf.random_normal([O, P, 3])  # B = tf.stack([B1, B2, B3], axis=2)
res = tf.einsum("noi,opi->npi", A, B)

Upvotes: 2

Related Questions