nnrl/optim/utils.py
def wrap_optim_cls(cls: Type[Optimizer]) -> Type[Optimizer]:
    """Return PyTorch optimizer with additional context manager."""

    class ContextManagerOptim(cls):  # type:ignore
        # pylint:disable=missing-class-docstring,too-few-public-methods
        @contextlib.contextmanager
        def optimize(self):
            """Zero grads before yielding and step the optimizer upon exit."""
            self.zero_grad()
            yield
            self.step()

    return ContextManagerOptim
Return PyTorch optimizer with additional context manager.
Return PyTorch optimizer with additional context manager.
Return PyTorch optimizer with additional context manager .
def wrap_optim_cls(cls: Type[Optimizer]) -> Type[Optimizer]:
    class ContextManagerOptim(cls):  
        @contextlib.contextmanager
        def optimize(self):
            self.zero_grad()
            yield
            self.step()
    return ContextManagerOptim
def wrap_optim_cls ( cls : Type [ Optimizer ] ) -> Type [ Optimizer ] : class ContextManagerOptim ( cls ) : @ contextlib . contextmanager def optimize ( self ) : """Zero grads before yielding and step the optimizer upon exit.""" self . zero_grad ( ) yield self . step ( ) return ContextManagerOptim
Return PyTorch optimizer with additional context manager.
Return PyTorch optimizer with additional context manager .
"""Return PyTorch optimizer with additional context manager.""" # type:ignore # pylint:disable=missing-class-docstring,too-few-public-methods """Zero grads before yielding and step the optimizer upon exit."""
cls
Type[Optimizer]