Tensorflow 在这种情况下,如何创建一个新的张量(从a中导出b)?

Tensorflow 在这种情况下,如何创建一个新的张量(从a中导出b)?,tensorflow,Tensorflow,我有一个张量'a',我想修改它的一个元素 a = tf.convert_to_tensor([[1.0, 1.0, 1.0], [1.0, 2.0, 1.0], [1.0, 1.0, 1.0]], dtype=tf.float32) 我可以得到这个元素的索引 index = tf.where(a==2) 如何从“a”派生“b” b = tf.convert_to_tensor([

我有一个张量'a',我想修改它的一个元素

 a = tf.convert_to_tensor([[1.0, 1.0, 1.0],
                           [1.0, 2.0, 1.0],
                           [1.0, 1.0, 1.0]], dtype=tf.float32)
我可以得到这个元素的索引

 index = tf.where(a==2)
如何从“a”派生“b”

 b = tf.convert_to_tensor([[1.0, 1.0, 1.0],
                           [1.0, 0.0, 1.0],
                           [1.0, 1.0, 1.0]], dtype=tf.float32)

我知道我不能用这个来修改张量。

我用tf.sparse\u to\u densed()来解决它

import tensorflow as tf

a = tf.convert_to_tensor([[1.0, 1.0, 1.0],
                         [1.0, 2.0, 1.0],
                         [1.0, 1.0, 1.0]], dtype=tf.float32)

index = tf.where(a > 1)
zero = tf.sparse_to_dense(index, tf.shape(a, out_type=tf.int64), 0., 1.)
update = tf.sparse_to_dense(index, tf.shape(a, out_type=tf.int64), 0., 0.)
b = a * zero + update

with tf.Session() as sess:
  print sess.run(b)