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["optimizer"], Adam):
sample = np.random.uniform(low=adam_range[0], high=adam_range[1], size=1)
elif isinstance(cfg["optimizer"], AdamW):
sample = np.random.uniform(low=adamw_range[0], high=adamw_range[1], size=1)
elif isinstance(cfg["optimizer"], SGD):
sample = np.random.uniform(low=sgd_range[0], high=sgd_range[1], size=1)
where configs is a list of dicts and cfg["optimizer"] 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["optimizer"].__name__? The latter seems inelegant.
Thank you.