浅谈tensorflow中张量的提取值和赋值
作者:信道者 发布时间:2022-06-23 19:20:55
tf.gather和gather_nd从params中收集数值,tf.scatter_nd 和 tf.scatter_nd_update用updates更新某一张量。严格上说,tf.gather_nd和tf.scatter_nd_update互为逆操作。
已知数值的位置,从张量中提取数值:tf.gather, tf.gather_nd
tf.gather indices每个元素(标量)是params某个axis的索引,tf.gather_nd 中indices最后一个阶对应于索引值。
tf.gather函数
函数原型
gather(
params,
indices,
validate_indices=None,
name=None,
axis=0
)
params是要查找的张量,indices是要查找值的索引(int32或int64),axis是查找轴,name是操作名。
如果indices是标量
如果indices是向量
如果indices是高阶张量
返回值:
该函数返回值类型与params相同,具体值是从params中收集过来的,形状为
tf.gather_nd函数
函数原型
gather_nd(
params,
indices,
name=None
)
indices是K阶张量,包含K-1阶的索引值。它最后一阶是索引,最后一阶维度必须小于等于params的秩。indices最后一阶的维数等于params的秩时,我们得到params的某些元素;indices最后一阶的维数小于params的秩时,我们得到params的切片。
输出张量的形状由indices的K-1阶和params索引到的形状拼接而成,如下面
indices.shape[:-1] + params.shape[indices.shape[-1]:]
参数:
params:被收集的张量。
indices:索引张量。必须是以下类型之一:int32,int64。
name:操作的名称(可选)。
返回值:
该函数返回一个张量.与params具有相同的类型。张量值从indices所给定的索引中收集,并且具有这样的形状:
已知赋值的位置,向张量赋值:tf.scatter_nd, tf.scatter_nd_update
tf.scatter_nd对零张量进行赋值,tf.scatter_nd_update对已有可变的张量进行赋值。
tf.scatter_nd函数
scatter_nd(
indices,
updates,
shape,
name=None
)
创建一个形状为shape的零张量,将updates赋值到indices指定的位置。
indices是整数张量,最内部维度对应于索引。
indices.shape[-1] <= shape.rank
如果indices.shape[-1] = shape.rank,那么indices直接对应到新张量的单个元素。如果indices.shape[-1] < shape.rank,那么indices中每个元素对新张量做切片操作。updates的形状应该如下所示
indices.shape[:-1] + shape[indices.shape[-1]:]
如果我们要把形状为(4,)的updates赋值给形状为(8,)的零张量,如下图所示。
我们需要这样子做
indices = tf.constant([[4], [3], [1], [7]])
updates = tf.constant([9, 10, 11, 12])
shape = tf.constant([8])
scatter = tf.scatter_nd(indices, updates, shape)
with tf.Session() as sess:
print(sess.run(scatter))
我们得到这样子的张量
[0, 11, 0, 10, 9, 0, 0, 12]
上面代码中,indices的形状是(4,1),updates的形状是(4,),shape的形状是(8,)。
indices.shape[:-1]+shape[indices.shape[-1]:] = (4,)+(,)=(4,)
如果我们要在三阶张量中插入两个切片,如下图所示,则应该像下面代码里所说的那样子做。
indices = tf.constant([[0], [2]])
updates = tf.constant([[[5, 5, 5, 5], [6, 6, 6, 6],
[7, 7, 7, 7], [8, 8, 8, 8]],
[[5, 5, 5, 5], [6, 6, 6, 6],
[7, 7, 7, 7], [8, 8, 8, 8]]])
shape = tf.constant([4, 4, 4])
scatter = tf.scatter_nd(indices, updates, shape)
with tf.Session() as sess:
print(sess.run(scatter))
indices的形状是(2,1),updates的形状是(2,4,4),shape的形状是(4,4,4)。
indices.shape[:-1]+shape[indices.shape[-1]:]=(2,)+(4,4)=(2,4,4)
我们会得到这样子的张量
[[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
[[5, 5, 5, 5], [6, 6, 6, 6], [7, 7, 7, 7], [8, 8, 8, 8]],
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]]
函数参数
indices:Tensor;必须是以下类型之一:int32,int64;索引值张量。
updates:Tensor;分散到输出的更新。
shape:Tensor;必须与indices具有相同的类型;1-d;得到的张量的形状。
name:操作的名称(可选)。
返回值
此函数返回一个Tensor,它与updates有相同的类型;一个有shape形状的新张量,初始化值为0,部分值根据indices用updates进行更新。
tf.scatter_nd_update函数
函数原型
scatter_nd_update(
ref,
indices,
updates,
use_locking=True,
name=None
)
scatter_nd_update也是把updates里面的值根据indices赋值到另外一个张量中,与scatter_nd不同的是,它是赋值到ref。
ref是秩为P的张量,indices是秩为Q的张量。
indices是整数类型的张量,必须具有这样的形状 。
indices最内部的维度对应于ref的某个元素或切片。
updates的形状是 ,是秩为Q-1+P-K的张量。
如果我们想要把(4,)的向量赋值到(8,)的ref中,我们可以像下面这样子操作。
ref = tf.Variable([1, 2, 3, 4, 5, 6, 7, 8])
indices = tf.constant([[4], [3], [1] ,[7]])
updates = tf.constant([9, 10, 11, 12])
update = tf.scatter_nd_update(ref, indices, updates)
with tf.Session() as sess:
print sess.run(update)
我们可以得到这样的ref
[1, 11, 3, 10, 9, 6, 7, 12]
函数参数
ref:一个可变的Tensor。
indices:一个 int32 或 int64 Tensor;一个对ref进行索引的张量.
updates:一个Tensor.必须与ref具有相同的类型;更新值张量.
use_locking:可选的bool;如果为True,则赋值将受锁定的保护;否则行为是不确定的,但可能表现出较少的争用.
name:操作的名称(可选).
返回值:
经过更新的ref。
来源:https://blog.csdn.net/chr1991/article/details/93474638
猜你喜欢
- 引用计数在Python源码中,每一个对象都是一个结构体表示,都有一个计数字段。typedef struct_object { i
- 打开在节点中加入一句:max_allowed_packet=5M 将MySQL的容量扩大到5M  
- Go pongo2 教程展示了如何使用 pongo2 模板引擎在 Golang 中使用模板。模板引擎是一个库,旨在将模板与数据结合起来以生成
- 目录1. 递归函数与回溯深搜的基础知识2. 求子集 (LeetCode 78)3. 求子集2 (LeetCode 90)4. 组合数之和(L
- <?php echo "<br/>数值强制转换:"; $string="2a";
- 前言selenium处理文件上传大致会有两种情况,一种是文件上传使用的是input标签元素,即<input type="fi
- 在asp里通过以下两个函数实现javascript里的escape函数和unescape函数加密功能。在ajax post或get时内容存在
- 在分析sIFR之前,先来快速的了解一下sIFR是什么,以及它是如何工作的。sIFR表示scalable Inman Flash Replac
- 1. 查找图像中出现的人脸代码示例:#导入face_recognition模块import face_recognition#将j
- copy模块用于对象的拷贝操作。该模块非常简单,只提供了两个主要的方法: copy.copy 与 copy.deepcopy ,分别表示浅复
- 导言(Introduction)这个提案描述了如何在jQuery的核心库中增加模板支持。更为特别是,这个提案描述了一个新的jQuery方法-
- 每月需更新某个excel表格,进行两项操作,且不覆盖原有的sheet:1. 在原来的excel表中新增sheet2. 往原有的excel表中
- Pycharm打开大文件的时候出现这个错误,根据提示以及百度知道,是因为IDEA对能关联的文件大小做了限制,主要是为了保护内存,默认值为25
- Python 操作 Excel常用工具数据处理是 Python 的一大应用场景,而 Excel 又是当前最流行的数据处理软件。因此用 Pyt
- Python容器使用的5个技巧和2个误区“容器”这两个字很少被 Python 技术文章提起。一看到“容器”,大家想到的多是那头蓝色小鲸鱼:D
- 如下所示: m_start =date +' 09:00' m_end =date +' 13:00'rsv
- 利用ACCESS可以用查询创建视图这一功能来进行查询分析。选择查询->在设计视图中创建查询,然后关闭弹出的表选择对话框,在下面的窗口上
- 本文实例讲述了PHP自定义函数用法。分享给大家供大家参考,具体如下:Demo1.php<?php //标准函数,内置函数
- 为了将excel数据自动转换成所需要的erlang数据,听同事说使用python会很方便简单,就自学了两天python,写了一个比较粗糙的p
- 本文实例讲述了Python Flask框架模板操作。分享给大家供大家参考,具体如下:模板在前面的示例中,视图函数的主要作用是生成请求的响应,