如何在这种情况下创建一个新的张量(从a派生出来)?

时间:2017-05-26 09:56:49

标签: tensorflow

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

 index = tf.where(a==2)

我可以获得该元素的索引。

 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)

如何推导' b'来自' a'?

setBorder()

我知道我无法修改Date.prototype.isPrototypeOf中的张量。

1 个答案:

答案 0 :(得分:0)

我使用tf.sparse_to_dense()

解决了这个问题
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)