Setting Values Of A Tensor At The Indices Given By Tf.where()
I am attempting to add noise to a tensor that holds the greyscale pixel values of an image. I want to set a random number of the pixels values to 255. I was thinking something alon
Solution 1:
tf.where()
can be used for this too, assigning the noise value where mask elements are True
, else the original input
value:
import tensorflow as tf
input = tf.zeros((4, 4))
noise_value = 255.
random = tf.random_normal(tf.shape(input))
mask = tf.greater(random, 1.)
input_noisy = tf.where(mask, tf.ones_like(input) * noise_value, input)
with tf.Session() as sess:
print(sess.run(input_noisy))
# [[ 0. 255. 0. 0.]
# [ 0. 0. 0. 0.]
# [ 0. 0. 0. 255.]
# [ 0. 255. 255. 255.]]
Post a Comment for "Setting Values Of A Tensor At The Indices Given By Tf.where()"