Skip to main content
 首页 » 编程设计

python之如何在 tensorflow 中的张量中执行项目分配

2024年08月15日32cloudgamer

设一个张量为a = [0,0,0,0,0,0,0,0],另一个张量为b = [1,3,0,5 ],这里我想要一个 tensorflow 操作,将 1 放入张量 a 中,从张量 b 中获取位置值。因此,输出张量将为 [1,1,0,1,0,1,0,0]。 如何解决这个问题呢?

请您参考如下方法:

这个呢?

a = tf.Variable([0,0,0,0,0,0,0,0]) 
b = tf.Variable([1,3,0,5]) 
 
with tf.Session() as sess: 
 
    sess.run(tf.global_variables_initializer()) 
 
    update = tf.scatter_update(a, 
                               b, 
                               tf.tile(tf.constant([1], 
                                       tf.int32), 
                               b.shape)) 
 
    print(update.eval(session=sess)) 

输出是

[1 1 0 1 0 1 0 0]