簡體   English   中英

在tf.where()給定的索引處設置張量值

[英]Setting values of a tensor at the indices given by tf.where()

我正在嘗試將噪聲添加到容納圖像灰度像素值的張量中。 我想將像素值的隨機數設置為255。

我在考慮以下方面:

random = tf.random_normal(tf.shape(input))
mask = tf.where(tf.greater(random, 1))

然后嘗試找出如何為mask每個索引將input的像素值設置為255。

tf.where()也可以用於此,在mask元素為True下分配噪聲值,否則為原始input值:

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.]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM