Reputation: 1638
Assume the following 3 tensors (with same shape) defined in TensorFlow.
A = [[1,2,3],[4,5,6],[7,8,9]]
B = [[10,11,12],[13,14,15],[16,17,18]]
C = [[19,20,21],[22,23,24],[25,26,27]]
The objective is to create a tensor D which has the following form
D = [[1,2,3],[10,11,12],[19,20,21],[4,5,6],[13,14,15],[22,23,24],[7,8,9],[16,17,18],[25,26,27]]
What is the fast and efficient way of doing this?
How will back propagation happen in this Op?
Upvotes: 0
Views: 605
Reputation: 3197
My attempt is this. If I assume the given shapes then this produces the desired output.
A = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
B = tf.constant([[10, 11, 12], [13, 14, 15], [16, 17, 18]])
C = tf.constant([[19, 20, 21], [22, 23, 24], [25, 26, 27]])
with tf.Session() as sess :
sess.run(tf.global_variables_initializer())
ODD = tf.concat( [A[0::2],B[0::2],C[0::2]], axis=0)
EVENANDODD = tf.concat([ODD[0::2],
tf.concat([A[1::2], B[1::2], C[1::2]], axis=0)], axis=0)
FINAL = tf.concat([EVENANDODD,
ODD[1::2]], axis=0)
print( sess.run(FINAL) )
[[ 1 2 3] [10 11 12] [19 20 21] [ 4 5 6] [13 14 15] [22 23 24] [ 7 8 9] [16 17 18] [25 26 27]]
Note : I couldn't address the backprop and performance points.
Upvotes: 1
Reputation: 702
In your specific case, A, B, C are 3x3 matrices and you wanna get a 9x3 matrix. The most intuitive way is to concatenate them. Below I show it with numpy.
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = np.array([[10, 11, 12], [13, 14, 15], [16, 17, 18]])
c = np.array([[19, 20, 21], [22, 23, 24], [25, 26, 27]])
abc = np.concatenate((a, b, c), axis=0)
abc = abc.reshape((3, 3, 3))
abc = abc.transpose((1, 0, 2))
abc = abc.reshape((9, 3)) # this gives your objective
However, in general, you may wanna stack irregular rows from each matrix, e.g, [A0, B2, A2, A1, C1, C2, B0, C0, B1]. tf.gather
is an option.
import tensorflow as tf
a = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = tf.constant([[10, 11, 12], [13, 14, 15], [16, 17, 18]])
c = tf.constant([[19, 20, 21], [22, 23, 24], [25, 26, 27]])
abc = tf.concat((a, b, c), axis=0)
abc = tf.gather(abc, [0, 5, 2, 1, 7, 8, 3, 5, 6, 4], axis=0)
sess = tf.InteractiveSession()
print(sess.run(abc))
If not mistaken, in tf.gather, backprop can be done w.r.t. elements of a, b, c (it is just 1 or 0), but not w.r.t. the index (in this case, [0, 5, 2, 1, 7, 8, 3, 5, 6, 4]
).
Upvotes: 0