MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / LearningRateFinder

Class LearningRateFinder

monai/optimizers/lr_finder.py:146–549  ·  view source on GitHub ↗

Learning rate range test. The learning rate range test increases the learning rate in a pre-training run between two boundaries in a linear or exponential manner. It provides valuable information on how well the network can be trained over a range of learning rates and what is the o

Source from the content-addressed store, hash-verified

144
145
146class LearningRateFinder:
147 """Learning rate range test.
148
149 The learning rate range test increases the learning rate in a pre-training run
150 between two boundaries in a linear or exponential manner. It provides valuable
151 information on how well the network can be trained over a range of learning rates
152 and what is the optimal learning rate.
153
154 Example (fastai approach):
155 >>> lr_finder = LearningRateFinder(net, optimizer, criterion)
156 >>> lr_finder.range_test(data_loader, end_lr=100, num_iter=100)
157 >>> lr_finder.get_steepest_gradient()
158 >>> lr_finder.plot() # to inspect the loss-learning rate graph
159
160 Example (Leslie Smith's approach):
161 >>> lr_finder = LearningRateFinder(net, optimizer, criterion)
162 >>> lr_finder.range_test(train_loader, val_loader=val_loader, end_lr=1, num_iter=100, step_mode="linear")
163
164 Gradient accumulation is supported; example:
165 >>> train_data = ... # prepared dataset
166 >>> desired_bs, real_bs = 32, 4 # batch size
167 >>> accumulation_steps = desired_bs // real_bs # required steps for accumulation
168 >>> data_loader = torch.utils.data.DataLoader(train_data, batch_size=real_bs, shuffle=True)
169 >>> acc_lr_finder = LearningRateFinder(net, optimizer, criterion)
170 >>> acc_lr_finder.range_test(data_loader, end_lr=10, num_iter=100, accumulation_steps=accumulation_steps)
171
172 By default, image will be extracted from data loader with x["image"] and x[0], depending on whether
173 batch data is a dictionary or not (and similar behaviour for extracting the label). If your data loader
174 returns something other than this, pass a callable function to extract it, e.g.:
175 >>> image_extractor = lambda x: x["input"]
176 >>> label_extractor = lambda x: x[100]
177 >>> lr_finder = LearningRateFinder(net, optimizer, criterion)
178 >>> lr_finder.range_test(train_loader, val_loader, image_extractor, label_extractor)
179
180 References:
181 Modified from: https://github.com/davidtvs/pytorch-lr-finder.
182 Cyclical Learning Rates for Training Neural Networks: https://arxiv.org/abs/1506.01186
183 """
184
185 def __init__(
186 self,
187 model: nn.Module,
188 optimizer: Optimizer,
189 criterion: torch.nn.Module,
190 device: str | torch.device | None = None,
191 memory_cache: bool = True,
192 cache_dir: str | None = None,
193 amp: bool = False,
194 pickle_module: types.ModuleType = pickle,
195 pickle_protocol: int = DEFAULT_PROTOCOL,
196 verbose: bool = True,
197 ) -> None:
198 """Constructor.
199
200 Args:
201 model: wrapped model.
202 optimizer: wrapped optimizer.
203 criterion: wrapped loss function.

Callers 1

test_lr_finderMethod · 0.90

Calls

no outgoing calls

Tested by 1

test_lr_finderMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…