v9.1.1
Menu

Per-Layer Learning Rates

easyFREECustom OptimizersPyTorch Advanced

Problem Statement

Configure different learning rates for different layers using parameter groups.

Background

PyTorch optimizers accept a list of parameter groups, each with its own hyperparameters. This is essential for fine-tuning where you want lower LR for pretrained layers.

Your Task

The starter code creates a 2-layer model. Create an Adam optimizer with different learning rates per layer: 0.001 for fc1 and 0.01 for fc2.

Output Format

Returns a dictionary with "num_param_groups", "group0_lr", "group1_lr", "group0_num_params", and "group1_num_params".

Example

Input
None
Output
{'num_param_groups': 2, 'group0_lr': 0.001, 'group1_lr': 0.01, 'group0_num_params': 2, 'group1_num_params': 2}
Reasoning
  • The function takes no arguments. It builds a fixed two-layer network: fc1 maps 4→84 \to 8, a ReLU follows, and fc2 maps 8→28 \to 2. The ReLU has no learnable weights.
  • The model's learnable tensors are split into two groups, each with its own step size η\eta. Group 0 holds fc1 with η0=0.001\eta_0 = 0.001 and group 1 holds fc2 with η1=0.01\eta_1 = 0.01. So num_param_groups is 2.
  • With Adam, every update in a group is scaled by that group's η\eta. So the output layer moves η1/η0=10\eta_1 / \eta_0 = 10 times faster than the first layer.
  • Each linear layer has two tensors, a weight and a bias. For fc1 they are an 8×48 \times 4 matrix (32 numbers) and a length-8 vector. For fc2 they are a 2×82 \times 8 matrix (16 numbers) and a length-2 vector.
  • Notice that group0_num_params and group1_num_params are both 2. They count tensors, not scalars: 40 values and 18 values both come out as 2. The group order also matters, because the first group listed becomes group 0.

Constraints

  • Two parameter groups with different LRs
  • fc1: lr=0.001, fc2: lr=0.01
  • Use Adam optimizer

Learn this in

More from PyTorch Advanced

solution.py

Test results

Sign in to run your code against the test cases.