Function for picking an optimizer

I am trying to write a function which returns an optimizer from a dictionary, but I get the following error message:
ValueError: optimizer got an empty parameter list

The Function is as follows:

def pickOptimizer(opt, *args, **kwargs):
    optimizers = {
        'sgd': optim.SGD(*args, **kwargs),
        'asgd': optim.ASGD(*args, **kwargs),
        'adam': optim.Adam(*args, **kwargs),
        'adamw': optim.AdamW(*args, **kwargs),
        'adamax': optim.Adamax(*args, **kwargs),
        'rms': optim.RMSprop(*args, **kwargs),
        'rprop': optim.Rprop(*args, **kwargs),
        'adah': AdaHessian(*args, **kwargs)
    return optimizers[opt]

If I write params=net.parameters() explicitly into the function it works as I expect otherwise I get an error. For this to work I need to put this function into my main document.
Thank you for any help.

you’re exhausting the iterator, make a list of parameters or don’t construct all the optimizers

I see, but is there a way to make the choice of optimizer easier than just re-writing the code? Need to be able to run is with the help of bash scripts and I want to adjust the network this way.

I use this

def _get_core_optimizer(name):
	import torch.optim as optim
	return getattr(optim, name)

Thank you very much, now it works as I wanted.