Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

additive Gaussian noise in Tensorflow

I'm trying to add Gaussian noise to a layer of my network in the following way.

def Gaussian_noise_layer(input_layer, std):
    noise = tf.random_normal(shape = input_layer.get_shape(), mean = 0.0, stddev = std, dtype = tf.float32) 
    return input_layer + noise

I'm getting the error:

ValueError: Cannot convert a partially known TensorShape to a Tensor: (?, 2600, 2000, 1)

My minibatches need to be of different sizes sometimes, so the size of the input_layer tensor will not be known until the execution time.

If I understand correctly, someone answering Cannot convert a partially converted tensor in TensorFlow suggested to set shape to tf.shape(input_layer). However then, when I try to apply a convolutional layer to that noisy layer I get another error:

ValueError: dims of shape must be known but is None

What is the correct way of achieving my goal of adding Gaussian noise to the input layer of a shape unknown until the execution time?

like image 374
Deeplearningmaniac Avatar asked Dec 15 '16 23:12

Deeplearningmaniac


People also ask

How do you add Gaussian noise to an image in Python?

We can add noise to the image using noise() function. noise function can be useful when applied before a blur operation to defuse an image. Following are the noise we can add using noise() function: gaussian.

What is noise in Tensorflow?

Gaussian Noise (GS) is a natural choice as corruption process for real valued inputs. As it is a regularization layer, it is only active at training time.

Where do you put Gaussian noise?

We can add a GaussianNoise layer as the input layer. The amount of noise must be small. Given that the input values are within the range [0, 1], we will add Gaussian noise with a mean of 0.0 and a standard deviation of 0.01, chosen arbitrarily.


1 Answers

To dynamically get the shape of a tensor with unknown dimensions you need to use tf.shape()

For instance

import tensorflow as tf
import numpy as np


def gaussian_noise_layer(input_layer, std):
    noise = tf.random_normal(shape=tf.shape(input_layer), mean=0.0, stddev=std, dtype=tf.float32) 
    return input_layer + noise


inp = tf.placeholder(tf.float32, shape=[None, 8], name='input')
noise = gaussian_noise_layer(inp, .2)
noise.eval(session=tf.Session(), feed_dict={inp: np.zeros((4, 8))})
like image 181
Junier Avatar answered Oct 19 '22 04:10

Junier