メインコンテンツへスキップ
W&B Models では、wandb.plot の method を使用して、wandb.Run.log() でチャートをトラッキングできます。これには、トレーニング中に時間の経過とともに変化するチャートも含まれます。custom charting framework の詳細については、custom charts walkthrough を参照してください。

基本チャート

W&B のチャートを作成するには、次の手順に従います。
  1. wandb.Table オブジェクトを作成し、可視化したいデータを追加します。
  2. W&B に組み込まれている ヘルパー関数 のいずれかを使用して、プロットを生成します。
  3. 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="カスタム Y 対 X 折れ線グラフ"
            )
        }
    )
これを使用すると、任意の2次元に対する曲線をログできます。2つの値のリストを互いにプロットする場合は、それぞれのリストの値の数が完全に一致している必要があります。たとえば、各点には x と y の両方が必要です。
カスタム折れ線グラフ
詳細は、W&B を使用したカスタム折れ線グラフの作成レポートを参照してください。コードを実行する

モデル評価用チャート

これらのプリセットチャートには組み込みの wandb.plot() method が用意されており、スクリプトから直接すばやく簡単にチャートをログして、UI で必要な情報を正確に確認できます。
1行でPrecision-Recall curveを作成できます:
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"]
  • (任意) プロットに表示するラベルのサブセット (リスト形式のまま) 。
適合率-再現率曲線
詳細は、W&B で適合率-再現率曲線をプロットするレポートを参照してください。コードを実行する

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

より細かくカスタマイズするには、組み込みのカスタムチャート プリセットを調整するか、新しいプリセットを作成してから、チャートを保存します。チャート 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 のカスタムチャートを使う代わりに、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 の chart も直接受け付けています。
「You attempted to log an empty plot」のようなエラーが表示される場合は、fig = plt.figure() を使って図をプロットとは別に保持し、その後 wandb.Run.log() の call で fig をログしてください。

カスタムHTMLをW&B Tablesにログする

W&Bでは、Plotly と Bokeh のインタラクティブなチャートを HTML としてログし、Tables に追加できます。

Plotly の図を HTML として Tables にログする

インタラクティブな Plotly チャートは、HTML に変換することで W&B 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ファイルとしてTablesに追加する。
    table.add_data(wandb.Html(path_to_plotly_html))

    # 表をログする。
    run.log({"test_table": table})

Bokeh の図を HTML として Tables にログする

インタラクティブな Bokeh チャートは、HTML に変換することで W&B 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})