偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

終于把神經(jīng)網(wǎng)絡(luò)中的正則化技術(shù)搞懂了??!

人工智能
模型在訓(xùn)練過程中會定期在驗(yàn)證集上進(jìn)行評估,如果驗(yàn)證集上的損失開始增大(即驗(yàn)證集的性能變差),則認(rèn)為模型可能已經(jīng)過擬合。早停法會在驗(yàn)證損失不再下降時(shí)停止訓(xùn)練,以防止模型繼續(xù)在訓(xùn)練集上過度擬合。

大家好,我是小寒

今天給大家分享神經(jīng)網(wǎng)絡(luò)中常用的正則化技術(shù)。

神經(jīng)網(wǎng)絡(luò)中的正則化技術(shù)是用于防止模型過擬合的一系列方法。

過擬合通常發(fā)生在模型在訓(xùn)練數(shù)據(jù)上表現(xiàn)得很好,但在測試數(shù)據(jù)上表現(xiàn)不佳,這意味著模型在訓(xùn)練過程中學(xué)習(xí)到了數(shù)據(jù)中的噪聲或細(xì)節(jié),而非通用的模式。

神經(jīng)網(wǎng)絡(luò)中常用的正則化技術(shù)包括

  • 早停法
  • L1 和 L2 正則化
  • Dropout
  • 數(shù)據(jù)增強(qiáng)
  • 添加噪聲
  • Batch Normalization

早停法

早停法是一種簡單但非常有效的正則化技術(shù)。

模型在訓(xùn)練過程中會定期在驗(yàn)證集上進(jìn)行評估,如果驗(yàn)證集上的損失開始增大(即驗(yàn)證集的性能變差),則認(rèn)為模型可能已經(jīng)過擬合。

早停法會在驗(yàn)證損失不再下降時(shí)停止訓(xùn)練,以防止模型繼續(xù)在訓(xùn)練集上過度擬合。

圖片

import tensorflow as tf

# Creating a simple neural network model
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(64, activatinotallow='relu', input_shape=(100,)),
    tf.keras.layers.Dense(32, activatinotallow='relu'),
    tf.keras.layers.Dense(1, activatinotallow='sigmoid')
])

# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Using EarlyStopping callback
early_stopping = tf.keras.callbacks.EarlyStopping(
    mnotallow='val_loss',  # Monitoring validation loss
    patience=5,  # Number of epochs with no improvement to wait before stopping
    restore_best_weights=True  # Restores the weights of the best epoch
)

# Train the model with early stopping
model.fit(X_train, y_train, validation_split=0.2, epochs=100, callbacks=[early_stopping])

L1 和 L2 正則化

L1正則化

import tensorflow as tf
from tensorflow.keras import regularizers

# Creating a simple neural network model with L1 regularization
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=(100,),
                          kernel_regularizer=regularizers.l1(0.01)),  # L1 Regularization
    tf.keras.layers.Dense(32, activation='relu',
                          kernel_regularizer=regularizers.l1(0.01)),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Summary of the model
model.summary()

L2正則化

L2 正則化則在損失函數(shù)中加入權(quán)重平方和作為懲罰項(xiàng),其公式為

正則化通過懲罰大權(quán)重的參數(shù),迫使權(quán)重的分布更加均勻,防止模型對訓(xùn)練數(shù)據(jù)中的特定特征過于敏感。

它不會像L1那樣產(chǎn)生稀疏解,但可以有效控制模型的復(fù)雜度。

import tensorflow as tf
from tensorflow.keras import regularizers

# Creating a neural network model with L2 regularization
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=(100,),
                          kernel_regularizer=regularizers.l2(0.01)),  # L2 Regularization
    tf.keras.layers.Dense(32, activation='relu',
                          kernel_regularizer=regularizers.l2(0.01)),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Summary of the model
model.summary()

Dropout

Dropout 是一種非常流行的正則化方法,尤其在深度神經(jīng)網(wǎng)絡(luò)中。

