wandb.plot メソッドを使用すると、トレーニング中に時間の経過とともに変化するグラフを含め、wandb.Run.log() でグラフを追跡できます。カスタムチャートフレームワークの詳細については、カスタムチャートのウォークスルーを参照してください。
基本的なグラフ
これらのシンプルなグラフを使用すると、メトリクスや結果の基本的な可視化を簡単に構築できます。
Line
Scatter
Bar
Histogram
Multi-line
カスタム折れ線グラフ(任意の軸上の接続された順序付きポイントのリスト)をログに記録します。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 が必要です。アプリで見るコードを実行する カスタム散布図(任意の軸のペア x および y 上のポイント (x, y) のリスト)をログに記録します。import wandb
with wandb.init() as run:
data = [[x, y] for (x, y) in zip(class_x_scores, class_y_scores)]
table = wandb.Table(data=data, columns=["class_x", "class_y"])
run.log({"my_custom_id": wandb.plot.scatter(table, "class_x", "class_y")})
これを使用して、任意の2次元の散布図をログに記録できます。2つの値のリストを互いにプロットする場合、リスト内の値の数は正確に一致する必要があります。例えば、各ポイントには x と y が必要です。アプリで見るコードを実行する カスタム棒グラフ(ラベル付きの値のリストを棒として表示)を、わずか数行でネイティブにログ記録します。import wandb
with wandb.init() as run:
data = [[label, val] for (label, val) in zip(labels, values)]
table = wandb.Table(data=data, columns=["label", "value"])
run.log(
{
"my_bar_chart_id": wandb.plot.bar(
table, "label", "value", title="Custom Bar Chart"
)
}
)
これを使用して、任意の棒グラフをログに記録できます。リスト内のラベルと値の数は正確に一致する必要があります。各データポイントには両方が必要です。アプリで見るコードを実行する カスタムヒストグラム(値のリストを出現回数/頻度によってビンに分類)を、わずか数行でネイティブにログ記録します。例えば、予測信頼度スコア(scores)のリストがあり、その分布を可視化したいとします。import wandb
with wandb.init() as run:
data = [[s] for s in scores]
table = wandb.Table(data=data, columns=["scores"])
run.log({"my_histogram": wandb.plot.histogram(table, "scores", title="Histogram")})
これを使用して、任意のヒストグラムをログに記録できます。data はリストのリストであり、行と列の2次元配列をサポートするように設計されていることに注意してください。アプリで見るコードを実行する 共有された1組の x-y 軸上に、複数の線、または x-y 座標ペアの複数の異なるリストをプロットします。import wandb
with wandb.init() as run:
run.log(
{
"my_custom_id": wandb.plot.line_series(
xs=[0, 1, 2, 3, 4],
ys=[[10, 20, 30, 40, 50], [0.5, 11, 72, 3, 41]],
keys=["metric Y", "metric Z"],
title="Two Random Metrics",
xname="x units",
)
}
)
x と y のポイントの数は正確に一致する必要があることに注意してください。複数の y 値のリストに対して1つの x 値のリストを提供することも、y 値のリストごとに個別の x 値のリストを提供することもできます。アプリで見る
モデルの評価 チャート
これらのプリセットチャートには組み込みの 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"...])
- (オプション)プロットで可視化するラベルのサブセット(リスト形式)
アプリで見るコードを実行する 1行で ROC曲線 を作成します。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({"roc": wandb.plot.roc_curve(ground_truth, predictions)})
コードが以下にアクセスできる場合はいつでもこれをログに記録できます。
- 一連の例に対するモデルの予測スコア(
predictions)
- それらの例に対応する正解ラベル(
ground_truth)
- (オプション)ラベル/クラス名のリスト(例:ラベルインデックス0が cat、1 = dog、2 = bird などの場合は
labels=["cat", "dog", "bird"...])
- (オプション)プロットで可視化するこれらのラベルのサブセット(リスト形式)
アプリで見るコードを実行する 1行でマルチクラスの 混合行列 を作成します。import wandb
cm = wandb.plot.confusion_matrix(
y_true=ground_truth, preds=predictions, class_names=class_names
)
with wandb.init() as run:
run.log({"conf_mat": cm})
コードが以下にアクセスできる場所ならどこでもこれをログに記録できます。
- 一連の例に対するモデルの予測ラベル(
preds)または正規化された確率スコア(probs)。確率は (例の数, クラスの数) の形状である必要があります。確率または予測のいずれかを提供できますが、両方は提供できません。
- それらの例に対応する正解ラベル(
y_true)
class_names としてのラベル/クラス名の文字列の完全なリスト。例:インデックス0が cat、1が dog、2が bird の場合、class_names=["cat", "dog", "bird"]。
アプリで見るコードを実行する
インタラクティブなカスタムチャート
完全にカスタマイズするには、組み込みの 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 の代わりに、matplotlib や Plotly で生成されたチャートをログに記録することもできます。
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})