本文介绍了如何在训练期间添加具有不同标准的高斯噪声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 keras 和 tensorflow 训练 CNN.我想在训练期间将高斯噪声添加到我的输入数据中,并在进一步的步骤中降低噪声的百分比.我现在在做什么,我使用:

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. What I do right now, I use:

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 采用的变量是噪声分布的标准偏差,我无法为其分配动态值,我如何添加例如噪声,然后根据我所处的时代减少该值?

The variable that GaussianNoise takes is the standard deviation of the noise distribution and I couldn't assign a dynamic value to it, how can I add for example a noise, and then decrease this value based on the epoch that I am in?

推荐答案

您可以简单地设计一个自定义的 callback,它可以在训练一个 epoch 之前更改 stddev.

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

参考:

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>

这篇关于如何在训练期间添加具有不同标准的高斯噪声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 10:00