淺談tensorflow中張量的提取值和賦值
tf.gather和gather_nd從params中收集數(shù)值,tf.scatter_nd 和 tf.scatter_nd_update用updates更新某一張量。嚴(yán)格上說,tf.gather_nd和tf.scatter_nd_update互為逆操作。
已知數(shù)值的位置,從張量中提取數(shù)值:tf.gather, tf.gather_nd
tf.gather indices每個元素(標(biāo)量)是params某個axis的索引,tf.gather_nd 中indices最后一個階對應(yīng)于索引值。
tf.gather函數(shù)
函數(shù)原型
gather( params, indices, validate_indices=None, name=None, axis=0 )
params是要查找的張量,indices是要查找值的索引(int32或int64),axis是查找軸,name是操作名。
如果indices是標(biāo)量
如果indices是向量
如果indices是高階張量
返回值:
該函數(shù)返回值類型與params相同,具體值是從params中收集過來的,形狀為
tf.gather_nd函數(shù)
函數(shù)原型
gather_nd( params, indices, name=None )
indices是K階張量,包含K-1階的索引值。它最后一階是索引,最后一階維度必須小于等于params的秩。indices最后一階的維數(shù)等于params的秩時,我們得到params的某些元素;indices最后一階的維數(shù)小于params的秩時,我們得到params的切片。
輸出張量的形狀由indices的K-1階和params索引到的形狀拼接而成,如下面
indices.shape[:-1] + params.shape[indices.shape[-1]:]
參數(shù):
params:被收集的張量。
indices:索引張量。必須是以下類型之一:int32,int64。
name:操作的名稱(可選)。
返回值:
該函數(shù)返回一個張量.與params具有相同的類型。張量值從indices所給定的索引中收集,并且具有這樣的形狀:
已知賦值的位置,向張量賦值:tf.scatter_nd, tf.scatter_nd_update
tf.scatter_nd對零張量進(jìn)行賦值,tf.scatter_nd_update對已有可變的張量進(jìn)行賦值。
tf.scatter_nd函數(shù) scatter_nd( indices, updates, shape, name=None )
創(chuàng)建一個形狀為shape的零張量,將updates賦值到indices指定的位置。
indices是整數(shù)張量,最內(nèi)部維度對應(yīng)于索引。
indices.shape[-1] <= shape.rank
如果indices.shape[-1] = shape.rank,那么indices直接對應(yīng)到新張量的單個元素。如果indices.shape[-1] < shape.rank,那么indices中每個元素對新張量做切片操作。updates的形狀應(yīng)該如下所示
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,)
如果我們要在三階張量中插入兩個切片,如下圖所示,則應(yīng)該像下面代碼里所說的那樣子做。
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]]]
函數(shù)參數(shù)
indices:Tensor;必須是以下類型之一:int32,int64;索引值張量。
updates:Tensor;分散到輸出的更新。
shape:Tensor;必須與indices具有相同的類型;1-d;得到的張量的形狀。
name:操作的名稱(可選)。
返回值
此函數(shù)返回一個Tensor,它與updates有相同的類型;一個有shape形狀的新張量,初始化值為0,部分值根據(jù)indices用updates進(jìn)行更新。
tf.scatter_nd_update函數(shù)
函數(shù)原型
scatter_nd_update( ref, indices, updates, use_locking=True, name=None )
scatter_nd_update也是把updates里面的值根據(jù)indices賦值到另外一個張量中,與scatter_nd不同的是,它是賦值到ref。
ref是秩為P的張量,indices是秩為Q的張量。
indices是整數(shù)類型的張量,必須具有這樣的形狀 。
indices最內(nèi)部的維度對應(yīng)于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]
函數(shù)參數(shù)
ref:一個可變的Tensor。
indices:一個 int32 或 int64 Tensor;一個對ref進(jìn)行索引的張量.
updates:一個Tensor.必須與ref具有相同的類型;更新值張量.
use_locking:可選的bool;如果為True,則賦值將受鎖定的保護(hù);否則行為是不確定的,但可能表現(xiàn)出較少的爭用.
name:操作的名稱(可選).
返回值:
經(jīng)過更新的ref。
以上這篇淺談tensorflow中張量的提取值和賦值就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Ubuntu16安裝CUDA(9.1)和cuDNN的實(shí)現(xiàn)步驟(圖文)
本文主要介紹了Ubuntu16安裝CUDA(9.1)和cuDNN,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07Python實(shí)現(xiàn)把json格式轉(zhuǎn)換成文本或sql文件
這篇文章主要介紹了Python實(shí)現(xiàn)把json格式轉(zhuǎn)換成文本或sql文件,本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-07-07Python項(xiàng)目快速部署到Linux服務(wù)器的具體教程
Linux的開源特性和強(qiáng)大的命令行工具使得部署流程高度自動化,可重復(fù)性強(qiáng),本文將詳細(xì)介紹如何從零開始快速部署Python項(xiàng)目到Linux服務(wù)器,需要的朋友可以參考下2025-07-07