sd-scripts icon indicating copy to clipboard operation
sd-scripts copied to clipboard

How to use custom learning scheduler?

Open MehmetcanTozlu opened this issue 1 year ago • 5 comments

parser.add_argument( "--lr_scheduler_args", type=str, default=None, nargs="*", help='additional arguments for scheduler (like "T_max=100") / スケジューラの追加引数(例: "T_max100")', )

parser.add_argument(
    "--lr_scheduler",
    type=str,
    default="constant",
    help="scheduler to use for learning rate / 学習率のスケジューラ: linear, cosine, cosine_with_restarts, polynomial, constant (default), constant_with_warmup, adafactor",
)
parser.add_argument(
    "--lr_warmup_steps",
    type=int_or_float,
    default=0,
    help="Int number of steps for the warmup in the lr scheduler (default is 0) or float with ratio of train steps"
    " / 学習率のスケジューラをウォームアップするステップ数(デフォルト0)、または学習ステップの比率(1未満のfloat値の場合)",
)
parser.add_argument(
    "--lr_decay_steps",
    type=int_or_float,
    default=0,
    help="Int number of steps for the decay in the lr scheduler (default is 0) or float (<1) with ratio of train steps"
    " / 学習率のスケジューラを減衰させるステップ数(デフォルト0)、または学習ステップの比率(1未満のfloat値の場合)",
)
parser.add_argument(
    "--lr_scheduler_num_cycles",
    type=int,
    default=1,
    help="Number of restarts for cosine scheduler with restarts / cosine with restartsスケジューラでのリスタート回数",
)
parser.add_argument(
    "--lr_scheduler_power",
    type=float,
    default=1,
    help="Polynomial power for polynomial scheduler / polynomialスケジューラでのpolynomial power",
)
parser.add_argument(
    "--fused_backward_pass",
    action="store_true",
    help="Combines backward pass and optimizer step to reduce VRAM usage. Only available in SDXL, SD3 and FLUX"
    " / バックワードパスとオプティマイザステップを組み合わせてVRAMの使用量を削減します。SDXL、SD3、FLUXでのみ利用可能",
)
parser.add_argument(
    "--lr_scheduler_timescale",
    type=int,
    default=None,
    help="Inverse sqrt timescale for inverse sqrt scheduler,defaults to `num_warmup_steps`"
    + " / 逆平方根スケジューラのタイムスケール、デフォルトは`num_warmup_steps`",
)
parser.add_argument(
    "--lr_scheduler_min_lr_ratio",
    type=float,
    default=None,
    help="The minimum learning rate as a ratio of the initial learning rate for cosine with min lr scheduler and warmup decay scheduler"
    + " / 初期学習率の比率としての最小学習率を指定する、cosine with min lr と warmup decay スケジューラ で有効",
)

I want to assign a different learning rate to specific, predefined steps within the total number of training steps.

MehmetcanTozlu avatar Mar 27 '25 13:03 MehmetcanTozlu

lr_scheduler_type = "CosineAnnealingLR"
lr_scheduler_args = ["T_max=1024", "eta_min=1e-6"]
--lr_scheduler_type "CosineAnnealingLR"
--lr_scheduler_args "T_max=1024" "eta_min=1e-6"

and if you add your own LR scheduler module you can use the path to import it torch.optim.lr_scheduler.CosineAnnealingLR

rockerBOO avatar Mar 28 '25 03:03 rockerBOO

I have a question. I gave --learning_rate=0.0005 but writting lr=1e-7 on progress bar. Why?

steps: 64%|██████████████████████████████████████████████████████████████████████████████████▌ | 160/250 [13:48<07:46, 5.18s/it, Average key norm=0.0325, Keys Scaled=0, avr_loss=0.42, lr=1e-7]2025-04-03 15:10:13 INFO epoch is incremented. current_epoch: 40, epoch: 41 train_util.py:763 steps: 66%|███████████████████████████████████████████████████████████████████████████████████▉ | 164/250 [14:04<07:22, 5.15s/it, Average key norm=0.0325, Keys Scaled=0, avr_loss=0.429, lr=1e-7]2025-04-03 15:10:29 INFO epoch is incremented. current_epoch: 41, epoch: 42

@rockerBOO

MehmetcanTozlu avatar Apr 04 '25 07:04 MehmetcanTozlu

when you use a custom LR scheduler you need to add the LR manually I believe. may depend on the parameter variable

rockerBOO avatar Apr 04 '25 17:04 rockerBOO

The min/max learning rate etc. for the scheduler may affect it. In addition, if you use the scheduler other than constant, it will be natural that the actual learning rate is different from the given learning rate.

kohya-ss avatar Apr 05 '25 11:04 kohya-ss

Thank yours answers. I will try it.

MehmetcanTozlu avatar Apr 10 '25 12:04 MehmetcanTozlu