2017-03-20 32 views
1

我有張量和相同等級的索引張量。我想將與索引張量中的索引對應的張量的值設置爲某個標量。我該怎麼做呢?Numpy陣列的Tensorflow當量[indices] =標量

換句話說,我正在尋找的Tensorflow相當於以下numpy的操作:

array[indices] = scalar 

在我的具體情況下,我們談論的是一個一維張量:

mask = tf.zeros_like(some_1D_tensor) 

(e.g. mask = [0, 0, 0, 0, 0]) 

indices是一維張量,其中包含我想設置爲標量值1的mask索引。所以我想:

mask[indices] = 1 

(e.g. for indices = [1, 3] the output should be mask == [0, 1, 0, 1, 0]) 

回答

2

我不知道,如果它之前,或者如果我只是還沒有看到它是不存在的,但一般情況下相當於

array[indices] = scalar 

tensor = tf.scatter_nd_update(tensor, indices, updates) 

使用tf.scatter_nd_update()