關(guān)于tf.sactter找了好多blog都沒有找到比較詳細的說明糯笙,一般都是翻譯一下官方文檔的列子梭冠,只對簡單情況做了說明,但是稍微復雜一點的沒做解釋装蓬。自己英文沒怎么看明白怖糊,所以就自己實驗一下這個函數(shù)到底怎么玩的。下面具體看例子峡迷。
簡單情況
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)
print(scatter)
結(jié)果如下:
[0, 11, 0, 10, 9, 0, 0, 12]
如上所示银伟,indice是shape中的索引,shape是初始化一個0矩陣绘搞,將updates中的值按照indices插到具體的位置上彤避。
再看一個復雜情況:
shape = [4, 4]
indices = np.array([[[0,1],[1,3]],
[[2,2],[2,2]]])
indices = tf.constant(indices)
update = tf.constant(np.arange(4).reshape(2,2))
tf.scatter_nd(indices, update, [4,4])
結(jié)果如下:
array([[0, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 5, 0],
[0, 0, 0, 0]])>
結(jié)果就很顯然了,對于updates來說夯辖,update的每一個值的對應(yīng)的位置對應(yīng)一個indice的索引,索引是指向shape,例如琉预,shape[0, 1] = update[0,0], shape[2,2] = update[1,0] + update[1,1], indices比update多一個維度。