How to add Gaussian noise with varying std during training?

后端 未结 1 1987
情歌与酒
情歌与酒 2020-12-20 05:47

I am training a CNN using keras and tensorflow. I would like to add Gaussian noise to my input data during training and reduce the percentage of the noise in further steps.

相关标签:
1条回答
  • 2020-12-20 06:18

    You can simply design a custom callback which changes the stddev before training for a epoch.

    Reference:

    https://www.tensorflow.org/api_docs/python/tf/keras/layers/GaussianNoise

    https://www.tensorflow.org/guide/keras/custom_callback

    from tensorflow.keras.layers import Input, Dense, Add, Activation
    from tensorflow.keras.models import Model
    import tensorflow as tf
    import numpy as np
    import random
    
    
    from tensorflow.python.keras.layers import Input, GaussianNoise, BatchNormalization
    inputs = Input(shape=100)
    bn0 = BatchNormalization(axis=1, scale=True)(inputs)
    g0 = GaussianNoise(0.5)(bn0) 
    d0 = Dense(10)(g0)
    model = Model(inputs, d0)
    
    model.compile('adam', 'mse')
    model.summary()
    
    
    class MyCustomCallback(tf.keras.callbacks.Callback):
    
      def on_epoch_begin(self, epoch, logs=None):
        self.model.layers[2].stddev = random.uniform(0, 1)
        print('updating sttdev in training')
        print(self.model.layers[2].stddev)
    
    
    X_train = np.zeros((10,100))
    y_train = np.zeros((10,10))
    
    noise_change = MyCustomCallback()
    model.fit(X_train, 
              y_train, 
              batch_size=32, 
              epochs=5, 
              callbacks = [noise_change])
    
    
    Model: "model_5"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    input_6 (InputLayer)         [(None, 100)]             0         
    _________________________________________________________________
    batch_normalization_5 (Batch (None, 100)               400       
    _________________________________________________________________
    gaussian_noise_5 (GaussianNo (None, 100)               0         
    _________________________________________________________________
    dense_5 (Dense)              (None, 10)                1010      
    =================================================================
    Total params: 1,410
    Trainable params: 1,210
    Non-trainable params: 200
    _________________________________________________________________
    Epoch 1/5
    updating sttdev in training
    0.984045691131548
    1/1 [==============================] - 0s 1ms/step - loss: 1.6031
    Epoch 2/5
    updating sttdev in training
    0.02821459469022025
    1/1 [==============================] - 0s 742us/step - loss: 1.5966
    Epoch 3/5
    updating sttdev in training
    0.6102984511769268
    1/1 [==============================] - 0s 1ms/step - loss: 1.8818
    Epoch 4/5
    updating sttdev in training
    0.021155188690323512
    1/1 [==============================] - 0s 1ms/step - loss: 1.2032
    Epoch 5/5
    updating sttdev in training
    0.35950227285165115
    1/1 [==============================] - 0s 2ms/step - loss: 1.8817
    
    <tensorflow.python.keras.callbacks.History at 0x7fc67ce9e668>
    
    0 讨论(0)
提交回复
热议问题