import torch


def get_optimizer(model, learning_rate, optimizer_state_dict=None, verbose=False):
    """
    Returns an ADAM optimizer attached to the given model
    (and stored on the same device).
    If optimizer_state_dict is not None, it will fill in the optimizer state.
    However, learning_rate will override and saved optimizer state (to allow
    you to adjust during training.)
    
    """
    if verbose: print("Building optimizer... ", end='')
    optimizer = torch.optim.Adam(model.parameters(), learning_rate)
    if optimizer_state_dict:
        optimizer.load_state_dict(optimizer_state_dict)
        # allow for change in learning rate:
        for param_group in optimizer.param_groups:
            param_group['lr'] = learning_rate
    if verbose: print("Done.")
    return optimizer