メインコンテンツまでスキップ

ログ図

wandb.plotのメソッドを使って、wandb.logでチャートをトラッキングできます。トレーニング中に時間経過で変化するチャートも含まれます。カスタムチャートフレームワークについて詳しく知りたい場合は、こちらのガイドを参照してください。

基本チャート

これらのシンプルなチャートは、メトリクスや結果の基本的なデータ可視化を簡単に構築するのに便利です。

wandb.plot.line() カスタム線プロット(任意の軸で接続され順序付けされた点のリスト)をログに記録します。

data = [[x, y] for (x, y) in zip(x_values, y_values)]
table = wandb.Table(data=data, columns=["x", "y"])
wandb.log(
{"my_custom_plot_id": wandb.plot.line(table, "x", "y",
title="カスタムY対X線プロット")})

これを使用して、任意の2つの次元で曲線をログに記録できます。2つの値のリストを互いにプロットする場合、リスト内の値の数は正確に一致する必要があります(つまり、各点にxとyが必要です)。

アプリで見る →

コードを実行する →

モデル評価チャート

これらのプリセットチャートには組み込まれたwandb.plotメソッドがあり、スクリプトから直接チャートをログに記録し、UIで正確な情報を簡単に表示できます。

wandb.plot.pr_curve()

1行でPR曲線を作成します。

wandb.log({"pr": wandb.plot.pr_curve(ground_truth, predictions)})

このログは、コードが次のものにアクセスできるときに記録できます。

  • モデルの予測スコア(predictions)を一連の例に適用
  • それらの例に対応する正解ラベル(ground_truth
  • (オプション)ラベル/クラス名のリスト(labels=["cat", "dog", "bird"...] index 0がcat、1= dog、2= birdなどを意味します)
  • (オプション)プロットで表示するラベルのサブセット(リスト形式のまま)

アプリで見る →

コードを実行 →

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

完全なカスタマイズについては、カスタムチャートプリセットを微調整するか新しいプリセットを作成し、チャートを保存してください。チャートIDを使用して、スクリプトから直接カスタムプリセットにデータをログに記録します。

# プロットする列を持つ表を作成する
table = wandb.Table(data=data,
columns=["step", "height"])

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

# テーブルを使用して、新しいカスタムチャートプリセットを設定する
# 既存のチャートプリセットを使用する場合は、vega_spec_nameを変更してください
my_custom_chart = wandb.plot_table(
vega_spec_name="carey/new_chart",
data_table=table,
fields=fields)

コードを実行 →

Matplotlib と Plotly のプロット

wandb.plotでW&B カスタムチャートを使用する代わりに、matplotlibPlotly で生成されたチャートをログに記録することができます。

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel("いくつかの興味深い数値")
wandb.log({"chart": plt})

wandb.log()matplotlibのプロットや図オブジェクトを渡すだけです。デフォルトでは、プロットをPlotlyプロットに変換します。プロットを画像としてログに記録したい場合は、プロットをwandb.Imageに渡すことができます。また、直接Plotlyチャートも受け付けています。

備考

「空のプロットをログに記録しようとしました」というエラーが出ている場合は、fig = plt.figure()でプロットとは別に図を保管し、wandb.logfigをログに記録できます。

W&BのテーブルにカスタムHTMLをログする

Weights & Biasesは、PlotlyやBokehからのインタラクティブチャートをHTMLとしてログに記録し、テーブルに追加することをサポートしています。

テーブルにPlotly図をHTMLとしてログする

インタラクティブなPlotlyチャートをwandbのテーブルにログするには、それらをHTMLに変換します。

import wandb
import plotly.express as px

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

# テーブルを作成
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)

# HTMLファイルとしてPlotly図をテーブルに追加する
table.add_data(wandb.Html(path_to_plotly_html))

# テーブルをログに記録する
run.log({"test_table": table})
wandb.finish()

Bokeh図をHTMLとしてテーブルにログする

インタラクティブなBokehグラフをwandbテーブルにログすることができます。HTMLに変換してからログしてください。

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)

run = wandb.init(project='audio_test')

my_table = wandb.Table(columns=['audio_with_plot'], data=[[wandb_html], [wandb_html]])

run.log({"audio_table": my_table})

run.finish()


Was this page helpful?👍👎