import torch


def create_learning_rate_scheduler(scheduler_name, optimizer, **kwargs):
    """
    Create a torch learning rate scheduler of the specified type for the given optimizer.

    Args:
        scheduler_name: The type of learning rate scheduler to create (as string), must be a valid scheduler name
        from torch.optim.lr_scheduler, e.g. 'ExponentialLR'.
        optimizer: The optimizer to use this scheduler with.
        **kwargs: Additional named parameters to the schedulers init function.

    Returns:
        The learning rate scheduler.

    Raises:
        ValueError: on an invalid scheduler name (i.e. no class in torch.optim.lr_scheduler).
    """
    try:
        scheduler_class = getattr(torch.optim.lr_scheduler, scheduler_name)
    except AttributeError:
        raise ValueError(f'Invalid learning rate scheduler {scheduler_name}. Please pass a valid scheduler name from '
                         'torch.optim.lr_scheduler')
    return scheduler_class(optimizer, **kwargs)