在数学上,高斯噪声是一种通过向输出数据增加均值为零和标准差()的正态分布随机值而产生的噪声。 正态分布,也称为高斯分布,是一种间断概率分布,由其概率密度函数 (PDF) 定义:
pdf(x) = (1/ (*sqrt(2*))) *e^(- (x—)²/ (2*²))
其中 x 是随机变量, 是均值, 是标准差。
通过生成具备正态分布的随机值并将它们增加到输出数据。例如如果对图像增加高斯噪声,能够将图像示意为像素值的二维矩阵,而后应用 numpy 库 np.random.randn(rows,cols) 生成具备正态分布的随机值, 并将它们增加到图像的像素值中。 这就会失去增加了高斯噪声的新图像。
高斯噪声也称为白噪声,是一种遵从正态分布的随机噪声。 在深度学习中,训练时往往会在输出数据中退出高斯噪声,以进步模型的鲁棒性和泛化能力。 这称为数据裁减。 通过向输出数据增加噪声,模型被迫学习对输出中的渺小变动具备鲁棒性的特色,这能够帮忙它在新的、看不见的数据上体现更好。 高斯噪声也能够在训练过程中增加到神经网络的权重中以进步其性能,这种技术称为 Dropout。
让咱们先从一个简略的例子开始:
噪声的标准偏差 (noise_std) 被设置为较大的值 50,这将导致更多的噪声被增加到图像中。 能够看到噪声更加显著,并且原始图像的特色不太显著。
值得注意的是,在增加更多噪声时,须要确保噪声不超过像素值的无效范畴(即 0 到 255 之间)。 在这个例子中,np.clip() 函数用于确保噪声图像的像素值落在无效范畴内。
尽管更多的噪声可能更容易看出原始图像和噪声图像之间的差别,但它也可能使模型更难以从数据中学习有用的特色,并可能导致适度拟合或欠拟合。 所以最好从大量噪声开始,而后在监控模型性能的同时逐步减少噪声。
importcv2 importnumpyasnp # Load the image image=cv2.imread('dog.jpg') # Add Gaussian noise to the image noise_std=50 noise=np.random.randn(*image.shape) *noise_std noisy_image=np.clip(image+noise, 0, 255).astype(np.uint8) # Display the original and noisy images cv2.imshow('Original Image', image) cv2.imshow('Noisy Image', noisy_image) cv2.waitKey(0) cv2.destroyAllWindows()
高斯噪声如何用于深度学习的一些示例。
- 数据加强:高斯噪声在深度学习中的一种常见用处是在训练期间将其增加到输出数据中。 例如能够在每个图像通过模型之前增加高斯噪声。 这将迫使模型学习对输出中的渺小变动具备鲁棒性的特色,这些噪声能够代表图像上的污迹或轻微的缺失。 因而即便图像与训练数据略有不同,模型也更有可能正确辨认图像。
- Dropout:高斯噪声在深度学习中的另一个用处是在训练期间将其增加到神经网络的权重中。 这被称为Dropout。 在训练过程中,dropout 以肯定的概率(例如 0.5)随机将网络中的一些权重设置为零。 这迫使网络学习数据的多个冗余示意,使模型更强壮且不易适度拟合。
- 正则化:将高斯噪声增加到模型的参数中也能够看作是一种正则化技术。 它迫使模型具备更小的权重值,这反过来又使模型更通用并且更不容易适度拟合。
- 反抗训练:对抗性示例是专门为坑骗模型而设计的输出,在反抗训练中,模型是在用小的、有针对性的扰动加强的例子上训练的,比方高斯噪声。 这使得模型对对抗性示例更加持重。
- 半监督学习:训练时能够在输出数据中退出高斯噪声,进步半监督模型的性能。 这能够帮忙模型更好地利用无限的标记数据并学习更多的个别特色。
- 迁徙学习:微调时能够在输出数据中退出高斯噪声,以进步迁徙学习模型的性能。 这能够帮忙模型更好地适应新工作并更好地泛化到看不见的数据。
- 生成反抗网络 (GAN):能够将高斯噪声增加到生成器输出中,以进步生成样本的多样性。
- 贝叶斯深度学习:训练时能够在模型的权重中退出高斯噪声,使其对过拟合具备更强的鲁棒性,进步模型的泛化能力。
- 强化学习:在训练过程中,能够在代理的输出或动作空间中退出高斯噪声,使其对环境变动具备更强的鲁棒性,进步智能体的泛化能力。
在上述所有示例中,高斯噪声通过特定的均值和标准差,以受控形式增加到输出或权重。 指标是进步模型的性能和鲁棒性,同时又不会让模型很难从数据中学习。
上面咱们介绍如何在应用 Python 和 Keras在训练期间将高斯噪声增加到输出数据,阐明如何在训练期间将高斯噪声增加到输出数据,而后再将其传递给模型:
fromkeras.preprocessing.imageimportImageDataGenerator # Define the data generator datagen=ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization=False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range=0.1, # randomly shift images horizontally (fraction of total width) height_shift_range=0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=False, # randomly flip images vertical_flip=False, # randomly flip images noise_std=0.5 # add gaussian noise to the data with std of 0.5 ) # Use the generator to transform the data during training model.fit_generator(datagen.flow(x_train, y_train, batch_size=32), steps_per_epoch=len(x_train) /32, epochs=epochs)
Keras 的 ImageDataGenerator 类用于定义一个数据生成器,该数据生成器将指定的数据加强技术利用于输出数据。 咱们将 noise_std 设置为 0.5,这意味着标准偏差为 0.5 的高斯噪声将增加到输出数据中。 而后在调用 model.fit_generator 期间应用生成器在训练期间将数据裁减利用于输出数据。
至于Dropout,能够应用Keras中的Dropout层,设置dropout的rate,如果设置rate为0.5,那么dropout层会drop掉50%的权重。 以下是如何向模型增加 dropout 层的示例:
fromkeras.layersimportDropout model=Sequential() model.add(Dense(64, input_dim=64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(64, activation='relu')) model.add(Dense(10, activation='softmax'))
须要留神的是,标准差、Dropout的理论值将取决于具体问题和数据的特色。应用不同的值进行试验并监督模型的性能通常是一个好主见。
上面咱们介绍应用Keras 在训练期间将高斯噪声增加到输出数据和权重。为了向输出数据增加噪声,咱们能够应用 numpy 库生成随机噪声并将其增加到输出数据中。 这是如何执行此操作的示例:
importnumpyasnp # Generate some random input data x_train=np.random.rand(1000, 64) y_train=np.random.rand(1000, 10) # Add Gaussian noise to the input data noise_std=0.5 x_train_noisy=x_train+noise_std*np.random.randn(*x_train.shape) # Train the model model.fit(x_train_noisy, y_train, epochs=10)
咱们输出数据 x_train 是形态为 (1000, 64) 的二维数组,噪声是应用 np.random.randn(*x_train.shape) 生成的,它将返回具备雷同形态的正态分布均值为 0,标准差为 1的随机值数组。而后将生成的噪声与噪声的标准差 (0.5) 相乘,并将其增加到输出数据中,从而将其增加到输出数据中。
为了给权重增加噪声,咱们能够应用 Keras 中的 Dropout 层,它会在训练过程中随机抛弃一些权重。 高斯噪声是深度学习中宽泛应用的技术,在图像分类训练时能够在图像中退出高斯噪声,进步图像分类模型的鲁棒性。 这在训练数据无限或具备很大可变性时特地有用,因为模型被迫学习对输出中的小变动具备鲁棒性的特色。
以下是如何在训练期间向图像增加高斯噪声以进步图像分类模型的鲁棒性的示例:
fromkeras.preprocessing.imageimportImageDataGenerator # Define the data generator datagen=ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization=False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range=0, # randomly shift images horizontally (fraction of total width) height_shift_range=0, # randomly shift images vertically (fraction of total height) horizontal_flip=False, # randomly flip images vertical_flip=False, # randomly flip images noise_std=0.5 # add gaussian noise to the data with std of 0.5 ) # Use the generator to transform the data during training model.fit_generator(datagen.flow(x_train, y_train, batch_size=32), steps_per_epoch=len(x_train) /32, epochs=epochs)
指标检测:在指标检测模型的训练过程中,能够将高斯噪声增加到输出数据中,以使其对图像中的渺小变动(例如光照条件、遮挡和摄像机角度)更加鲁棒。
defadd_noise(image, std): """Add Gaussian noise to an image.""" noise=np.random.randn(*image.shape) *std returnnp.clip(image+noise, 0, 1) # Add noise to the training images x_train_noisy=np.array([add_noise(img, 0.1) forimginx_train]) # Train the model model.fit(x_train_noisy, y_train, epochs=10)
语音辨认:在训练过程中,能够在音频数据中退出高斯噪声,这能够帮忙模型更好地解决音频信号中的背景噪声和其余烦扰,进步语音辨认模型的鲁棒性。
defadd_noise(audio, std): """Add Gaussian noise to an audio signal.""" noise=np.random.randn(*audio.shape) *std returnaudio+noise # Add noise to the training audio x_train_noisy=np.array([add_noise(audio, 0.1) foraudioinx_train]) # Train the model model.fit(x_train_noisy, y_train, epochs=10)
生成模型:在 GAN、Generative Pre-training Transformer (GPT) 和 VAE 等生成模型中,能够在训练期间将高斯噪声增加到输出数据中,以进步模型生成新的、看不见的数据的能力。
# Generate random noise noise=np.random.randn(batch_size, 100) # Generate fake images fake_images=generator.predict(noise) # Add Gaussian noise to the fake images fake_images_noisy=fake_images+0.1*np.random.randn(*fake_images.shape) # Train the discriminator discriminator.train_on_batch(fake_images_noisy, np.zeros((batch_size, 1)))
在这个例子中,生成器被训练为基于随机噪声作为输出生成新的图像,并且在生成的图像传递给鉴别器之前,将高斯噪声增加到生成的图像中。这进步了生成器生成新的、看不见的数据的能力。
反抗训练:在反抗训练时,能够在输出数据中退出高斯噪声,使模型对反抗样本更加鲁棒。
上面的反抗训练应用疾速梯度符号法(FGSM)生成反抗样本,高斯噪声为 在训练期间将它们传递给模型之前增加到对抗性示例中。 这进步了模型对对抗性示例的鲁棒性。
# Generate adversarial examples x_adv=fgsm(model, x_train, y_train, eps=0.01) # Add Gaussian noise to the adversarial examples noise_std=0.05 x_adv_noisy=x_adv+noise_std*np.random.randn(*x_adv.shape) # Train the model model.fit(x_adv_noisy, y_train, epochs=10)
去噪:能够将高斯噪声增加到图像或信号中,模型的指标是学习去除噪声并复原原始信号。上面的例子中输出图像“x_train”首先用规范的高斯噪声毁坏 0.1 的偏差,而后将损坏的图像通过去噪主动编码器以重建原始图像。 主动编码器学习去除噪声并复原原始信号。
# Add Gaussian noise to the images noise_std=0.1 x_train_noisy=x_train+noise_std*np.random.randn(*x_train.shape) # Define the denoising autoencoder input_img=Input(shape=(28, 28, 1)) x=Conv2D(32, (3, 3), activation='relu', padding='same')(input_img) x=MaxPooling2D((2, 2), padding='same')(x) x=Conv2D(32, (3, 3), activation='relu', padding='same')(x) encoded=MaxPooling2D((2, 2), padding='same')(x) # at this point the representation is (7, 7, 32) x=Conv2D(32, (3, 3), activation='relu', padding='same')(encoded) x=UpSampling2D((2, 2))(x) x=Conv2D(32, (3, 3), activation='relu', padding='same')(x) x=UpSampling2D((2, 2))(x) decoded=Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x) autoencoder=Model(input_img, decoded) autoencoder.compile(optimizer='adam', loss='binary
异样检测:高斯噪声能够增加到失常数据中,模型的指标是学习将增加的噪声作为异样检测。
# Add Gaussian noise to the normal data noise_std=0.1 x_train_noisy=x_train+noise_std*np.random.randn(*x_train.shape) # Concatenate the normal and the noisy data x_train_concat=np.concatenate((x_train, x_train_noisy)) y_train_concat=np.concatenate((np.zeros(x_train.shape[0]), np.ones(x_train_noisy.shape[0]))) # Train the anomaly detection model model.fit(x_train_concat, y_train_concat, epochs=10)
持重优化:在优化过程中,能够将高斯噪声增加到模型的参数中,使其对参数中的小扰动更加持重。
# Define the loss function def loss_fn(params): model.set_weights(params) return model.evaluate(x_test, y_test, batch_size=32)[0] # Define the optimizer optimizer = optimizers.Adam(1e-3) # Define the step function def step_fn(params): with tf.GradientTape() as tape: loss = loss_fn(params) grads = tape.gradient(loss, params) optimizer.apply_gradients(zip(grads, params)) return params + noise_std * np.random.randn(*params.shape) # Optimize the model params = model.get_weights()
高斯噪声是深度学习中用于为输出数据或权重增加随机性的一种技术。 它是一种通过将均值为零且标准差 () 正态分布的随机值增加到输出数据中而生成的随机噪声。 向数据中增加噪声的目标是使模型对输出中的小变动更强壮,并且可能更好地解决看不见的数据。 高斯噪声可用于宽泛的利用,例如图像分类、对象检测、语音辨认、生成模型和持重优化。
https://avoid.overfit.cn/post/828b65b5323f45d3b54e39117f93ff31
作者:AI TutorMaster