delu.evaluation#

class delu.evaluation(*modules)[source]#

[DEPRECATED | Instead, use model.eval() + torch.no_inference/no_grad] Context-manager & decorator for models evaluation.

This code…

with delu.evaluation(model):  # or: evaluation(model_0, model_1, ...)
    ...
@delu.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 (Module) –

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 and torch.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 and torch.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 delu.evaluation(...):
        for a in b:
            yield c

Examples

a = torch.nn.Linear(1, 1)
b = torch.nn.Linear(2, 2)
with delu.evaluation(a):
    ...
with delu.evaluation(a, b):
    ...
@delu.evaluation(a)
def f():
    ...
@delu.evaluation(a, b)
def f():
    ...