首页 > python教程

浅谈tensorflow中张量的提取值和赋值

时间:2020-10-31 python教程 查看: 887

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。

以上这篇浅谈tensorflow中张量的提取值和赋值就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持python博客。

展开全文
上一篇:Python log模块logging记录打印用法解析
下一篇:python系统指定文件的查找只输出目录下所有文件及文件夹
输入字:
相关知识
Python 实现图片色彩转换案例

我们在看动漫、影视作品中,当人物在回忆过程中,体现出来的画面一般都是黑白或者褐色的。本文将提供将图片色彩转为黑白或者褐色风格的案例详解,感兴趣的小伙伴可以了解一下。

python初学定义函数

这篇文章主要为大家介绍了python的定义函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助,希望能够给你带来帮助

图文详解Python如何导入自己编写的py文件

有时候自己写了一个py文件,想要把它导入到另一个py文件里面,所以下面这篇文章主要给大家介绍了关于Python如何导入自己编写的py文件的相关资料,需要的朋友可以参考下

python二分法查找实例代码

二分算法是一种效率比较高的查找算法,其输入的是一个有序的元素列表,如果查找元素包含在列表中,二分查找返回其位置,否则返回NONE,下面这篇文章主要给大家介绍了关于python二分法查找的相关资料,需要的朋友可以参考下