[英]how to select particular values defined in one tensor with data in another tensor
我有一個張量變量y( tf.shape(y) => [140,8]
)和另一個變量x = tf.constant([2,4,5,7],tf.int32)
我想為y中的數據選擇x中提到的所有行和列[2,4,5,7]。
在Matlab中,我可以簡單地定義req_data = y[:,x]
給我x中為y數據選擇的列。 在tensorflow中如何做?
如果你想做req_data = y[:,x]
首先使用tf.transpose
,所以張量的形狀將為( tf.transpose
)
然后使用tf.gather
選擇數據
因為tf.gather
只在axis = 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]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.