> ## 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 と統合して、ハイパーパラメーターチューニングの試行をトラッキングし、メトリクスをログし、実験結果を比較できます。

# Ray Tune

W\&B は、2 つの軽量なインテグレーションを通じて [Ray](https://github.com/ray-project/ray) と統合できます。

* `WandbLoggerCallback` 関数は、Tune に報告されたメトリクスを自動的に Wandb API にログします。
* 関数 API と併用できる `setup_wandb()` 関数は、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 の config エントリの内容は、キーワード引数として `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 の場合、モデル チェックポイントがArtifactsとしてアップロードされます。デフォルトは 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): この例から生成されたダッシュボードを確認できます。
