代码之家  ›  专栏  ›  技术社区  ›  Ujjwal

张量流中张量的快速高效交织

  •  0
  • Ujjwal  · 技术社区  · 6 年前

    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]]
    

    我们的目标是创建一个张量D,它具有以下形式

    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]]
    

    快速有效的方法是什么?

    反向传播将如何在这个Op中发生?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Mohan Radhakrishnan    6 年前

    我的尝试就是这样。如果我假设给定的形状,那么这将产生所需的输出。

    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) )
    

    [19 20 21] [ 4 5 6] [13 14 15] [ 7 8 9]

    注意:我无法解决backprop和性能点。

        2
  •  0
  •   Richard_wth    6 年前

    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
    

    tf.gather 是一种选择。

    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))
    

    如果没有弄错,在tf.聚集,backprop可以用a,b,c的元素(仅为1或0)进行,但不能用索引(在本例中, [0, 5, 2, 1, 7, 8, 3, 5, 6, 4] ).