delu.evaluation¶
- class delu.evaluation(*modules)[source]¶
Context-manager & decorator for models evaluation.
This code…
with evaluation(model): # or: evaluation(model_0, model_1, ...) ... @evaluation(model) # or: @evaluation(model_0, model_1, ...) def f(): ...
…is equivalent to the following:
context = getattr(torch, 'inference_mode', torch.no_grad) with context(): model.eval() ... @context() def f(): model.eval() ...
- Parameters
modules (torch.nn.modules.module.Module) –
- Return type
Note
The training status of modules is undefined once a context is finished or a decorated function returns.
Warning
The function must be used in the same way as
torch.no_grad
andtorch.inference_mode
, i.e. only as a context manager or a decorator as shown below in the examples. Otherwise, the behaviour is undefined.Warning
Contrary to
torch.no_grad
andtorch.inference_mode
, the function cannot be used to decorate generators. So, in the case of generators, you have to manually create a context:def my_generator(): with evaluation(...): for a in b: yield c
Examples
a = torch.nn.Linear(1, 1) b = torch.nn.Linear(2, 2) with evaluation(a): ... with evaluation(a, b): ... @evaluation(a) def f(): ... @evaluation(a, b) def f(): ...