溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

分析tensorflow中張量的提取值和賦值

發(fā)布時間:2021-11-06 10:19:13 來源:億速云 閱讀:296 作者:iii 欄目:編程語言

這篇文章主要介紹“分析tensorflow中張量的提取值和賦值”,在日常操作中,相信很多人在分析tensorflow中張量的提取值和賦值問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”分析tensorflow中張量的提取值和賦值”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

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)

創(chuàng)建一個形狀為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中張量的提取值和賦值”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI