Tensorflow:使用索引列表

时间:2018-03-02 22:30:20

标签: tensorflow

我看到了问题396844153702642540722200,这些问题有答案并提出了同样的问题。

然而,这些是在一年前被问到的,我想知道是否有更新的答案来更有效地做到这一点。另外,由于使用了gather_nd,我不确定它们是否可以区分。

1 个答案:

答案 0 :(得分:0)

易于实施。 例如,假设您有张量数据[[1,2 ,, [3,4]],则要获取第一列。 您可以使用 tf.transpose(data,perm = [1,0]) tf.gather_nd(data,[[0]])

import tensorflow as tf
import numpy as np

data = [[1, 3], [2, 4]]

a = tf.Variable(initial_value=data, dtype=tf.float32)
b = tf.transpose(a, perm=[1, 0])
fc = tf.gather_nd(b, [[0]])

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    column = sess.run([fc])[0]
    print(column)