函数原型

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. Java线程详解(5)-线程的同步与锁
  2. Android 原生通知Notification 写法
  3. 洛谷P2055 [ZJOI2009]假期的宿舍 题解
  4. 编程题:编写一个函数string_copy()完成strcpy()的作用,并验证。
  5. ubuntu 简单配置samba
  6. 可观测性PHP秩判据,线性系统的可控性与可观测性
  7. 【算法】算法 二分查找 二分查找 查找多个相同的值
  8. zabbix监控端口状态
  9. a5松下驱动器参数设置表_松下伺服驱动器参数设置MSD043A1X
  10. oppo r11s鸿蒙固件,OPPO R11s刷机包
  11. python全栈开发包括那些_简述Python全栈工程师要学会什么
  12. 通过docker搭建Yapi
  13. 拼多多商家刚开店,哪些行为不能做?
  14. c语言找adc最小值,STC12C5A60S2 AD转换C语言示例程序(ADC查询方式)
  15. 黔程似锦——2021国庆游攻略
  16. Oracle 12c统一审计
  17. 那些入行的Python工程师们还好吗?
  18. UltraEdit中文破解版
  19. pandas把一个excel的特定数据写到另一个excel_Python读写Excel文件第三方库汇总,你想要的都在这儿!...
  20. SSM框架和SpringBoot框架的对比

热门文章

  1. 【操作系统】IO模型篇之从BIO、NIO、AIO到内核select、epoll剖析
  2. 基于单片机的数字化农业管理系统
  3. 京东淘宝始料未及,小程序将是未来首选购物渠道
  4. Unity 角色血条
  5. javaWEB(EL+JSTL实现购物车)
  6. 网络技术之以太网链路聚合!
  7. 表单插件——form
  8. 《华林科纳-半导体工艺》种植体刻蚀表面分析
  9. linux 中NetworkManager和network的区别
  10. asp.net电子地图