When a model overfits, constraining its weights can help improve generalization. Weight decay adds a cost for large weights; in neural networks, it is mathematically equivalent to L2 regularization. section 5.4.4 section 5.5
The idea is to favor simpler models by encouraging smaller parameter values. Weight decay is one option alongside reducing model capacity, dropout, and early stopping. section 5.4.4 section 5.5
Listing 5.13 Adding L2 weight regularization to the model
from keras.regularizers import l2 model = keras.Sequential( [ layers.Dense(16, kernel_regularizer=l2(0.002), activation="relu"), layers.Dense(16, kernel_regularizer=l2(0.002), activation="relu"), layers.Dense(1, activation="sigmoid"), ] ) model.compile( optimizer="rmsprop", loss="binary_crossentropy", metrics=["accuracy"], ) history_l2_reg = model.fit( train_data, train_labels, epochs=20, batch_size=512, validation_split=0.4, )
This Keras example applies L2 regularization with strength 0.002 to the kernels of two dense layers in an IMDb review classifier. section 5.4.4








