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

Method range_test

monai/optimizers/lr_finder.py:256–376  ·  view source on GitHub ↗

Performs the learning rate range test. Args: train_loader: training set data loader. val_loader: validation data loader (if desired). image_extractor: callable function to get the image from a batch of data. Default: `x["image"] if isinsta

(
        self,
        train_loader: DataLoader,
        val_loader: DataLoader | None = None,
        image_extractor: Callable = default_image_extractor,
        label_extractor: Callable = default_label_extractor,
        start_lr: float | None = None,
        end_lr: float = 10.0,
        num_iter: int = 100,
        step_mode: str = "exp",
        smooth_f: float = 0.05,
        diverge_th: int = 5,
        accumulation_steps: int = 1,
        non_blocking_transfer: bool = True,
        auto_reset: bool = True,
    )

Source from the content-addressed store, hash-verified

254 self.model.to(self.model_device)
255
256 def range_test(
257 self,
258 train_loader: DataLoader,
259 val_loader: DataLoader | None = None,
260 image_extractor: Callable = default_image_extractor,
261 label_extractor: Callable = default_label_extractor,
262 start_lr: float | None = None,
263 end_lr: float = 10.0,
264 num_iter: int = 100,
265 step_mode: str = "exp",
266 smooth_f: float = 0.05,
267 diverge_th: int = 5,
268 accumulation_steps: int = 1,
269 non_blocking_transfer: bool = True,
270 auto_reset: bool = True,
271 ) -> None:
272 """Performs the learning rate range test.
273
274 Args:
275 train_loader: training set data loader.
276 val_loader: validation data loader (if desired).
277 image_extractor: callable function to get the image from a batch of data.
278 Default: `x["image"] if isinstance(x, dict) else x[0]`.
279 label_extractor: callable function to get the label from a batch of data.
280 Default: `x["label"] if isinstance(x, dict) else x[1]`.
281 start_lr : the starting learning rate for the range test.
282 The default is the optimizer's learning rate.
283 end_lr: the maximum learning rate to test. The test may stop earlier than
284 this if the result starts diverging.
285 num_iter: the max number of iterations for test.
286 step_mode: schedule for increasing learning rate: (`linear` or `exp`).
287 smooth_f: the loss smoothing factor within the `[0, 1[` interval. Disabled
288 if set to `0`, otherwise loss is smoothed using exponential smoothing.
289 diverge_th: test is stopped when loss surpasses threshold:
290 `diverge_th * best_loss`.
291 accumulation_steps: steps for gradient accumulation. If set to `1`,
292 gradients are not accumulated.
293 non_blocking_transfer: when `True`, moves data to device asynchronously if
294 possible, e.g., moving CPU Tensors with pinned memory to CUDA devices.
295 auto_reset: if `True`, returns model and optimizer to original states at end
296 of test.
297 Returns:
298 None
299 """
300
301 # Reset test results
302 self.history = {"lr": [], "loss": []}
303 best_loss = -float("inf")
304
305 # Move the model to the proper device
306 self.model.to(self.device)
307
308 # Check if the optimizer is already attached to a scheduler
309 self._check_for_scheduler()
310
311 # Set the starting learning rate
312 if start_lr:
313 self._set_learning_rate(start_lr)

Callers 1

test_lr_finderMethod · 0.95

Calls 12

_check_for_schedulerMethod · 0.95
_set_learning_rateMethod · 0.95
_train_batchMethod · 0.95
_validateMethod · 0.95
get_lrMethod · 0.95
resetMethod · 0.95
ExponentialLRClass · 0.90
LinearLRClass · 0.90
TrainDataLoaderIterClass · 0.85
ValDataLoaderIterClass · 0.85
appendMethod · 0.45
stepMethod · 0.45

Tested by 1

test_lr_finderMethod · 0.76