L1 regularization adds the sum of the absolute values of the model's weights to the loss. It helps make the model simpler by pushing some weights to zero, which can remove unimportant features.
Click to reveal answer
beginner
What does L2 regularization do to a model's weights?
L2 regularization adds the sum of the squares of the model's weights to the loss. It encourages smaller weights overall but does not force them to zero, helping the model avoid overfitting.
Click to reveal answer
intermediate
How do L1 and L2 regularization help prevent overfitting?
Both add a penalty to the loss based on the size of weights. This penalty discourages the model from fitting noise in the training data, making it generalize better to new data.
Click to reveal answer
beginner
Show a simple TensorFlow code snippet to add L2 regularization to a Dense layer.
import tensorflow as tf
from tensorflow.keras import layers, regularizers
model = tf.keras.Sequential([
layers.Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.01)),
layers.Dense(1)
])
Click to reveal answer
intermediate
What is the main difference in the effect of L1 vs L2 regularization on model weights?
L1 regularization can make some weights exactly zero, effectively selecting features. L2 regularization makes weights smaller but usually keeps them all nonzero.
Click to reveal answer
Which regularization method can lead to sparse models by setting some weights to zero?
ADropout
BL2 regularization
CL1 regularization
DBatch normalization
✗ Incorrect
L1 regularization adds absolute value penalties that can push weights exactly to zero, creating sparsity.
What does L2 regularization add to the loss function?
ASum of absolute weights
BSum of weight inverses
CSum of weights
DSum of squared weights
✗ Incorrect
L2 regularization adds the sum of the squares of the weights to the loss.
In TensorFlow, which argument adds L2 regularization to a Dense layer?
Abias_regularizer
Bkernel_regularizer
Cactivity_regularizer
Ddropout_rate
✗ Incorrect
The kernel_regularizer argument applies regularization to the layer's weights.
Why do we use regularization in machine learning models?
ATo prevent overfitting
BTo reduce dataset size
CTo speed up training
DTo increase model complexity
✗ Incorrect
Regularization helps prevent overfitting by penalizing large weights.
Which regularization method is more likely to keep all weights small but nonzero?
AL2 regularization
BEarly stopping
CL1 regularization
DData augmentation
✗ Incorrect
L2 regularization shrinks weights but usually does not make them exactly zero.
Explain in your own words how L1 and L2 regularization help a model avoid overfitting.
Think about how adding extra cost to big weights changes the model.
You got /4 concepts.
Write a short TensorFlow code example showing how to add L1 regularization to a Dense layer.
Use tensorflow.keras.layers and regularizers.l1 with a small value.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using L1 and L2 regularization in TensorFlow models?
easy
A. To change the activation function of the model
B. To increase the size of the model weights
C. To prevent overfitting by adding penalties to large weights
D. To speed up the training process by skipping layers
Solution
Step 1: Understand the role of regularization
L1 and L2 regularization add penalties to the loss function based on the size of the weights to reduce overfitting.
Step 2: Identify the effect on model weights
These penalties discourage large weights, helping the model generalize better on new data.
Final Answer:
To prevent overfitting by adding penalties to large weights -> Option C
Quick Check:
Regularization = Prevent overfitting [OK]
Hint: Regularization adds penalty to weights to avoid overfitting [OK]
Common Mistakes:
Thinking regularization speeds up training
Confusing regularization with activation functions
Believing regularization increases weight size
2. Which of the following is the correct way to add L2 regularization to a Dense layer in TensorFlow?
easy
A. Dense(64, kernel_regularizer=tf.keras.regularizers.l2(0.01))
B. Dense(64, regularizer=tf.keras.l2(0.01))
C. Dense(64, kernel_regularizer=tf.keras.regularizers.l1(0.01))
D. Dense(64, kernel_regularizer=tf.keras.regularizers.l1_l2(0.01))
Solution
Step 1: Recall the correct argument name
The argument to add regularization in a Dense layer is kernel_regularizer.
Step 2: Use the correct regularizer function
For L2 regularization, use tf.keras.regularizers.l2() with the penalty value.
Final Answer:
Dense(64, kernel_regularizer=tf.keras.regularizers.l2(0.01)) -> Option A
A. Missing parentheses after l2; fix with l2(0.01)
B. kernel_regularizer should be regularizer
C. Dense layer does not support regularization
D. l2 should be replaced with l1
Solution
Step 1: Identify the error in regularizer usage
The code uses tf.keras.regularizers.l2 without calling it as a function.
Step 2: Correct the syntax by adding parentheses and penalty value
Use tf.keras.regularizers.l2(0.01) to create the regularizer instance.
Final Answer:
Missing parentheses after l2; fix with l2(0.01) -> Option A
Quick Check:
Call l2() with penalty value [OK]
Hint: Always call l1 or l2 with parentheses and penalty [OK]
Common Mistakes:
Forgetting parentheses after l1 or l2
Using 'regularizer' instead of 'kernel_regularizer'
Thinking Dense layer can't have regularization
5. You want to build a TensorFlow model that uses both L1 and L2 regularization on the kernel weights of a Dense layer with penalties 0.001 and 0.01 respectively. Which code snippet correctly applies this?
hard
A. Dense(128, kernel_regularizer=tf.keras.regularizers.l2(0.01), kernel_regularizer=tf.keras.regularizers.l1(0.001))
B. Dense(128, kernel_regularizer=tf.keras.regularizers.l1_l2(l1=0.001, l2=0.01))
C. Dense(128, kernel_regularizer=tf.keras.regularizers.l1(0.001), bias_regularizer=tf.keras.regularizers.l2(0.01))
D. Dense(128, kernel_regularizer=tf.keras.regularizers.l1(0.001) + tf.keras.regularizers.l2(0.01))
Solution
Step 1: Understand how to combine L1 and L2 regularization
TensorFlow provides l1_l2 regularizer to apply both penalties together on the same weights.
Step 2: Check the correct syntax for l1_l2
Use tf.keras.regularizers.l1_l2(l1=0.001, l2=0.01) as the kernel_regularizer argument.
Final Answer:
Dense(128, kernel_regularizer=tf.keras.regularizers.l1_l2(l1=0.001, l2=0.01)) -> Option B
Quick Check:
Use l1_l2() for combined regularization [OK]
Hint: Use l1_l2() to combine L1 and L2 penalties [OK]
Common Mistakes:
Trying to add two kernel_regularizer arguments
Adding regularizers instead of using l1_l2
Applying L1 to kernel and L2 to bias instead
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.