一尘不染

如何在训练过程中添加具有不同std的高斯噪声?

python

我正在使用keras和tensorflow训练CNN。我想在训练期间将高斯噪声添加到我的输入数据中,并在以后的步骤中降低噪声的百分比。我现在使用的是:

from tensorflow.python.keras.layers import Input, GaussianNoise, BatchNormalization
inputs = Input(shape=x_train_n.shape[1:])
bn0 = BatchNormalization(axis=1, scale=True)(inputs)
g0 = GaussianNoise(0.5)(bn0)

GaussianNoise所采用的变量是噪声分布的标准偏差,我无法为其分配动态值,如何添加例如噪声,然后根据自己所处的时期减小该值?


阅读 237

收藏
2021-01-20

共1个答案

一尘不染

您可以简单地设计一个自定义callback,以更改stddev某个时期的训练前的习惯。

参考:

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>
2021-01-20