> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wandb.ai/llms.txt
> Use this file to discover all available pages before exploring further.

> W&B를 Ray Tune과 통합해 하이퍼파라미터 튜닝 trial을 추적하고, 메트릭을 기록하며, 실험 결과를 비교합니다.

# Ray Tune

W\&B는 두 가지 경량 인테그레이션을 제공해 [Ray](https://github.com/ray-project/ray)와 통합됩니다.

* `WandbLoggerCallback` 함수는 Tune에 보고된 메트릭을 Wandb API에 자동으로 기록합니다.
* `setup_wandb()` 함수는 함수 API와 함께 사용할 수 있으며, Tune의 트레이닝 정보를 사용해 Wandb API를 자동으로 초기화합니다. 이후에는 평소처럼 Wandb API를 사용할 수 있습니다. 예를 들어 `run.log()`를 사용해 트레이닝 과정을 기록할 수 있습니다.

<div id="configure-the-integration">
  ## 인테그레이션 설정
</div>

```python theme={null}
from ray.air.integrations.wandb import WandbLoggerCallback
```

wandb 설정은 `tune.run()`의 `config` 매개변수에 wandb 키를 전달해 수행합니다(아래 예시 참조).

wandb 설정 항목의 내용은 키워드 인수로 `wandb.init()`에 전달됩니다. 다만 다음 설정은 예외이며, `WandbLoggerCallback` 자체를 설정하는 데 사용됩니다:

<div id="parameters">
  ### 매개변수
</div>

`project (str)`: Wandb 프로젝트의 이름입니다. 필수 항목입니다.

`api_key_file (str)`: Wandb API 키가 들어 있는 파일의 경로입니다.

`api_key (str)`: Wandb API 키입니다. `api_key_file` 대신 설정할 수 있습니다.

`excludes (list)`: 로그에서 제외할 메트릭 목록입니다.

`log_config (bool)`: 결과 딕셔너리의 config 매개변수를 로그에 기록할지 여부입니다. 기본값은 False입니다.

`upload_checkpoints (bool)`:  True이면 모델 체크포인트가 아티팩트로 업로드됩니다. 기본값은 False입니다.

<div id="example">
  ### 예시
</div>

```python theme={null}
from ray import tune, train
from ray.air.integrations.wandb import WandbLoggerCallback


def train_fc(config):
    for i in range(10):
        train.report({"mean_accuracy": (i + config["alpha"]) / 10})


tuner = tune.Tuner(
    train_fc,
    param_space={
        "alpha": tune.grid_search([0.1, 0.2, 0.3]),
        "beta": tune.uniform(0.5, 1.0),
    },
    run_config=train.RunConfig(
        callbacks=[
            WandbLoggerCallback(
                project="<your-project>", api_key="<your-api-key>", log_config=True
            )
        ]
    ),
)

results = tuner.fit()
```

<div id="setup_wandb">
  ## setup\_wandb
</div>

```python theme={null}
from ray.air.integrations.wandb import setup_wandb
```

이 유틸리티 함수는 Ray Tune과 함께 사용할 수 있도록 Wandb를 초기화하는 데 도움이 됩니다. 기본적인 사용 방법은 트레이닝 함수에서 `setup_wandb()`를 호출하는 것입니다:

```python theme={null}
from ray.air.integrations.wandb import setup_wandb


def train_fn(config):
    # wandb 초기화
    wandb = setup_wandb(config)
    run = wandb.init(
        project=config["wandb"]["project"],
        api_key_file=config["wandb"]["api_key_file"],
    )

    for i in range(10):
        loss = config["a"] + config["b"]
        run.log({"loss": loss})
        tune.report(loss=loss)
    run.finish()


tuner = tune.Tuner(
    train_fn,
    param_space={
        # 검색 공간 정의
        "a": tune.choice([1, 2, 3]),
        "b": tune.choice([4, 5, 6]),
        # wandb 설정
        "wandb": {"project": "Optimization_Project", "api_key_file": "/path/to/file"},
    },
)
results = tuner.fit()
```

<div id="example-code">
  ## 예시 코드
</div>

인테그레이션이 어떻게 작동하는지 확인할 수 있도록 몇 가지 예시를 준비했습니다:

* [Colab](https://wandb.me/raytune-colab): 인테그레이션을 직접 사용해 볼 수 있는 간단한 데모입니다.
* [대시보드](https://wandb.ai/anmolmann/ray_tune): 예시에서 생성된 대시보드를 확인할 수 있습니다.
