TensorFlow函数:tf.gather_nd
2018-10-20 18:05 更新
函数:tf.gather_nd
gather_nd(
params,
indices,
name=None
)
参见指南:张量变换>分割和连接
将参数中的切片收集到由索引指定的形状的张量中.
索引(indices)是一个 k 维整数张量,最好作为一个 (k-1) 维的索引(indices)张量的参数,其中每个元素定义了一个参数切片:
output[i_0, ..., i_{K-2}] = params[indices[i0, ..., i_{K-2}]]
然而在 tf.gather 函数中,将片段定义为参数的第一维度;在 tf.gather_nd 中,索引将切片定义为参数的第一个 n 维度.其中: N = indices.shape[-1]
索引的最后一个维度最多可以是参数的秩:
indices.shape[-1] <= params.rank
索引的最后一个维度对应于元素 (如果 indices.shape[-1] == params.rank) 或切片 (如果 indices.shape[-1] < params.rank) 沿参数 dices.shape[-1] 中的维度.输出张量具有形状:
indices.shape[:-1] + params.shape[indices.shape[-1]:]
下面是一些例子:
简单的索引到矩阵:
indices = [[0, 0], [1, 1]]
params = [['a', 'b'], ['c', 'd']]
output = ['a', 'd']
将索引分为矩阵:
indices = [[1], [0]]
params = [['a', 'b'], ['c', 'd']]
output = [['c', 'd'], ['a', 'b']]
索引到一个3-tensor:
indices = [[1]]
params = [[['a0', 'b0'], ['c0', 'd0']],
[['a1', 'b1'], ['c1', 'd1']]]
output = [[['a1', 'b1'], ['c1', 'd1']]]
indices = [[0, 1], [1, 0]]
params = [[['a0', 'b0'], ['c0', 'd0']],
[['a1', 'b1'], ['c1', 'd1']]]
output = [['c0', 'd0'], ['a1', 'b1']]
indices = [[0, 0, 1], [1, 0, 1]]
params = [[['a0', 'b0'], ['c0', 'd0']],
[['a1', 'b1'], ['c1', 'd1']]]
output = ['b0', 'b1']
分批索引到矩阵中:
indices = [[[0, 0]], [[0, 1]]]
params = [['a', 'b'], ['c', 'd']]
output = [['a'], ['b']]
分批切片索引成矩阵:
indices = [[[1]], [[0]]]
params = [['a', 'b'], ['c', 'd']]
output = [[['c', 'd']], [['a', 'b']]]
分批索引到一个 3-tensor:
indices = [[[1]], [[0]]]
params = [[['a0', 'b0'], ['c0', 'd0']],
[['a1', 'b1'], ['c1', 'd1']]]
output = [[[['a1', 'b1'], ['c1', 'd1']]],
[[['a0', 'b0'], ['c0', 'd0']]]]
indices = [[[0, 1], [1, 0]], [[0, 0], [1, 1]]]
params = [[['a0', 'b0'], ['c0', 'd0']],
[['a1', 'b1'], ['c1', 'd1']]]
output = [[['c0', 'd0'], ['a1', 'b1']],
[['a0', 'b0'], ['c1', 'd1']]]
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']]
参数:
- params:张量.这个张量是用来收集数值的.
- indices:张量.必须是以下类型之一:int32,int64;索引张量.
- name:操作的名称(可选).
返回值:
该函数返回一个张量.与参数具有相同的类型.参数值从索引所给定的索引中收集,并且具有形状:indices.shape[:-1] + params.shape[indices.shape[-1]:]