Regularization helps a model avoid memorizing training data too much. It keeps the model simple so it can work well on new data.
0
0
Why regularization prevents overfitting in TensorFlow
Introduction
When your model learns training data perfectly but fails on new data.
When your model is very complex with many layers or parameters.
When you want to improve your model's ability to generalize.
When training data is limited or noisy.
When you notice your training accuracy is much higher than validation accuracy.
Syntax
TensorFlow
model = tf.keras.Sequential([
tf.keras.layers.Dense(units, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01)),
# ... other layers ...
])Use kernel_regularizer to add regularization to layers.
Common types: L1, L2, or both (L1_L2).
Examples
Adds L2 regularization with strength 0.01 to a dense layer.
TensorFlow
tf.keras.layers.Dense(64, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01))
Adds L1 regularization with strength 0.005 to encourage sparsity.
TensorFlow
tf.keras.layers.Dense(32, activation='relu', kernel_regularizer=tf.keras.regularizers.l1(0.005))
Combines L1 and L2 regularization on the weights.
TensorFlow
tf.keras.layers.Dense(128, activation='relu', kernel_regularizer=tf.keras.regularizers.l1_l2(l1=0.001, l2=0.001))
Sample Model
This code trains two simple models on the same data. One model uses L2 regularization to keep weights small. The other does not. You can see how regularization affects training loss.
TensorFlow
import tensorflow as tf from tensorflow.keras import layers, models, regularizers import numpy as np # Create simple data: y = 2x + noise x_train = np.linspace(-1, 1, 100) y_train = 2 * x_train + np.random.normal(0, 0.1, 100) # Model without regularization model_no_reg = models.Sequential([ layers.Dense(64, activation='relu', input_shape=(1,)), layers.Dense(1) ]) model_no_reg.compile(optimizer='adam', loss='mse') # Model with L2 regularization model_reg = models.Sequential([ layers.Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.01), input_shape=(1,)), layers.Dense(1) ]) model_reg.compile(optimizer='adam', loss='mse') # Train both models history_no_reg = model_no_reg.fit(x_train, y_train, epochs=50, verbose=0) history_reg = model_reg.fit(x_train, y_train, epochs=50, verbose=0) # Print final training losses print(f"Loss without regularization: {history_no_reg.history['loss'][-1]:.4f}") print(f"Loss with L2 regularization: {history_reg.history['loss'][-1]:.4f}")
OutputSuccess
Important Notes
Regularization adds a penalty to large weights, encouraging simpler models.
L2 regularization (also called weight decay) penalizes the square of weights.
Regularization strength is controlled by a small number like 0.01; too high can hurt learning.
Summary
Regularization helps models avoid memorizing training data by keeping weights small.
This leads to better performance on new, unseen data.
Common regularization methods include L1 and L2 penalties on model weights.