函数原型

tf.gather_nd(params, indices, batch_dims=0, name=None
)

函数说明

从指定的张量中根据索引来进行切片。注意tf.gather函数和tf.gather_nd函数的区别。

参数params表示待切片的张量。

参数indices表示进行切片的索引。

参数batch_dims表示批处理的维度,这个参数比较难理解,可以看一下使用示例中的用法。

使用示例

1、一维张量的索引

>>> params = tf.constant([1, 3, 5, 7, 9])
# 对于一位张量,只能进行索引,输入某一个索引对应的值。
>>> indices = tf.constant([0])
>>> tf.gather_nd(params, indices)
<tf.Tensor: shape=(), dtype=int32, numpy=1>

2、二维张量的索引

>>> params = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> params
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])>
>>> indices = tf.constant([[0, 0], [1, 1]])
>>> tf.gather_nd(params, indices)
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([1, 5])>>>> params = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> params
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])>
>>> indices = tf.constant([[0], [1]])
# 注意使用batch_dims和不使用batch_dims,参数indices的区别
>>> tf.gather_nd(params, indices, batch_dims=1)
<tf.Tensor: shape=(3,), dtype=int32, numpy=array([1, 5,])>

3、二维张量的切片

>>> params = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> params
<tf.Tensor: shape=(3, 3), dtype=int32, numpy=
array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])>
>>> indices = tf.constant([[0], [1]])
>>> tf.gather_nd(params, indices)
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],[4, 5, 6]])>

4、三维张量的索引

# 没有使用batch_dims
>>> params = tf.constant([[[1, 1], [2, 2], [3, 3]],[[4, 4], [5, 5], [6, 6]],[[7, 7], [8, 8], [9, 9]]])
>>> params
<tf.Tensor: shape=(3, 3, 2), dtype=int32, numpy=
array([[[1, 1],[2, 2],[3, 3]],[[4, 4],[5, 5],[6, 6]],[[7, 7],[8, 8],[9, 9]]])>
>>> indices = tf.constant([[0, 0], [1, 1], [2, 2]])
>>> tf.gather_nd(params, indices)
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 1],[5, 5],[9, 9]])># 使用batch_dims
>>> params = tf.constant([[[1, 1], [2, 2], [3, 3]],[[4, 4], [5, 5], [6, 6]],[[7, 7], [8, 8], [9, 9]]])
>>> params
<tf.Tensor: shape=(3, 3, 2), dtype=int32, numpy=
array([[[1, 1],[2, 2],[3, 3]],[[4, 4],[5, 5],[6, 6]],[[7, 7],[8, 8],[9, 9]]])>
>>> indices = tf.constant([[0], [1], [2]])
>>> tf.gather_nd(params, indices, batch_dims=1)
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 1],[5, 5],[9, 9]])>

5、三维张量的切片

>>> params = tf.constant([[[1, 1], [2, 2], [3, 3]],[[4, 4], [5, 5], [6, 6]],[[7, 7], [8, 8], [9, 9]]])
>>> indices = tf.constant([[0], [1]])
>>> tf.gather_nd(params, indices)
<tf.Tensor: shape=(2, 3, 2), dtype=int32, numpy=
array([[[1, 1],[2, 2],[3, 3]],[[4, 4],[5, 5],[6, 6]]])>

tf.gather_nd函数相关推荐

  1. 【python】tensorflow框架中的tf.gather_nd()函数对应的 pytorch框架的gather_nd()函数

    tf.gather_nd 函数对应的pytorch函数 1. 简单介绍 2. 步入正题 2.1 tensorflow tf.gather_nd() 2.2 pytorch框架手动实现gather_nd ...

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

    函数:tf.gather_ndgather_nd( params, indices, name=None ) 将参数中的切片收集到由索引指定的形状的张量中. 索引(indices)是一个 k 维整数张 ...

  3. tf.gather tf.gather_nd 和 tf.batch_gather 使用方法

    在计算机视觉的项目中,会遇到根据索引获取数组特定下标元素的任务.常用的函数有tf.gather , tf.gather_nd 和 tf.batch_gather. 1.tf.gather(params ...

  4. tensorflow的tf.reduce_mean函数

    tf.reduce_mean函数的作用是求平均值.第一个参数是一个集合,可以是列表.二维数组和多维数组.第二个参数指定在哪个维度上面求平均值.默认对所有的元素求平均.tf.reduce_mean 比如 ...

  5. 计算机一级电子表格TF函数,tf.slice()函数详解(极详细)

    tf.slice()是TensorFlow库中分割张量的一个函数,其定义为def slice(input_, begin, size, name=None):.tf.slice()函数的那些参数设置实 ...

  6. tensorflow中tf.get_variable()函数详解

    如果变量存在,函数tf.get_variable()会返回现有的变量:如果变量不存在,会根据给定形状和初始值创建一个新的变量. def get_variable(name, shape=None, d ...

  7. 【tensorflow】tf.reshape函数说明:重塑张量

    转载 [471]tf.reshape函数说明_周小董-CSDN博客 https://blog.csdn.net/xc_zhou/article/details/85342542 函数原型: tf.re ...

  8. tf.pad函数功能介绍

    tf.pad()函数主要是对张量在各个维度上进行填充,该函数的参数如下所示: pad(tensor,paddings,mode='CONSTANT',name=None ) 其中: tensor是待填 ...

  9. tf.reduce_max()函数的用法详解

    tf.reduce_max()函数 tf.reduce_max(input_tensor,axis=None,name=None,keepdims=False #是否保持矩形原狀 ) 参数解释: in ...

最新文章

  1. 适合初学者的数据结构_数据结构101:图-初学者的直观介绍
  2. 远程办公,团队如何高效沟通与协作
  3. java多线程总结图_Java多线程总结之Queue
  4. org.apache.poi 读取数字问题
  5. 判别学习与生成学习的区别
  6. 使用函数_使用TEXT函数生成中文大写金额
  7. SQL Server 2000 JDBC驱动的完整安装及测试说明
  8. 《JavaScript权威指南》读书笔记二
  9. Spring 学习系列 -- Spring + Mybatis 从零开始配置多数据源访问
  10. 活动选择问题 贪心
  11. C#反编译工具ilspy下载地址
  12. 接口性能测试+调优+性能指标
  13. 日本人布置工作至少说5遍
  14. 双驰推大规模个性化定制生产迎来颠覆性变革
  15. UFS系列十:UFS电源管理
  16. 七大行星排列图片_太阳系九大行星排列顺序(口诀:水金地,火木土,天海)...
  17. PDF文件加密解密-文件设置密码
  18. 使用Xavier读取RealSense D435i的踩坑经历
  19. 闪耀暖暖的3D建模真好吗?
  20. Eclipse修改为中文版

热门文章

  1. 【论文分享】☆☆☆ 异质图神经网络模型 HAN:Heterogeneous Graph Attention Network
  2. oa项目实战php网盘,赵庆轩OA项目实战视频教程附源码+静态页+PPT
  3. 云端移动巧妙获取网站访客手机号码
  4. java多态讲解例子_Java多态性定义与用法实例详解
  5. SAP基底数据仓库实战项目(从底层到前端完成一张数据报表(下))
  6. win10 c语言语音朗读,win10安装朗读女软件
  7. 游戏建模|坚持下来的人都是非常优秀的?因为他离不开以下几点
  8. 基于mdm9206 threadx_os的I2c操作相关API介绍
  9. iOS系统键盘和自定义键盘的切换
  10. MMPOSE代码教学