How to check if an object is a certain PyTorch optimizer?
I want to check the class of objects like this:
from torch.optim import Adam, SGD, AdamW

adam_range = (0.8, 1.0)
adamw_range = (0.6, 0.7)
sgd_range = (0.0, 0.5)

targets = []
for cfg in configs:
    if isinstance(cfg[&quot;optimizer&quot;], Adam):
        sample = np.random.uniform(low=adam_range[0], high=adam_range[1], size=1)
    elif isinstance(cfg[&quot;optimizer&quot;], AdamW):
        sample = np.random.uniform(low=adamw_range[0], high=adamw_range[1], size=1)
    elif isinstance(cfg[&quot;optimizer&quot;], SGD):
        sample = np.random.uniform(low=sgd_range[0], high=sgd_range[1], size=1)

where configs is a list of dicts and cfg[&quot;optimizer&quot;] are uninitialized references of from torch.optim import Adam, SGD, AdamW. However, none of the if/elif statements evaluate to True.
How do I properly check the type of these references? Is there a better way than checking cfg[&quot;optimizer&quot;].__name__? The latter seems inelegant.
Thank you.

Your cfg[&quot;optimizer&quot;] is not an instance of any optimizer, but the type itself.
therefore, you should test it like this:
for cfg in configs:
    if cfg[&quot;optimizer&quot;] is Adam:
        sample = np.random.uniform(low=adam_range[0], high=adam_range[1], size=1)
    elif cfg[&quot;optimizer&quot;] is AdamW:
        sample = np.random.uniform(low=adamw_range[0], high=adamw_range[1], size=1)
    elif cfg[&quot;optimizer&quot;] is SGD:
        sample = np.random.uniform(low=sgd_range[0], high=sgd_range[1], size=1)

To emphasize the difference between an instance of Adam and type Adam, try the following:
opt = Adam(model.parameters(), lr=0.1)  # make an _instance_ of Adam

isinstance(opt, Adam)  # True  - opt is an instance of Adam optimizer

isinstance(Adam, Adam) # False - Adam is a type, not an instance of Adam

isinstnace(Adam, type) # True  - &quot;Adam&quot; is a type not an instance of Adam

type(opt) is Adam      # True  - the type of opt is Adam

type(opt) == Adam      # True

I find this page to summarize this difference quite well.
