import math


def _generate_cosine_lr(lr_init, lr_max, total_steps, warmup_steps):
    """
    Create an array of learning rates conforming to the cosine decay mode.

    Args:
       lr_init (float): Starting learning rate.
       lr_max (float): Maximum learning rate.
       total_steps (int): Total number of train steps.
       warmup_steps (int): Number of warmup steps.

    Returns:
       np.array, a learning rate array.
    """
    decay_steps = total_steps - warmup_steps
    lr_each_step = []
    lr_inc = (float(lr_max) - float(lr_init)) / float(warmup_steps)
    for i in range(total_steps):
        if i < warmup_steps:
            lr = float(lr_init) + lr_inc * (i + 1)
        else:
            linear_decay = (total_steps - i) / decay_steps
            cosine_decay = 0.5 * \
                (1 + math.cos(math.pi * 2 * 0.47 * i / decay_steps))
            decayed = linear_decay * cosine_decay + 0.00001
            lr = lr_max * decayed
        lr_each_step.append(lr)
    return lr_each_step