鑒于我對每一行都有所需的索引,我想對張量的每一行分別操作 tf.gather。例如,如果一個張量:
A = tf.constant([[2., 5., 12., 9., 0., 0., 3.],
[0., 12., 2., 0., 0., 0., 5.],
[0., 0., 10., 0., 4., 4., 3.]], dtype=tf.float32)
有指數:
idxs = tf.constant([[0, 1, 3, 6, 0, 0, 0],
[1, 1, 2, 6, 6, 6, 6],
[2, 2, 4, 4, 6, 6, 6]], dtype=tf.int32)
我希望根據相應的索引行收集每一行:
output:
[[2. 5. 9. 3. 2. 2. 2.]
[12. 12. 2. 5. 5. 5. 5.]
[10. 10. 4. 4. 3. 3. 3.]]
我想過也許使用 tf.scan 但沒有成功。
uj5u.com熱心網友回復:
idxs需要轉換為full indices然后使用tf.gather_nd:
ii = tf.cast(tf.range(idxs.shape[0])[...,None], tf.float32)*tf.ones(idxs.shape[1], dtype=tf.float32)
indices = tf.stack([tf.cast(ii, tf.int32), idxs], axis=-1)
使用,
tf.gather_nd(A, indices)
[[ 2., 5., 9., 3., 2., 2., 2.],
[12., 12., 2., 5., 5., 5., 5.],
[10., 10., 4., 4., 3., 3., 3.]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/531231.html
標籤:Python张量流喀拉斯
上一篇:如何在AzureMLStudioDesigner中訓練自定義Tensorflow模型
下一篇:圖表已斷開:無法在“input_20”層獲取張量Tensor("input_20:0",shape=(?,25,2),dtype=float32)的值
