假设我有一个长度为space
的二维数组n
。我如何使用[[1,2,3], [0.7, 1. 2.6], [9, 2, 1.4], ...]
返回数组的所有第一个和第三个元素。即返回一个长度为tf.gather_nd
的数组,如:n
答案 0 :(得分:3)
您可以使用tensorflow中的numpy style sub-sampling method
:y = X[:,::2]
x = np.array([[1,2,3], [0.7, 1., 2.6], [9, 2, 1.4]])
X = tf.constant(x)
Y = X[:,::2]
sess = tf.InteractiveSession()
out = Y.eval()
#array([[ 1. , 3. ],
# [ 0.7, 2.6],
# [ 9. , 1.4]])
答案 1 :(得分:2)
看起来有点破解,但似乎你可以转置张量,选择行(对应于原始张量中的列),然后将其转置回来:
import tensorflow as tf
import numpy as np
x = np.array([[1,2,3], [0.7, 1., 2.6], [9, 2, 1.4]])
with tf.Session() as sess:
val = sess.run(tf.transpose(tf.gather_nd(tf.transpose(x), [[0], [2]])))
val
#array([[ 1. , 3. ],
# [ 0.7, 2.6],
# [ 9. , 1.4]])