有没有更好的方法从tensorflow中的张量中删除列?

时间:2016-11-19 23:21:38

标签: tensorflow

我正在处理涉及删除列的图像群集问题。我能想到的唯一方法是使用tf.concat连接原始张量的两个切片,以及一些空格。我必须多次这样做,我担心它可能会很慢。有更好的方法吗?

new_tensor = tf.concat(1, [tf.slice(data, [0, 0, 0], [to_remove - 1, image_size, 1]), tf.slice(data, [to_remove, 0, 0], [image_size - to_remove - 1, image_size, 1]), tf.zeros([image_size, 1], tf.float32)])

1 个答案:

答案 0 :(得分:0)

你可以使用tf.gather,这更符合你的目标,但说实话,我不确定它是否会更快:

ind = tf.concat(0, [tf.range(0, to_remove), tf.range(to_remove + 1, data.get_shape()[0])])
removed = tf.gather(ind, data)
zero_filled = tf.concat(0, [removed, tf.zeros([image_size, 1], tf.float32)])

顺便说一句,在slice中,您可以使用-1来表示"直到此维度结束"。