这是示例代码。我想使用tf.scatter_nd将值分配给新的张量。就像,updated_values = tf.scatter_nd(tf.expand_dims(indices_tf,-1),values_tf,tf.shape(values_tf))。索引张量中有一些重复的索引,这会导致在update_values张量中增加麻烦。我只想给总是与索引张量具有相同形状的信息张量分配一个权重。该代码描述了细节。
import numpy as np
import tensorflow as tf
info = np.array([0,3,4,5,6,2,1])
indices = np.array([0,1,2])
values = np.array([7,9,10])
delta_tf = tf.convert_to_tensor(info,tf.int32)
indices_tf = tf.convert_to_tensor(indices,tf.int32)
values_tf = tf.convert_to_tensor(values,tf.float32)
updated_values = tf.scatter_nd(tf.expand_dims(indices_tf,-1),values_tf,tf.shape(values_tf))
sess = tf.Session()
updated_values_ = sess.run(updated_values)
print(updated_values_)
# The updated_values_ is [7. 18. 14. 0. 0. 0. 0.].
# I would like tf.scatter_nd to assign only one value to updated_values
# at repetitive indices not adding them.
#
# So I want to make a mask from indices according to info,# the rule is that when meeting repetitive index in indices,# the mask will compare the values in info,then reset the maximum value to 1,the others to 0.
# info: [0,1]
# indices: [0,2]
# mask: [1,0]
#
# In this example,the mask will reset the position at 0,6 in info to 1,the others to 0.
# So the mask is [1,0].
mask = np.array([1,0]) # the desired mask
mask_tf = tf.convert_to_tensor(mask,tf.float32)
updated_valuess = tf.scatter_nd(tf.expand_dims(indices_tf,values_tf * mask_tf,tf.shape(values_tf))
updated_valuess_ = sess.run(updated_valuess)
print(updated_valuess_) # This output [7. 2. 4. 0. 0. 0. 0.] is what I want.
如何生成此蒙版?