ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๊ณ  ์ €์žฅ ์‹œ์— ๊ณ ๋ฏผ์ด ์ƒ๊ฒผ๋‹ค. ์–ด๋–ค ๊ฒƒ์„ ๊ธฐ์ค€์œผ๋กœ ์ข‹์€ ๋ชจ๋ธ์ด๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์„๊นŒ? ๊ธฐ์กด์—” loss๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ์ƒ๊ฐํ–ˆ์ง€๋งŒ ๋‚ด๊ฐ€ ๋ณด๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋ถˆ๊ท ํ˜•์ด ์‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœํžˆ loss๊ฐ€ ๊ธฐ์ค€์ด ๋˜๋Š” ๊ฒŒ ์•„๋‹๊ฑฐ๋ผ๋Š” ์ƒ๊ฐ์„ ํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”์ง€ ์ฐพ์•„๋ดค๋‹ค. ์ œ์ผ ์ข‹์€ ๋ ˆํผ๋Ÿฐ์Šค๋Š” ์บ๊ธ€์ด๋‹ค. ์ฝ”๋“œ๋„ ๋งŽ๊ณ  ์„ฑ๋Šฅ ์ค‘์‹ฌ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ ์ค‘์— ๋ดค๋˜ ์ฝ”๋“œ๋Š” Pytorch multi labels by cumulated level ๐Ÿ”— ์˜€๋‹ค. ์—ฌ๊ธฐ์„œ best model์„ ์ €์žฅํ•˜๋Š” ์ฝ”๋“œ๋งŒ ๊ฐ€์ ธ์™”๋‹ค.

if auroc > best_metric:
    best_metric = auroc
    torch.save(model.state_dict(), f'dict_model_{j}_fold_{fold}_ckpt_pytorch')
else :
    early_stoping += 1
    if early_stoping > EARLY_STOPPING :
        print(f'{Fore.RED}{Style.BRIGHT}====> early stopping{Style.RESET_ALL}\\n')
        break
if epoch+1 < 10 :
    a =' '
else :
    a =''
print(f'Epoch: {epoch+1}{a}/{EPOCHS} | Train Loss: {train_loss:.6f} | Val loss: {val_loss:.6f} | Val auc {auroc:.6f} | Best auc {best_metric:.6f}  | lr: {lr} ')
auroc = metric.reset()

์—ฌ๊ธฐ์„  auprc๋ฅผ ๊ธฐ์ค€์œผ๋กœ best model์„ ์ €์žฅํ•˜๊ณ  ์žˆ์—ˆ๊ณ  early stopping์ด ์ง€์ •ํ•œ ๊ฐ’๋ณด๋‹ค ํฌ๋‹ค๋ฉด ํ•™์Šต์„ ๋ฉˆ์ถ”๋Š” ๋กœ์ง์ด๋‹ค. ๋ฐ์ดํ„ฐ์˜ ํŠน์„ฑ์— ๋งž์ถฐ์„œ best model metric ๊ธฐ์ค€์„ ์žก์•„์•ผ ๋˜๋‚˜๋ณด๋‹ค. ์ด๋Œ€๋กœ๋Š” ์ฐ์ฐํ•˜๋‹ˆ ์ข€ ๋” ๊ฒ€์ฆ๋œ ์ฝ”๋“œ๋ฅผ ์ฐพ์•„๋ดค๋‹ค. ์ž˜ ๊ตฌํ˜„๋˜์–ด์žˆ๋Š” ํ—ˆ๊น…ํŽ˜์ด์Šค์˜ transformer ์ฝ”๋“œ๋ฅผ ๋œฏ์–ด๋ดค๋‹ค. ํ—ˆ๊น…ํŽ˜์ด์Šค์—๋Š” trainer์—์„œ early stopping ๋ฐฉ์‹์œผ๋กœ ํ•™์Šต์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ๋จผ์ € trainer callbacks์—์„œ EarlyStoppingCallback ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

trainer = Trainer(
            model,
            args,
            train_dataset=train_dataset,
            eval_dataset=val_dataset,
            callbacks = [EarlyStoppingCallback(early_stopping_patience=2)]
        )

metric_for_best_model์—์„œ ์›ํ•˜๋Š” metric์„ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋˜๊ณ  ๊ธฐ๋ณธ์€ loss๋กœ ๋˜์–ด์žˆ๋‹ค. ์ด ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๊ฒฝ์šฐ์—” greater_is_better๋„ ํ•จ๊ป˜ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค. auc์ฒ˜๋Ÿผ ๋†’์€ ๊ฐ’์ด ์ข‹๋‹ค๋ฉด True, loss์ฒ˜๋Ÿผ ๋‚ฎ์€ ๊ฐ’์ด ์ข‹๋‹ค๋ฉด False๋ฅผ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.

metric_for_best_model (str, optional) — Use in conjunction with load_best_model_at_end to specify the metric to use to compare two different models. Must be the name of a metric returned by the evaluation with or without the prefix "eval_". Will default to "loss" if unspecified and load_best_model_at_end=True (to use the evaluation loss).

If you set this value, greater_is_better will default to True. Don’t forget to set it to False if your metric is better when lower.

์ด์™ธ์—๋„ ์ง€์ •ํ•  ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • load_best_model_at_end = True (EarlyStoppingCallback() requires this to be True).
  • evaluation_strategy=’steps’ # or epoch
  • eval_steps = 50 (evaluate the metrics after N steps).

๊ฒฐ๊ตญ, loss ์™ธ์—๋„ f1์ด๋‚˜ auc ๋“ฑ ๋ฐ์ดํ„ฐ ํŠน์„ฑ์— ๋งž์ถฐ early stopping์„ ํ•˜๋Š” ๊ฒƒ์ด ๋งž๋‹ค. ๋‹จ, early stopping์˜ ํ›ˆ๋ จ ๋‹จ์œ„๊ฐ€ step์ผ ๊ฒฝ์šฐ test ์„ฑ๋Šฅ์ด ๋” ์•ˆ ์ข‹์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ฃผ์˜ํ•˜์ž.

์ฐธ๊ณ ํ•œ ์ž๋ฃŒ

์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
ยซ   2025/06   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Total
Today
Yesterday