File size: 5,861 Bytes
17c6d62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!--Copyright 2020 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

-->

# 콜백 [[callbacks]]

μ½œλ°±μ€ PyTorch [`Trainer`]의 반볡 ν•™μŠ΅ λ™μž‘μ„ μ‚¬μš©μž μ •μ˜ν•  수 μžˆλŠ” κ°μ²΄μž…λ‹ˆλ‹€
(이 κΈ°λŠ₯은 TensorFlowμ—μ„œλŠ” 아직 κ΅¬ν˜„λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€). μ½œλ°±μ€ 반볡 ν•™μŠ΅μ˜ μƒνƒœλ₯Ό
κ²€μ‚¬ν•˜μ—¬ (μ§„ν–‰ 상황 보고, TensorBoard λ˜λŠ” 기타 λ¨Έμ‹  λŸ¬λ‹ ν”Œλž«νΌμ— 둜그 남기기 λ“±) 
κ²°μ •(예: μ‘°κΈ° μ’…λ£Œ)을 내릴 수 μžˆμŠ΅λ‹ˆλ‹€.

μ½œλ°±μ€ [`TrainerControl`] 객체λ₯Ό λ°˜ν™˜ν•˜λŠ” 것 μ™Έμ—λŠ” 반볡 ν•™μŠ΅μ—μ„œ μ–΄λ–€ 것도 λ³€κ²½ν•  수 μ—†λŠ”
"읽기 μ „μš©" μ½”λ“œ μ‘°κ°μž…λ‹ˆλ‹€. 반볡 ν•™μŠ΅μ— 변경이 ν•„μš”ν•œ μ‚¬μš©μž μ •μ˜ μž‘μ—…μ΄ ν•„μš”ν•œ 경우, 
[`Trainer`]λ₯Ό μ„œλΈŒν΄λž˜μŠ€λ‘œ λ§Œλ“€μ–΄ ν•„μš”ν•œ λ©”μ†Œλ“œλ“€μ„ μ˜€λ²„λΌμ΄λ“œν•΄μ•Ό ν•©λ‹ˆλ‹€ (μ˜ˆμ‹œλŠ” [trainer](trainer)λ₯Ό μ°Έμ‘°ν•˜μ„Έμš”).

기본적으둜 `TrainingArguments.report_to`λŠ” `"all"`둜 μ„€μ •λ˜μ–΄ μžˆμœΌλ―€λ‘œ, [`Trainer`]λŠ” λ‹€μŒ μ½œλ°±μ„ μ‚¬μš©ν•©λ‹ˆλ‹€.

- [`DefaultFlowCallback`]λŠ” 둜그, μ €μž₯, 평가에 λŒ€ν•œ κΈ°λ³Έ λ™μž‘μ„ μ²˜λ¦¬ν•©λ‹ˆλ‹€.
- [`PrinterCallback`] λ˜λŠ” [`ProgressCallback`]λŠ” μ§„ν–‰ 상황을 ν‘œμ‹œν•˜κ³  둜그λ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€ 
  ([`TrainingArguments`]λ₯Ό 톡해 tqdm을 λΉ„ν™œμ„±ν™”ν•˜λ©΄ 첫 번째 콜백이 μ‚¬μš©λ˜κ³ , κ·Έλ ‡μ§€ μ•ŠμœΌλ©΄ 두 λ²ˆμ§Έκ°€ μ‚¬μš©λ©λ‹ˆλ‹€).
- [`~integrations.TensorBoardCallback`]λŠ” TensorBoardκ°€ (PyTorch >= 1.4
 λ˜λŠ” tensorboardXλ₯Ό 톡해) μ ‘κ·Ό κ°€λŠ₯ν•˜λ©΄ μ‚¬μš©λ©λ‹ˆλ‹€.
