import torch


def get_optimizer(opt_config, model):
  """Creates an optimizer based on the opt_config and model parameters.
  Args:
    - opt_config: (dict) optimizer config. It should have the following fields:
      * name: (str) name of the optimizer. It should be one of SGD, Adam, or LBFGS
      * params: (dict) optimizer specific parameters
  Returns:
    - optimizer object
  """
  opt_name = opt_config.name
  if opt_name == 'SGD':
    optimizer = torch.optim.SGD(model.parameters(), **opt_config.params)
  elif opt_name == 'Adam':
    optimizer = torch.optim.Adam(model.parameters(), **opt_config.params)
  elif opt_name == 'LBFGS':
    optimizer = torch.optim.LBFGS(model.parameters(), **opt_config.params)
  else:
    raise ValueError("Optimizer {} is not defined".format(opt_name))
  return optimizer