Learning rate scheduling helps the model learn better by changing the speed of learning during training. It starts fast and slows down to improve accuracy.
Learning rate scheduling in TensorFlow
import tensorflow as tf # Example: create a learning rate schedule schedule = tf.keras.optimizers.schedules.ExponentialDecay( initial_learning_rate=0.1, decay_steps=10000, decay_rate=0.96, staircase=True ) # Use schedule in optimizer optimizer = tf.keras.optimizers.SGD(learning_rate=schedule)
The learning rate schedule changes the learning rate during training automatically.
Common schedules include exponential decay, step decay, and cosine decay.
schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=0.01,
decay_steps=1000,
decay_rate=0.9
)schedule = tf.keras.optimizers.schedules.PiecewiseConstantDecay(
boundaries=[1000, 2000],
values=[0.1, 0.01, 0.001]
)schedule = tf.keras.optimizers.schedules.CosineDecay(
initial_learning_rate=0.1,
decay_steps=10000
)This program trains a simple model to learn y=2x+1 using a learning rate that halves every 2 steps. It shows how the learning rate changes and the prediction after training.
import tensorflow as tf import numpy as np # Create simple data: y = 2x + 1 x_train = np.array([[1.0], [2.0], [3.0], [4.0]], dtype=np.float32) y_train = np.array([[3.0], [5.0], [7.0], [9.0]], dtype=np.float32) # Define a simple linear model model = tf.keras.Sequential([ tf.keras.layers.Dense(1, input_shape=(1,)) ]) # Define learning rate schedule lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay( initial_learning_rate=0.1, decay_steps=2, decay_rate=0.5, staircase=True ) # Use SGD optimizer with learning rate schedule optimizer = tf.keras.optimizers.SGD(learning_rate=lr_schedule) # Compile model model.compile(optimizer=optimizer, loss='mse') # Train model history = model.fit(x_train, y_train, epochs=5, verbose=2) # Predict predictions = model.predict(np.array([[5.0]])) print(f"Prediction for input 5.0: {predictions[0][0]:.4f}")
Learning rate schedules help avoid getting stuck or jumping too much during training.
Choosing the right schedule and parameters can improve model results significantly.
Learning rate scheduling changes the learning speed during training.
It helps the model learn better and reach higher accuracy.
TensorFlow offers many built-in schedules like exponential and cosine decay.
Practice
Solution
Step 1: Understand learning rate scheduling concept
Learning rate scheduling changes how fast the model learns by adjusting the learning rate during training.Step 2: Identify the main goal
The goal is to help the model learn better and reach higher accuracy by controlling the learning speed.Final Answer:
To adjust the learning speed during training for better results -> Option AQuick Check:
Learning rate scheduling = adjust learning speed [OK]
- Confusing learning rate scheduling with batch size changes
- Thinking it changes model structure
- Assuming it stops training early
Solution
Step 1: Recall TensorFlow API for exponential decay
The correct class is tf.keras.optimizers.schedules.ExponentialDecay with parameters initial_learning_rate, decay_steps, and decay_rate.Step 2: Match parameters and class name
tf.keras.optimizers.schedules.ExponentialDecay(initial_learning_rate=0.1, decay_steps=1000, decay_rate=0.96) matches the correct class and parameter names exactly.Final Answer:
tf.keras.optimizers.schedules.ExponentialDecay(initial_learning_rate=0.1, decay_steps=1000, decay_rate=0.96) -> Option AQuick Check:
Correct class and parameters = tf.keras.optimizers.schedules.ExponentialDecay(initial_learning_rate=0.1, decay_steps=1000, decay_rate=0.96) [OK]
- Using wrong module like tf.keras.optimizers directly
- Incorrect parameter names like learning_rate instead of initial_learning_rate
- Misspelling class or method names
schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=0.1,
decay_steps=1000,
decay_rate=0.5)
learning_rate = schedule(2000).numpy()Solution
Step 1: Understand exponential decay formula
Learning rate = initial_learning_rate * decay_rate^(step / decay_steps). Here, step=2000, decay_steps=1000, decay_rate=0.5.Step 2: Calculate learning rate at step 2000
Calculate exponent: 2000 / 1000 = 2.
Learning rate = 0.1 * (0.5)^2 = 0.1 * 0.25 = 0.025.Final Answer:
0.025 -> Option DQuick Check:
0.1 * 0.5^2 = 0.025 [OK]
- Forgetting to divide step by decay_steps
- Using decay_rate power step instead of step/decay_steps
- Mixing up decay_rate and initial_learning_rate
schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=0.01,
decay_steps=500,
decay_rate=0.9)
optimizer = tf.keras.optimizers.Adam(learning_rate=schedule())Solution
Step 1: Check how schedule is used
ExponentialDecay expects a step argument to compute learning rate. Calling schedule() without arguments causes an error.Step 2: Verify optimizer learning rate parameter
Optimizer accepts schedule object directly, not schedule() call. Passing schedule() calls the function immediately without step.Final Answer:
schedule() is called without step argument, causing an error -> Option BQuick Check:
Call schedule without step causes error [OK]
- Calling schedule() instead of passing schedule object
- Thinking decay_rate value causes error
- Believing Adam can't use schedules
Solution
Step 1: Understand the requirement
Learning rate halves every 1000 steps: ExponentialDecay(initial_learning_rate=0.1, decay_steps=1000, decay_rate=0.5), but never below 0.01 (needs clipping).Step 2: Evaluate schedules
ExponentialDecay provides exact halving at multiples of decay_steps but lacks built-in minimum. Custom clipping needed.Step 3: Identify correct approach
Use ExponentialDecay with initial_learning_rate=0.1, decay_steps=1000, decay_rate=0.5 and then use tf.keras.optimizers.schedules.LearningRateSchedule subclass to clip minimum at 0.01 subclasses LearningRateSchedule to wrap ExponentialDecay and clip with tf.maximum(..., 0.01).Final Answer:
Use ExponentialDecay with initial_learning_rate=0.1, decay_steps=1000, decay_rate=0.5 and then use tf.keras.optimizers.schedules.LearningRateSchedule subclass to clip minimum at 0.01 -> Option CQuick Check:
ExponentialDecay + subclass clip = halving with floor [OK]
- Using CosineDecay which does smooth decay, not discrete halving every 1000 steps
- Using PiecewiseConstantDecay incorrectly for continuous exponential decay
- Attempting to wrap without proper subclassing
Ready to prove it?
Pass the test and this pattern moves unknown
