import math


def adjust_lr(iter, iter_per_epoch, config, fix_after_epoch=8, min_lr=0.0002):
    """ Exponentially decaying learning rate
    Args:
        iter: current iteration
        iter_per_epoch: iterations per epoch
        config: configuation file
    Returns:
        decay_lr: decayed learning rate
    """
    if config["decay_every_epoch"] == -1:
        decay_lr = config["init_lr"]
    else:
        iter = min(iter, iter_per_epoch * fix_after_epoch)
        decay_lr = max(min_lr, config["init_lr"] * math.exp(math.log(
            config["decay_factor"]) / iter_per_epoch \
            / config["decay_every_epoch"])**iter)
    return decay_lr