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

Class SupervisedTrainer

monai/engines/trainer.py:80–309  ·  view source on GitHub ↗

Standard supervised training method with image and label, inherits from ``Trainer`` and ``Workflow``. Args: device: an object representing the device on which to run. max_epochs: the total epoch number for trainer to run. train_data_loader: Ignite engine use data_lo

Source from the content-addressed store, hash-verified

78
79
80class SupervisedTrainer(Trainer):
81 """
82 Standard supervised training method with image and label, inherits from ``Trainer`` and ``Workflow``.
83
84 Args:
85 device: an object representing the device on which to run.
86 max_epochs: the total epoch number for trainer to run.
87 train_data_loader: Ignite engine use data_loader to run, must be Iterable or torch.DataLoader.
88 network: network to train in the trainer, should be regular PyTorch `torch.nn.Module`.
89 optimizer: the optimizer associated to the network, should be regular PyTorch optimizer from `torch.optim`
90 or its subclass.
91 loss_function: the loss function associated to the optimizer, should be regular PyTorch loss,
92 which inherit from `torch.nn.modules.loss`.
93 epoch_length: number of iterations for one epoch, default to `len(train_data_loader)`.
94 non_blocking: if True and this copy is between CPU and GPU, the copy may occur asynchronously
95 with respect to the host. For other cases, this argument has no effect.
96 prepare_batch: function to parse expected data (usually `image`, `label` and other network args)
97 from `engine.state.batch` for every iteration, for more details please refer to:
98 https://pytorch.org/ignite/generated/ignite.engine.create_supervised_trainer.html.
99 iteration_update: the callable function for every iteration, expect to accept `engine`
100 and `engine.state.batch` as inputs, return data will be stored in `engine.state.output`.
101 if not provided, use `self._iteration()` instead. for more details please refer to:
102 https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html.
103 inferer: inference method that execute model forward on input data, like: SlidingWindow, etc.
104 postprocessing: execute additional transformation for the model output data.
105 Typically, several Tensor based transforms composed by `Compose`.
106 key_train_metric: compute metric when every iteration completed, and save average value to
107 engine.state.metrics when epoch completed. key_train_metric is the main metric to compare and save the
108 checkpoint into files.
109 additional_metrics: more Ignite metrics that also attach to Ignite Engine.
110 metric_cmp_fn: function to compare current key metric with previous best key metric value,
111 it must accept 2 args (current_metric, previous_best) and return a bool result: if `True`, will update
112 `best_metric` and `best_metric_epoch` with current metric and epoch, default to `greater than`.
113 train_handlers: every handler is a set of Ignite Event-Handlers, must have `attach` function, like:
114 CheckpointHandler, StatsHandler, etc.
115 amp: whether to enable auto-mixed-precision training, default is False.
116 event_names: additional custom ignite events that will register to the engine.
117 new events can be a list of str or `ignite.engine.events.EventEnum`.
118 event_to_attr: a dictionary to map an event to a state attribute, then add to `engine.state`.
119 for more details, check: https://pytorch.org/ignite/generated/ignite.engine.engine.Engine.html
120 #ignite.engine.engine.Engine.register_events.
121 decollate: whether to decollate the batch-first data to a list of data after model computation,
122 recommend `decollate=True` when `postprocessing` uses components from `monai.transforms`.
123 default to `True`.
124 optim_set_to_none: when calling `optimizer.zero_grad()`, instead of setting to zero, set the grads to None.
125 more details: https://pytorch.org/docs/stable/generated/torch.optim.Optimizer.zero_grad.html.
126 to_kwargs: dict of other args for `prepare_batch` API when converting the input data, except for
127 `device`, `non_blocking`.
128 amp_kwargs: dict of the args for `torch.autocast("cuda")` API, for more details:
129 https://pytorch.org/docs/stable/amp.html#torch.autocast.
130 compile: whether to use `torch.compile`, default is False. If True, MetaTensor inputs will be converted to
131 `torch.Tensor` before forward pass, then converted back afterward with copied meta information.
132 compile_kwargs: dict of the args for `torch.compile()` API, for more details:
133 https://pytorch.org/docs/stable/generated/torch.compile.html#torch-compile.
134 accumulation_steps: number of mini-batches over which to accumulate gradients before
135 calling ``optimizer.step()``, effectively simulating a larger batch size on
136 memory-constrained hardware. Must be a positive integer. Default: 1 (no accumulation).
137 When ``epoch_length`` is known and not divisible by ``accumulation_steps``, a flush

Callers 14

run_training_testFunction · 0.90
run_interactionMethod · 0.90
test_handlerMethod · 0.90
run_interactionMethod · 0.90
test_containerMethod · 0.90
test_plotMethod · 0.90
test_multi_epochMethod · 0.90

Calls

no outgoing calls

Tested by 14

run_training_testFunction · 0.72
run_interactionMethod · 0.72
test_handlerMethod · 0.72
run_interactionMethod · 0.72
test_containerMethod · 0.72
test_plotMethod · 0.72
test_multi_epochMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…