2017-08-30 133 views
0

我想創建一個2D張量,其行是指標向量 - 零,除了單列中的一個 - 來自佔位符Tensor的輸入。 我試過類似的方法:從佔位符張量創建SparseTensor?

train_labels = tf.placeholder(tf.int32, shape=[64, 1]) 
... 
tf.SparseTensor(
    indices=[[i, x] for i, x in enumerate(train_labels)], 
    values=tf.ones(64), 
    dense_shape=[64, 50000]) 

但張量是不可迭代的。有沒有人有一些方向?張量操作中

回答

0

工作,我有一個醜陋的原型:

i = -1 
def mk_pair(x): 
    global i 
    i = i+1 
    c = tf.constant(i, dtype=tf.int64, shape=[1]) 
    x = tf.cast(x, tf.int64) 
    return tf.concat([c, x], 0) 

tf.SparseTensor(
    indices=tf.cast(tf.map_fn(mk_pair, train_labels), tf.int64), 
    values=tf.ones(64), 
    dense_shape=[64, 50000]) 
相關問題