replicate a row tensor using tf.tile?

前端 未结 6 1401
故里飘歌
故里飘歌 2021-01-01 17:19

I have a tensor which is simply a vector, vector = [0.5 0.4] and tf.shape indicates that it has shape=(1,), I would like to replicate the vector m times and hav

6条回答
  •  别那么骄傲
    2021-01-01 17:44

    Take the following, vec is a vector, multiply is your m, the number of times to repeat the vector. tf.tile is performed on the vector and then using tf.reshape it is reshaped into the desired structure.

    import tensorflow as tf
    
    vec = tf.constant([1, 2, 3, 4])
    multiply = tf.constant([3])
    
    matrix = tf.reshape(tf.tile(vec, multiply), [ multiply[0], tf.shape(vec)[0]])
    with tf.Session() as sess:
        print(sess.run([matrix]))
    

    This results in:

    [array([[1, 2, 3, 4],
           [1, 2, 3, 4],
           [1, 2, 3, 4]], dtype=int32)]
    

提交回复
热议问题