訓(xùn)練過程中,Dropout 隨機(jī)地“關(guān)閉”一部分神經(jīng)元及其連接,使得網(wǎng)絡(luò)在每次訓(xùn)練迭代中只使用部分神經(jīng)元進(jìn)行前向傳播和反向傳播。

Dropout 可以防止神經(jīng)元之間的共適應(yīng)性,提高網(wǎng)絡(luò)的泛化能力。

圖片圖片


import tensorflow as tf

# Creating a neural network model with Dropout
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(128, activatinotallow='relu', input_shape=(100,)),
    tf.keras.layers.Dropout(0.5),  # 50% Dropout
    tf.keras.layers.Dense(64, activatinotallow='relu'),
    tf.keras.layers.Dropout(0.5),  # 50% Dropout
    tf.keras.layers.Dense(10, activatinotallow='softmax')
])

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Summary of the model
model.summary()

數(shù)據(jù)增強(qiáng)

數(shù)據(jù)增強(qiáng)是通過對訓(xùn)練數(shù)據(jù)進(jìn)行一些隨機(jī)變換(如旋轉(zhuǎn)、翻轉(zhuǎn)、縮放、裁剪等),人為地?cái)U(kuò)充數(shù)據(jù)集的規(guī)模,使模型能夠看到更多的“不同”的數(shù)據(jù),從而減少過擬合。

這些變換不會改變數(shù)據(jù)的標(biāo)簽,但會增加訓(xùn)練數(shù)據(jù)的多樣性,迫使模型對不同的輸入具有更強(qiáng)的魯棒性。

圖片圖片


from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Create an ImageDataGenerator with augmentation
datagen = ImageDataGenerator(
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest'
)

# Example of applying the augmentation to an image
# Assuming 'images' is a numpy array of images
augmented_images = datagen.flow(images, batch_size=32)

# Use the augmented data for training
model.fit(augmented_images, epochs=10)

添加噪聲

在訓(xùn)練過程中,向輸入或隱藏層的神經(jīng)元加入隨機(jī)噪聲,以增強(qiáng)模型的魯棒性。

例如,可以向輸入數(shù)據(jù)中加入高斯噪聲或其他分布的噪聲。

這樣模型可以在面對真實(shí)數(shù)據(jù)中的擾動或噪聲時(shí)表現(xiàn)得更好,從而提升泛化能力。

圖片圖片


Batch Normalization

批歸一化(Batch Normalization)也是一種廣泛使用的正則化技術(shù),它的主要目的是解決訓(xùn)練過程中的“內(nèi)部協(xié)變量偏移”,即網(wǎng)絡(luò)的每一層輸入分布在訓(xùn)練過程中不斷變化的問題。

BN 將每一批數(shù)據(jù)的輸入進(jìn)行歸一化,使得輸入數(shù)據(jù)的均值為0,方差為1,然后再對其進(jìn)行縮放和平移:

責(zé)任編輯:武曉燕 來源: 程序員學(xué)長
相關(guān)推薦

2024-09-12 08:28:32

2024-10-17 13:05:35

神經(jīng)網(wǎng)絡(luò)算法機(jī)器學(xué)習(xí)深度學(xué)習(xí)

2024-07-24 08:04:24

神經(jīng)網(wǎng)絡(luò)激活函數(shù)

2024-11-07 08:26:31

神經(jīng)網(wǎng)絡(luò)激活函數(shù)信號

2024-12-02 01:10:04

神經(jīng)網(wǎng)絡(luò)自然語言DNN

2024-10-28 00:38:10

2024-11-15 13:20:02

2025-02-21 08:29:07

2024-12-12 00:29:03

2024-09-20 07:36:12

2024-10-05 23:00:35

2024-07-17 09:32:19

2024-09-23 09:12:20

2024-10-16 07:58:48

2024-12-03 08:16:57

2024-08-01 08:41:08

2024-10-08 15:09:17

2024-10-08 10:16:22

2024-10-28 15:52:38

機(jī)器學(xué)習(xí)特征工程數(shù)據(jù)集

2024-10-30 08:23:07

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號