()
Defers backpropagation until the end of scope.

    Accumulates all gradients passed to `dni.backward` inside the scope and
    backpropagates them all in a single `torch.autograd.backward` call.

    Use it and `dni.backward` whenever you want to backpropagate multiple times
    through the same nodes in the computation graph, for example when mixing
    real and synthetic gradients. Otherwise, PyTorch will complain about
    backpropagating more than once through the same graph.

    Scopes of this context manager cannot be nested.
Defers backpropagation until the end of scope.
Defers backpropagation until the end of scope .
def defer_backward():
    """Defers backpropagation until the end of scope.

    Accumulates all gradients passed to `dni.backward` inside the scope and
    backpropagates them all in a single `torch.autograd.backward` call.

    Use it and `dni.backward` whenever you want to backpropagate multiple times
    through the same nodes in the computation graph, for example when mixing
    real and synthetic gradients. Otherwise, PyTorch will complain about
    backpropagating more than once through the same graph.

    Scopes of this context manager cannot be nested.
    """
    if _Manager.defer_backward:
        raise RuntimeError('cannot nest defer_backward')
    _Manager.defer_backward = True

    try:
        yield

        if _Manager.deferred_gradients:
            (variables, gradients) = zip(*_Manager.deferred_gradients)
            torch.autograd.backward(variables, gradients)
    finally:
        _Manager.reset_defer_backward()
def defer_backward ( ) : if _Manager . defer_backward : raise RuntimeError ( 'cannot nest defer_backward' ) _Manager . defer_backward = True try : yield if _Manager . deferred_gradients : ( variables , gradients ) = zip ( * _Manager . deferred_gradients ) torch . autograd . backward ( variables , gradients ) finally : _Manager . reset_defer_backward ( )