tf.gather_nd函数
函数原型
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函数相关推荐
- 【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 ...
- c语言gather函数,TensorFlow函数:tf.gather_nd
函数:tf.gather_ndgather_nd( params, indices, name=None ) 将参数中的切片收集到由索引指定的形状的张量中. 索引(indices)是一个 k 维整数张 ...
- tf.gather tf.gather_nd 和 tf.batch_gather 使用方法
在计算机视觉的项目中,会遇到根据索引获取数组特定下标元素的任务.常用的函数有tf.gather , tf.gather_nd 和 tf.batch_gather. 1.tf.gather(params ...
- tensorflow的tf.reduce_mean函数
tf.reduce_mean函数的作用是求平均值.第一个参数是一个集合,可以是列表.二维数组和多维数组.第二个参数指定在哪个维度上面求平均值.默认对所有的元素求平均.tf.reduce_mean 比如 ...
- 计算机一级电子表格TF函数,tf.slice()函数详解(极详细)
tf.slice()是TensorFlow库中分割张量的一个函数,其定义为def slice(input_, begin, size, name=None):.tf.slice()函数的那些参数设置实 ...
- tensorflow中tf.get_variable()函数详解
如果变量存在,函数tf.get_variable()会返回现有的变量:如果变量不存在,会根据给定形状和初始值创建一个新的变量. def get_variable(name, shape=None, d ...
- 【tensorflow】tf.reshape函数说明:重塑张量
转载 [471]tf.reshape函数说明_周小董-CSDN博客 https://blog.csdn.net/xc_zhou/article/details/85342542 函数原型: tf.re ...
- tf.pad函数功能介绍
tf.pad()函数主要是对张量在各个维度上进行填充,该函数的参数如下所示: pad(tensor,paddings,mode='CONSTANT',name=None ) 其中: tensor是待填 ...
- tf.reduce_max()函数的用法详解
tf.reduce_max()函数 tf.reduce_max(input_tensor,axis=None,name=None,keepdims=False #是否保持矩形原狀 ) 参数解释: in ...
最新文章
- Java线程详解(5)-线程的同步与锁
- Android 原生通知Notification 写法
- 洛谷P2055 [ZJOI2009]假期的宿舍 题解
- 编程题:编写一个函数string_copy()完成strcpy()的作用,并验证。
- ubuntu 简单配置samba
- 可观测性PHP秩判据,线性系统的可控性与可观测性
- 【算法】算法 二分查找 二分查找 查找多个相同的值
- zabbix监控端口状态
- a5松下驱动器参数设置表_松下伺服驱动器参数设置MSD043A1X
- oppo r11s鸿蒙固件,OPPO R11s刷机包
- python全栈开发包括那些_简述Python全栈工程师要学会什么
- 通过docker搭建Yapi
- 拼多多商家刚开店,哪些行为不能做?
- c语言找adc最小值,STC12C5A60S2 AD转换C语言示例程序(ADC查询方式)
- 黔程似锦——2021国庆游攻略
- Oracle 12c统一审计
- 那些入行的Python工程师们还好吗?
- UltraEdit中文破解版
- pandas把一个excel的特定数据写到另一个excel_Python读写Excel文件第三方库汇总,你想要的都在这儿!...
- SSM框架和SpringBoot框架的对比