- [`~integrations.WandbCallback`]λŠ” [wandb](https://www.wandb.com/)κ°€ μ„€μΉ˜λ˜μ–΄ 있으면
 μ‚¬μš©λ©λ‹ˆλ‹€.
- [`~integrations.CometCallback`]λŠ” [comet_ml](https://www.comet.com/site/)이 μ„€μΉ˜λ˜μ–΄ 있으면 μ‚¬μš©λ©λ‹ˆλ‹€.
- [`~integrations.MLflowCallback`]λŠ” [mlflow](https://www.mlflow.org/)κ°€ μ„€μΉ˜λ˜μ–΄ 있으면 μ‚¬μš©λ©λ‹ˆλ‹€.
- [`~integrations.NeptuneCallback`]λŠ” [neptune](https://neptune.ai/)이 μ„€μΉ˜λ˜μ–΄ 있으면 μ‚¬μš©λ©λ‹ˆλ‹€.
- [`~integrations.AzureMLCallback`]λŠ” [azureml-sdk](https://pypi.org/project/azureml-sdk/)κ°€ μ„€μΉ˜λ˜μ–΄
 있으면 μ‚¬μš©λ©λ‹ˆλ‹€.
- [`~integrations.CodeCarbonCallback`]λŠ” [codecarbon](https://pypi.org/project/codecarbon/)이 μ„€μΉ˜λ˜μ–΄
 있으면 μ‚¬μš©λ©λ‹ˆλ‹€.
- [`~integrations.ClearMLCallback`]λŠ” [clearml](https://github.com/allegroai/clearml)이 μ„€μΉ˜λ˜μ–΄ 있으면 μ‚¬μš©λ©λ‹ˆλ‹€.
- [`~integrations.DagsHubCallback`]λŠ” [dagshub](https://dagshub.com/)이 μ„€μΉ˜λ˜μ–΄ 있으면 μ‚¬μš©λ©λ‹ˆλ‹€.
- [`~integrations.FlyteCallback`]λŠ” [flyte](https://flyte.org/)κ°€ μ„€μΉ˜λ˜μ–΄ 있으면 μ‚¬μš©λ©λ‹ˆλ‹€.
- [`~integrations.DVCLiveCallback`]λŠ” [dvclive](https://dvc.org/doc/dvclive)κ°€ μ„€μΉ˜λ˜μ–΄ 있으면 μ‚¬μš©λ©λ‹ˆλ‹€.

νŒ¨ν‚€μ§€κ°€ μ„€μΉ˜λ˜μ–΄ μžˆμ§€λ§Œ ν•΄λ‹Ή 톡합 κΈ°λŠ₯을 μ‚¬μš©ν•˜κ³  μ‹Άμ§€ μ•Šλ‹€λ©΄, `TrainingArguments.report_to`λ₯Ό μ‚¬μš©ν•˜κ³ μž ν•˜λŠ” 톡합 κΈ°λŠ₯ λͺ©λ‘μœΌλ‘œ λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€ (예: `["azure_ml", "wandb"]`).

μ½œλ°±μ„ κ΅¬ν˜„ν•˜λŠ” μ£Όμš” ν΄λž˜μŠ€λŠ” [`TrainerCallback`]μž…λ‹ˆλ‹€. 이 ν΄λž˜μŠ€λŠ” [`Trainer`]λ₯Ό 
μΈμŠ€ν„΄μŠ€ν™”ν•˜λŠ” 데 μ‚¬μš©λœ [`TrainingArguments`]λ₯Ό κ°€μ Έμ˜€κ³ , ν•΄λ‹Ή Trainer의 λ‚΄λΆ€ μƒνƒœλ₯Ό 
[`TrainerState`]λ₯Ό 톡해 μ ‘κ·Όν•  수 있으며, [`TrainerControl`]을 톡해 반볡 ν•™μŠ΅μ—μ„œ 일뢀 
μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.


## μ‚¬μš© κ°€λŠ₯ν•œ 콜백 [[available-callbacks]]

λΌμ΄λΈŒλŸ¬λ¦¬μ—μ„œ μ‚¬μš© κ°€λŠ₯ν•œ [`TrainerCallback`] λͺ©λ‘μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€:

[[autodoc]] integrations.CometCallback
    - setup

[[autodoc]] DefaultFlowCallback

[[autodoc]] PrinterCallback

[[autodoc]] ProgressCallback

[[autodoc]] EarlyStoppingCallback

[[autodoc]] integrations.TensorBoardCallback

[[autodoc]] integrations.WandbCallback
    - setup

[[autodoc]] integrations.MLflowCallback
    - setup

[[autodoc]] integrations.AzureMLCallback

[[autodoc]] integrations.CodeCarbonCallback

[[autodoc]] integrations.NeptuneCallback

[[autodoc]] integrations.ClearMLCallback

[[autodoc]] integrations.DagsHubCallback

[[autodoc]] integrations.FlyteCallback

[[autodoc]] integrations.DVCLiveCallback
    - setup

## TrainerCallback [[trainercallback]]

[[autodoc]] TrainerCallback

μ—¬κΈ° PyTorch [`Trainer`]와 ν•¨κ»˜ μ‚¬μš©μž μ •μ˜ μ½œλ°±μ„ λ“±λ‘ν•˜λŠ” μ˜ˆμ‹œκ°€ μžˆμŠ΅λ‹ˆλ‹€:

```python
class MyCallback(TrainerCallback):
    "A callback that prints a message at the beginning of training"

    def on_train_begin(self, args, state, control, **kwargs):
        print("Starting training")


trainer = Trainer(
    model,
    args,
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    callbacks=[MyCallback],  # μš°λ¦¬λŠ” 콜백 클래슀λ₯Ό 이 λ°©μ‹μœΌλ‘œ μ „λ‹¬ν•˜κ±°λ‚˜ κ·Έκ²ƒμ˜ μΈμŠ€ν„΄μŠ€(MyCallback())λ₯Ό 전달할 수 μžˆμŠ΅λ‹ˆλ‹€
)
```

또 λ‹€λ₯Έ μ½œλ°±μ„ λ“±λ‘ν•˜λŠ” 방법은 `trainer.add_callback()`을 ν˜ΈμΆœν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€:

```python
trainer = Trainer(...)
trainer.add_callback(MyCallback)
# λ‹€λ₯Έ λ°©λ²•μœΌλ‘œλŠ” 콜백 클래슀의 μΈμŠ€ν„΄μŠ€λ₯Ό 전달할 수 μžˆμŠ΅λ‹ˆλ‹€
trainer.add_callback(MyCallback())
```

## TrainerState [[trainerstate]]

[[autodoc]] TrainerState

## TrainerControl [[trainercontrol]]

[[autodoc]] TrainerControl