Tensorflow (the way to add noise)

In the denoising autoencoder, the input of the model is the weakened form of the original input after some form of noise addition process, so the added noise is generally divided into: added Gaussian white noise, mask noise, and salt and pepper noise .

1. Additive Gaussian Noise

self.scale = tf,placeholder(dtype = tf.float32)

self.x_corrupted = tf.add(self.x, self.scale*tf.random_normal(shape = (self.n_input,)))

2. Mask noise

self.keep_prob = tf.placeholder(dtype = tf.float32)

self.x_corrupted = tf.nn.dropout(self.x, self.keep_prob)

3. Salt and pepper noise

def salt_and_pepper_noise(X,v)

  X_noise = X.copy()

  n_features = X.shape[1]

  mn = X.min()

  mx = X.max()

  for i,sample in enumerate(X):

    mask = np.random.randint(0,n_features,v)

    for m in mask:

      if np.random.rand() < .5:

        X_noise[i][m] = mn

      else:

        X_noise[i][m] = mx

    return X_noise

Explanation :

1. enumerate(X):

This function is a function in python. Its function is to form a sequence of an iterable and traversable object, and you can get the index and value at the same time. To put it bluntly, it is to group the lists and strings together. Then, use this function to return each element in it, and return the position coordinates of each element.

If we want to traverse both the elements and the index (the location of the element), we can use a for loop, just like when the salt and pepper noise above is added, i is the index (the location of the element), and sample is each element

2..np.random.randint(low,high,size)

This function looks very simple, and then generates some numbers. What are these numbers? They are taken between the defined maximum and minimum values. Then how much we take depends on the size. If it is 1... The number between ....n will generate a row of n columns of elements, if it is similar to (1,3), (2,6), etc., then it will form an array matrix

only low

np.random.randint(2,size = 5)

array([0,1,1,1,1])

np.random.randint(5,size = (3,4))

array([[1,2,3,4],

   [2,3,4,1],

   [2,1,4,0]])

WARNING : The value of low cannot be obtained. If there is high, then [low, high), that is, high cannot be obtained.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324687553&siteId=291194637