c语言gather函数,TensorFlow函数:tf.gather_nd

本文详细解析了TensorFlow中的tf.gather_nd函数,介绍了如何使用索引来从参数张量中收集切片,并通过实例展示了从矩阵到3-tensor的各种索引操作。掌握这一功能对于处理多维数据至关重要。
摘要由CSDN通过智能技术生成

函数:tf.gather_ndgather_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]:]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值