【问题标题】:How to tile rows for different times in TF1.x?How to tile rows for different times in TF1.x?
【发布时间】:2022-12-26 13:56:20
【问题描述】:

I have a batch of data with shape [?, dim],

x=[[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]]

and a tensor indicates repetition number for each row with shape [?,1], say:

rep_nums=[[1],[2],[1],[3],[1]]

and expecting result to be :

[[ 0,  1,  2,  3,  4],
 [ 5,  6,  7,  8,  9],
 [ 5,  6,  7,  8,  9],
 [10, 11, 12, 13, 14],
 [15, 16, 17, 18, 19],
 [15, 16, 17, 18, 19],
 [15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24]]

I tried dynamic_partition as this mentioned, but only works in TF2.x, which not compatible my pre-exist project.

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    I think tf.repeat will help.

    import tensorflow as tf
    
    c1 = tf.constant([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14],
           [15, 16, 17, 18, 19],
           [20, 21, 22, 23, 24]])
    
    times = tf.constant([1, 2, 1, 3, 1])
    
    res = tf.repeat(c1, times, axis=0)
    
    with tf.Session() as sess:
        print (sess.run(res))
    

    【讨论】:

      猜你喜欢
      • 2022-12-26
      • 2022-12-02
      • 2022-12-19
      • 2022-12-02
      • 1970-01-01
      • 2022-12-27
      • 2022-12-01
      • 2022-12-01
      • 2022-12-02
      相关资源
      最近更新 更多