def get_pytorch_optimizers() -> Dict[str, Type[Optimizer]]: optimizers = inheritors(Optimizer) optimizers_dict = { opt.__name__: opt for opt in optimizers if _is_pytorch_optimizer(opt) } return optimizers_dict
def get_pytorch_losses() -> Dict[str, Type[_Loss]]: losses = inheritors(_Loss) losses_dict = { loss.__name__: loss for loss in losses if _is_pytorch_loss(loss) } return losses_dict
def get_pytorch_losses(): losses = inheritors(_Loss) losses_dict = { l.__name__: l for l in losses if not l.__name__.startswith('_') } # filter _WeightedLoss return losses_dict
def test_inheritors(): class ParentClass: pass class ChildClass1(ParentClass): pass classes = inheritors(ParentClass) assert len(classes) == 1 assert list(classes)[0] is ChildClass1 class ChildClass2(ParentClass): pass class ChildClass3(ParentClass): pass classes = inheritors(ParentClass) assert len(classes) == 3 for cls in [ChildClass1, ChildClass2, ChildClass3]: assert cls in classes
def attach(self, engine, handler_kwargs_dict=None): if handler_kwargs_dict is None: handler_kwargs_dict = dict() for event_enum in inheritors(EventEnum): for key, event in event_enum.__members__.items(): if hasattr(self, event.value): handler = getattr(self, event.value) if isinstance(handler, Callable): handler_kwargs = handler_kwargs_dict.get(event, dict()) engine.add_event_handler(event, handler, **handler_kwargs) else: raise TypeError( f"Attribute {event.value} is not callable.")
def attach(self, engine: Engine): """Attach callback to the :class:`argus.engine.Engine`. Args: engine (Engine): The engine to which the callback will be attached. """ for event_enum in inheritors(EventEnum): for key, event in event_enum.__members__.items(): if hasattr(self, event.value): handler = getattr(self, event.value) if isinstance(handler, (FunctionType, MethodType)): engine.add_event_handler(event, handler) else: raise TypeError( f"Attribute {event.value} is not callable.")
def get_pytorch_optimizers(): optimizers = inheritors(Optimizer) optimizers_dict = {opt.__name__: opt for opt in optimizers} return optimizers_dict