MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / prepare

Method prepare

python/paddle/hapi/model.py:2161–2245  ·  view source on GitHub ↗

Configures the model before running. Args: optimizer (Optimizer|None, optional): Optimizer must be set in training and should be a Optimizer instance. It can be None in eval and test mode. Default: None. loss (Loss|Callable|N

(
        self,
        optimizer: paddle.optimizer.Optimizer | None = None,
        loss: (
            paddle.nn.Layer | Callable[[Tensor, Tensor], Tensor] | None
        ) = None,
        metrics: Metric | list[Metric] | None = None,
        amp_configs: str | dict[str, Any] | None = None,
    )

Source from the content-addressed store, hash-verified

2159 self._adapter._amp_configs[key] = amp_configs[key]
2160
2161 def prepare(
2162 self,
2163 optimizer: paddle.optimizer.Optimizer | None = None,
2164 loss: (
2165 paddle.nn.Layer | Callable[[Tensor, Tensor], Tensor] | None
2166 ) = None,
2167 metrics: Metric | list[Metric] | None = None,
2168 amp_configs: str | dict[str, Any] | None = None,
2169 ) -> None:
2170 """
2171
2172 Configures the model before running.
2173
2174 Args:
2175 optimizer (Optimizer|None, optional): Optimizer must be set in training
2176 and should be a Optimizer instance. It can be None in eval
2177 and test mode. Default: None.
2178 loss (Loss|Callable|None, optional): Loss function can
2179 be a `paddle.nn.Layer` instance or any callable function
2180 taken the predicted values and ground truth values as input.
2181 It can be None when there is no loss. Default: None.
2182 metrics (Metric|list[Metric]|None, optional): If metrics is set, all
2183 metrics will be calculated and output in train/eval mode. Default: None.
2184 amp_configs (str|dict|None, optional): AMP configurations. If AMP or pure
2185 float16 training is used, the key 'level' of 'amp_configs'
2186 should be set to 'O1' or 'O2' respectively. Otherwise, the
2187 value of 'level' defaults to 'O0', which means float32
2188 training. In addition to 'level', parameters consistent with
2189 mixed precision API could also be passed in. The supported
2190 keys are: 'init_loss_scaling', 'incr_ratio', 'decr_ratio',
2191 'incr_every_n_steps', 'decr_every_n_nan_or_inf',
2192 'use_dynamic_loss_scaling', 'custom_white_list',
2193 'custom_black_list', and 'custom_black_varnames'or
2194 'use_fp16_guard' is only supported in static graph mode. Mixed
2195 precision API documentations :ref:`api_paddle_amp_auto_cast`
2196 and :ref:`api_paddle_amp_GradScaler` could be referenced
2197 for details. For convenience, 'amp_configs' could be set to
2198 'O1' or 'O2' if no more parameters are needed. 'amp_configs'
2199 could be None in float32 training. Default: None.
2200
2201 Returns:
2202 None
2203
2204 """
2205 self._place = _get_device()
2206 if isinstance(self._place, base.CUDAPlace):
2207 global _parallel_context_initialized
2208 if (
2209 paddle.distributed.ParallelEnv().nranks > 1
2210 and not _parallel_context_initialized
2211 ):
2212 if in_dynamic_mode():
2213 main_prog_seed = base.default_main_program().random_seed
2214 startup_prog_seed = (
2215 base.default_startup_program().random_seed
2216 )
2217 base.disable_dygraph()
2218 paddle.disable_static(self._place)

Callers 15

mainFunction · 0.95
test_wandb_callbackMethod · 0.95
inferMethod · 0.95
get_modelMethod · 0.95
models_inferMethod · 0.95
test_lenetMethod · 0.95
fitMethod · 0.95
fit_with_tuple_inputMethod · 0.95
evaluateMethod · 0.95

Calls 4

_prepare_ampMethod · 0.95
TypeErrorClass · 0.85
to_listFunction · 0.70

Tested by 15

test_wandb_callbackMethod · 0.76
inferMethod · 0.76
get_modelMethod · 0.76
models_inferMethod · 0.76
test_lenetMethod · 0.76
fitMethod · 0.76
fit_with_tuple_inputMethod · 0.76
evaluateMethod · 0.76
predictMethod · 0.76