1. 程式人生 > >『TensorFlow』高級高維切片gather_nd

『TensorFlow』高級高維切片gather_nd

有關 lan param put bsp 理解 索引 替換 style

gather用於高級切片,有關官方文檔的介紹,關於維度的說明很是費解,示例也不太直觀,這裏給出我的解讀,示例見下面,

indices = [[0, 0], [1, 1]]
params = [[‘a‘, ‘b‘], [‘c‘, ‘d‘]]
output = [‘a‘, ‘d‘]

indices = [[[0, 0]], [[0, 1]]]
params = [[‘a‘, ‘b‘], [‘c‘, ‘d‘]]
output = [[‘a‘], [‘b‘]]

我們兩次被索引對象是相同的:[2, 2]形狀,而indice的形狀各不相同,按照官網的算法計算輸出很不直觀,按照我的理解,我們由最後一維度向外看indices即可,即[0, 0]
索引到‘a‘[1, 1]索引到‘b‘,然後我們使用索引到的對象替換掉最後一維度的索引即可,例如,對於[[0, 0], [1, 1]],替換掉[0, 0][1, 1]後,即為[‘a‘, ‘d‘],而[[[0, 0]], [[0, 1]]]替換後保留了外層兩個維度:[[‘a‘], [‘b‘]]。 我們看官網另一個例子,對應[0, 0, 1]獲得‘b0‘[1, 0, 1]獲得‘b1‘……,簡單替換即可。

indices = [[[0, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 1, 0]]]
params = [[[‘a0‘, ‘b0‘], [‘c0‘, ‘d0‘]],

[[‘a1‘, ‘b1‘], [‘c1‘, ‘d1‘]]]
output = [[‘b0‘, ‘b1‘], [‘d0‘, ‘c1‘]]

『TensorFlow』高級高維切片gather_nd