0
0
TensorFlowml~10 mins

L1 and L2 regularization in TensorFlow - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add L2 regularization to a Dense layer in TensorFlow.

TensorFlow
layer = tf.keras.layers.Dense(64, kernel_regularizer=tf.keras.regularizers.[1](0.01))
Drag options to blanks, or click blank then click option'
Al2
Bl1_l2
Cl1
Dnone
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'l1' instead of 'l2' for L2 regularization.
Forgetting to specify the regularization parameter inside the parentheses.
2fill in blank
medium

Complete the code to add L1 regularization with strength 0.005 to a Dense layer.

TensorFlow
layer = tf.keras.layers.Dense(32, kernel_regularizer=tf.keras.regularizers.[1](0.005))
Drag options to blanks, or click blank then click option'
Al1
Bl2
Cl1_l2
Dnone
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'l2' instead of 'l1' for L1 regularization.
Not specifying the regularization strength correctly.
3fill in blank
hard

Fix the error in the code to correctly apply both L1 and L2 regularization.

TensorFlow
layer = tf.keras.layers.Dense(128, kernel_regularizer=tf.keras.regularizers.[1](l1=0.001, l2=0.002))
Drag options to blanks, or click blank then click option'
Al2
Bl1_l2
Cnone
Dl1
Attempts:
3 left
💡 Hint
Common Mistakes
Using only 'l1' or 'l2' when both are needed.
Not passing both l1 and l2 parameters.
4fill in blank
hard

Fill both blanks to create a model with L1 regularization on the first layer and L2 on the second.

TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, kernel_regularizer=tf.keras.regularizers.[1](0.01), activation='relu'),
    tf.keras.layers.Dense(10, kernel_regularizer=tf.keras.regularizers.[2](0.02), activation='softmax')
])
Drag options to blanks, or click blank then click option'
Al1
Bl2
Cl1_l2
Dnone
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up L1 and L2 regularizers.
Using the same regularizer for both layers.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps layer names to their regularization type strings ('L1', 'L2', or 'L1_L2').

TensorFlow
layers = {'dense1': tf.keras.regularizers.l1(0.01), 'dense2': tf.keras.regularizers.l2(0.02), 'dense3': tf.keras.regularizers.l1_l2(l1=0.001, l2=0.002)}
reg_types = {name: [1] for name, reg in layers.items() if isinstance(reg, [2])}
reg_types.update({name: 'L2' for name, reg in layers.items() if isinstance(reg, [3])})
Drag options to blanks, or click blank then click option'
A'L1'
Btf.keras.regularizers.L1
Ctf.keras.regularizers.L2
Dtf.keras.regularizers.L1L2
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names for isinstance checks.
Putting the string label in the wrong blank.