W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
scatter_nd(
indices,
updates,
shape,
name=None
)
參見指南:張量變換>分割和連接
根據(jù)indices將updates散布到新的(初始為零)張量.
根據(jù)索引對(duì)給定shape的零張量中的單個(gè)值或切片應(yīng)用稀疏updates來創(chuàng)建新的張量.此運(yùn)算符是tf.gather_nd運(yùn)算符的反函數(shù),它從給定的張量中提取值或切片.
警告:更新應(yīng)用的順序是非確定性的,所以如果indices包含重復(fù)項(xiàng)的話,則輸出將是不確定的.
indices是一個(gè)整數(shù)張量,其中含有索引形成一個(gè)新的形狀shape張量.indices的最后的維度可以是shape的最多的秩:
indices.shape[-1] <= shape.rank
indices的最后一個(gè)維度對(duì)應(yīng)于沿著shape的indices.shape[-1]維度的元素的索引(if indices.shape[-1] = shape.rank)或切片(if indices.shape[-1] < shape.rank)的索引.updates是一個(gè)具有如下形狀的張量:
indices.shape[:-1] + shape[indices.shape[-1]:]
最簡(jiǎn)單的分散形式是通過索引將單個(gè)元素插入到張量中.例如,假設(shè)我們想要在8個(gè)元素的1級(jí)張量中插入4個(gè)分散的元素.
在Python中,這個(gè)分散操作看起來像這樣:
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))
由此產(chǎn)生的張量將如下所示:
[0, 11, 0, 10, 9, 0, 0, 12]
我們也可以一次插入一個(gè)更高階張量的整個(gè)片.例如,如果我們想要在具有兩個(gè)新值的矩陣的第三維張量中插入兩個(gè)切片.
在Python中,這個(gè)分散操作看起來像這樣:
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))
由此產(chǎn)生的張量將如下所示:
[[[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ù)將返回一個(gè)Tensor,它與updates有相同的類型;根據(jù)indices應(yīng)用的一個(gè)新具有給定的形狀和更新的張量.
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: