text: from torch.optim.lr_scheduler import LambdaLR
  import math
  
  
  def get_cosine_schedule_with_warmup(optimizer, num_warmup_steps, num_training_steps, num_cycles=0.5, last_epoch=-1, min_lr_ratio=None):
      """ Create a schedule with a learning rate that decreases following the
      values of the cosine function between 0 and `pi * cycles` after a warmup
      period during which it increases linearly between 0 and 1.
      Adapted from Hugginface, modified by adding a minimal learning rate.
      """
  
      def lr_lambda(current_step):
          if current_step < num_warmup_steps:
              return float(current_step) / float(max(1, num_warmup_steps))
          progress = float(current_step - num_warmup_steps) / float(max(1, num_training_steps - num_warmup_steps))
          if min_lr_ratio is not None:
              if current_step < num_training_steps:
                  return max(min_lr_ratio, max(0.0, 0.5 * (1.0 + math.cos(math.pi * float(num_cycles) * 2.0 * progress))))
              else:
                  return min_lr_ratio
          else:
              return max(0.0, 0.5 * (1.0 + math.cos(math.pi * float(num_cycles) * 2.0 * progress)))
  
      return LambdaLR(optimizer, lr_lambda, last_epoch)