0
我有一個張量變量y(tf.shape(y) => [140,8]
)和另一可變x = tf.constant([2,4,5,7],tf.int32)
如何選擇特定值在一個張量在另一張量
我想選擇所有行和列[2,4,5,7-定義的數據]如y中數據的x所述。
在Matlab中,我可以簡單地定義req_data = y[:,x]
爲y數據提供了x中的選定列。 如何在tensorflow中做到這一點?
我有一個張量變量y(tf.shape(y) => [140,8]
)和另一可變x = tf.constant([2,4,5,7],tf.int32)
如何選擇特定值在一個張量在另一張量
我想選擇所有行和列[2,4,5,7-定義的數據]如y中數據的x所述。
在Matlab中,我可以簡單地定義req_data = y[:,x]
爲y數據提供了x中的選定列。 如何在tensorflow中做到這一點?
如果你想要做req_data = y[:,x]
第一次使用tf.transpose
,所以張量的形狀將是(8,140)
然後使用tf.gather
選擇數據
因爲tf.gather
上軸= 0僅工作,所以轉首先 然後轉置回
a = tf.constant([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])
a_trans = tf.transpose(a)
b = tf.constant([2,4,5,7])
c = tf.gather(a_trans, b)
c_trans = tf.transpose(c)
with tf.Session() as sess:
print sess.run(c_trans)
#output [[3 5 6 8]
# [13 15 16 18]]