tf.gather_nd(params, indices, name=None)
{#gather_nd}
用indices从张量params得到新张量
Gather values or slices from params
according to indices
.
params
is a Tensor of rank P
and indices
is a Tensor of rank Q
.
indices
must be integer tensor, containing indices into params
.It must be shape [d_0, ..., d_{Q-2}, K]
where 0 < K <= P
.
The innermost dimension of indices
(with length K
) corresponds toindices into elements (if K = P
) or slices (if K < P
) along the K
thdimension of params
.
Produces an output tensor with shape
[d_0, ..., d_{Q-2}, params.shape[K], ..., params.shape[P-1]].
Some examples below.
Simple indexing into a matrix:
indices = [[0, 0], [1, 1]]
params = [['a', 'b'], ['c', 'd']]
output = ['a', 'd']
Slice indexing into a matrix:
indices = [[1], [0]]
params = [['a', 'b'], ['c', 'd']]
output = [['c', 'd'], ['a', 'b']]
Indexing into a 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']
Batched indexing into a matrix:
indices = [[[0, 0]], [[0, 1]]]
params = [['a', 'b'], ['c', 'd']]
output = [['a'], ['b']]
Batched slice indexing into a matrix:
indices = [[[1]], [[0]]]
params = [['a', 'b'], ['c', 'd']]
output = [[['c', 'd']], [['a', 'b']]]
Batched indexing into a 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']]
Args:
params
: ATensor
.P-D
. The tensor from which to gather values.indices
: ATensor
. Must be one of the following types:int32
,int64
.Q-D
. Index tensor having shape[d_0, ..., d_{Q-2}, K]
.name
: A name for the operation (optional).
Returns:
A Tensor
. Has the same type as params
. (P+Q-K-1)-D
. Values from params
gathered from indices given by indices
.