メインコンテンツへスキップ
wandb.plot メソッドを使用すると、トレーニング中に時間の経過とともに変化するグラフを含め、wandb.Run.log() でグラフを追跡できます。カスタムチャートフレームワークの詳細については、カスタムチャートのウォークスルーを参照してください。

基本的なグラフ

これらのシンプルなグラフを使用すると、メトリクスや結果の基本的な可視化を簡単に構築できます。
カスタム折れ線グラフ(任意の軸上の接続された順序付きポイントのリスト)をログに記録します。
import wandb

with wandb.init() as run:
    data = [[x, y] for (x, y) in zip(x_values, y_values)]
    table = wandb.Table(data=data, columns=["x", "y"])
    run.log(
        {
            "my_custom_plot_id": wandb.plot.line(
                table, "x", "y", title="Custom Y vs X Line Plot"
            )
        }
    )
これを使用して、任意の2次元の曲線をログに記録できます。2つの値のリストを互いにプロットする場合、リスト内の値の数は正確に一致する必要があります。例えば、各ポイントには x と y が必要です。
Custom line plot
アプリで見るコードを実行する

モデルの評価 チャート

これらのプリセットチャートには組み込みの wandb.plot() メソッドがあり、スクリプトから直接チャートを迅速かつ簡単にログに記録し、UIで探している正確な情報を確認できます。
1行で PR曲線 を作成します。
import wandb
with wandb.init() as run:
    # ground_truth は正解ラベルのリスト、predictions は予測スコアのリストです
    # 例: ground_truth = [0, 1, 1, 0], predictions = [0.1, 0.4, 0.35, 0.8]
    ground_truth = [0, 1, 1, 0]
    predictions = [0.1, 0.4, 0.35, 0.8]
    run.log({"pr": wandb.plot.pr_curve(ground_truth, predictions)})
コードが以下にアクセスできる場合はいつでもこれをログに記録できます。
  • 一連の例に対するモデルの予測スコア(predictions
  • それらの例に対応する正解ラベル(ground_truth
  • (オプション)ラベル/クラス名のリスト(例:ラベルインデックス0が cat、1 = dog、2 = bird などの場合は labels=["cat", "dog", "bird"...]
  • (オプション)プロットで可視化するラベルのサブセット(リスト形式)
Precision-recall curve
アプリで見るコードを実行する

インタラクティブなカスタムチャート

完全にカスタマイズするには、組み込みの Custom Chart プリセット を微調整するか、新しいプリセットを作成してチャートを保存します。チャート ID を使用して、スクリプトからそのカスタムプリセットに直接データをログ記録します。
import wandb
# プロットする列を含むテーブルを作成
table = wandb.Table(data=data, columns=["step", "height"])

# テーブルの列からチャートのフィールドへのマッピング
fields = {"x": "step", "value": "height"}

# テーブルを使用して新しいカスタムチャートプリセットにデータを入力
# 独自に保存したチャートプリセットを使用するには、vega_spec_name を変更します
# タイトルを編集するには、string_fields を変更します
my_custom_chart = wandb.plot_table(
    vega_spec_name="carey/new_chart",
    data_table=table,
    fields=fields,
    string_fields={"title": "Height Histogram"},
)

with wandb.init() as run:
    # カスタムチャートをログに記録
    run.log({"my_custom_chart": my_custom_chart})
コードを実行する

Matplotlib および Plotly のプロット

wandb.plot() を使用した W&B Custom Charts の代わりに、matplotlibPlotly で生成されたチャートをログに記録することもできます。
import wandb
import matplotlib.pyplot as plt

with wandb.init() as run:
    # シンプルな matplotlib プロットを作成
    plt.figure()
    plt.plot([1, 2, 3, 4])
    plt.ylabel("some interesting numbers")
    
    # プロットを W&B にログ記録
    run.log({"chart": plt})
matplotlib のプロットまたはフィギュアオブジェクトを wandb.Run.log() に渡すだけです。デフォルトでは、プロットは Plotly プロットに変換されます。プロットを画像としてログに記録したい場合は、プロットを wandb.Image に渡すことができます。また、Plotly チャートを直接受け入れることもできます。
「空のプロットをログに記録しようとしました(You attempted to log an empty plot)」というエラーが表示される場合は、fig = plt.figure() を使用してプロットとは別にフィギュアを保存し、wandb.Run.log() の呼び出しで fig をログに記録してください。

W&B Tables へのカスタム HTML のログ記録

W&B は、Plotly および Bokeh からのインタラクティブなチャートを HTML としてログに記録し、Tables に追加することをサポートしています。

Plotly フィギュアを HTML として Tables にログ記録する

Plotly チャートを HTML に変換することで、インタラクティブな Plotly チャートを wandb Tables にログ記録できます。
import wandb
import plotly.express as px

# 新しい run を初期化
with wandb.init(project="log-plotly-fig-tables", name="plotly_html") as run:

    # テーブルを作成
    table = wandb.Table(columns=["plotly_figure"])

    # Plotly フィギュアのパスを作成
    path_to_plotly_html = "./plotly_figure.html"

    # Plotly フィギュアの例
    fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])

    # Plotly フィギュアを HTML に書き出す
    # auto_play を False に設定すると、アニメーション化された Plotly チャートが
    # テーブル内で自動的に再生されるのを防ぎます
    fig.write_html(path_to_plotly_html, auto_play=False)

    # Plotly フィギュアを HTML ファイルとして Table に追加
    table.add_data(wandb.Html(path_to_plotly_html))

    # Table をログに記録
    run.log({"test_table": table})

Bokeh フィギュアを HTML として Tables にログ記録する

Bokeh チャートを HTML に変換することで、インタラクティブな Bokeh チャートを wandb Tables にログ記録できます。
from scipy.signal import spectrogram
import holoviews as hv
import panel as pn
from scipy.io import wavfile
import numpy as np
from bokeh.resources import INLINE

hv.extension("bokeh", logo=False)
import wandb


def save_audio_with_bokeh_plot_to_html(audio_path, html_file_name):
    sr, wav_data = wavfile.read(audio_path)
    duration = len(wav_data) / sr
    f, t, sxx = spectrogram(wav_data, sr)
    spec_gram = hv.Image((t, f, np.log10(sxx)), ["Time (s)", "Frequency (hz)"]).opts(
        width=500, height=150, labelled=[]
    )
    audio = pn.pane.Audio(wav_data, sample_rate=sr, name="Audio", throttle=500)
    slider = pn.widgets.FloatSlider(end=duration, visible=False)
    line = hv.VLine(0).opts(color="white")
    slider.jslink(audio, value="time", bidirectional=True)
    slider.jslink(line, value="glyph.location")
    combined = pn.Row(audio, spec_gram * line, slider).save(html_file_name)


html_file_name = "audio_with_plot.html"
audio_path = "hello.wav"
save_audio_with_bokeh_plot_to_html(audio_path, html_file_name)

wandb_html = wandb.Html(html_file_name)

with wandb.init(project="audio_test") as run:
    my_table = wandb.Table(columns=["audio_with_plot"], data=[[wandb_html]])
    run.log({"audio_table": my_table})