2017-07-26 43 views
2

我有一個張量,它只是一個矢量,vector = [0.5 0.4]和tf.shape表示它具有shape =(1,),我想重複該矢量m次並具有[m,2]的形狀,所以對於m = 2,matrix = [[0.5 0.4], [0.5 0.4]]。我如何實現使用tf.tile?使用tf.tile複製行張量?

回答

3

接下來,vec是一個向量,乘法是你的m,重複vec的次數。在矢量上執行平鋪,然後將結果重新整形到所需的結構中。

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

這導致:

[array([[1, 2, 3, 4], 
     [1, 2, 3, 4], 
     [1, 2, 3, 4]], dtype=int32)] 
相關問題