How to check if an object is an LR scheduler?

Checking if something is a PyTorch optimizer is pretty straightforward:

isinstance(something, torch.optim.Optimizer)

I was wondering what is the best / preferred way to check if something is a LR scheduler?

There is a (protected) class _LR_scheduler in the, but first of all not every type of LR scheduler inherits from it (e.g., ReduceLROnPlateau), and secondly it is kind of against Python conventions to type-check against a protected member of a module…

Anybody got some advice what is the best practice here? :slight_smile:

Edit: I gave it some more thought, and the best I came up with is to check if:

something.__module__ == 'torch.optim.lr_scheduler'

I guess this works, but it still feels a bit hacky, so if anyone’s got a better idea let me know!


from torch.optim.lr_scheduler import _LRScheduler, ReduceLROnPlateau
# _LRScheduler can be imported, just ignore the error hint of your IDE
# The only exception is that ReduceLROnPlateau not inherits from _LRScheduler

isinstance(something, (_LRScheduler, ReduceLROnPlateau))