-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "checkpoint_mode" kwarg to plotting (#99)
* Add checkpoint_mode kwarg to plotting * Remove dummy key of token_category * Revert "Remove dummy key of token_category" This reverts commit 2edb2c5. * Update vis notebook * Remove kwargs and have fixed args in place --------- Co-authored-by: Siwei Li <[email protected]>
- Loading branch information
Showing
2 changed files
with
143 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,99 @@ | ||
from typing import Union | ||
|
||
import ipywidgets | ||
import numpy as np | ||
import plotly.graph_objects as go | ||
|
||
|
||
def visualize_per_token_category( | ||
input: dict[str, dict[str, tuple]], log_scale=False, **kwargs: str | ||
) -> ipywidgets.VBox: | ||
model_names = list(input.keys()) | ||
categories = list(input[model_names[0]].keys()) | ||
input: dict[Union[str, int], dict[str, tuple]], | ||
log_scale=False, | ||
line_metric="Means", | ||
checkpoint_mode=True, | ||
shade_color="rgba(68, 68, 68, 0.3)", | ||
line_color="rgb(31, 119, 180)", | ||
bar_color="purple", | ||
marker_color="SkyBlue", | ||
background_color="AliceBlue", | ||
) -> go.FigureWidget: | ||
input_x = list(input.keys()) | ||
categories = list(input[input_x[0]].keys()) | ||
category = categories[0] | ||
|
||
def get_hovertexts(mid: np.ndarray, lo: np.ndarray, hi: np.ndarray) -> list[str]: | ||
return [f"Loss: {m:.3f} ({l:.3f}, {h:.3f})" for m, l, h in zip(mid, lo, hi)] | ||
|
||
def get_plot_values(category: str) -> tuple[np.ndarray, np.ndarray, np.ndarray]: | ||
x = np.array([input[name][category] for name in model_names]).T | ||
x = np.array([input[x][category] for x in input_x]).T | ||
means, err_lo, err_hi = x[0], x[1], x[2] | ||
return means, err_lo, err_hi | ||
|
||
means, err_low, err_hi = get_plot_values(category) | ||
g = go.FigureWidget( | ||
data=go.Scatter( | ||
x=model_names, | ||
means, err_lo, err_hi = get_plot_values(category) | ||
|
||
if checkpoint_mode: | ||
scatter_plot = go.Figure( | ||
[ | ||
go.Scatter( | ||
name="Upper Bound", | ||
x=input_x, | ||
y=means + err_hi, | ||
mode="lines", | ||
marker=dict(color=shade_color), | ||
line=dict(width=0), | ||
showlegend=False, | ||
), | ||
go.Scatter( | ||
name="Lower Bound", | ||
x=input_x, | ||
y=means - err_lo, | ||
marker=dict(color=shade_color), | ||
line=dict(width=0), | ||
mode="lines", | ||
fillcolor=shade_color, | ||
fill="tonexty", | ||
showlegend=False, | ||
), | ||
go.Scatter( | ||
name=line_metric, | ||
x=input_x, | ||
y=means, | ||
mode="lines", | ||
marker=dict( | ||
color=line_color, | ||
size=0, | ||
line=dict(color=line_color, width=1), | ||
), | ||
), | ||
] | ||
) | ||
else: | ||
scatter_plot = go.Scatter( | ||
x=input_x, | ||
y=means, | ||
error_y=dict( | ||
type="data", | ||
symmetric=False, | ||
array=err_hi, | ||
arrayminus=err_low, | ||
color=kwargs.get("bar_color", "purple"), | ||
arrayminus=err_lo, | ||
color=bar_color, | ||
), | ||
marker=dict( | ||
color=kwargs.get("marker_color", "SkyBlue"), | ||
color=marker_color, | ||
size=15, | ||
line=dict(color=kwargs.get("line_color", "MediumPurple"), width=2), | ||
line=dict(color=line_color, width=2), | ||
), | ||
hovertext=get_hovertexts(means, err_low, err_hi), | ||
hovertext=get_hovertexts(means, err_lo, err_hi), | ||
hoverinfo="text+x", | ||
), | ||
) | ||
g = go.FigureWidget( | ||
data=scatter_plot, | ||
layout=go.Layout( | ||
yaxis=dict( | ||
title="Loss", | ||
type="log" if log_scale else "linear", | ||
), | ||
plot_bgcolor=kwargs.get("bg_color", "AliceBlue"), | ||
plot_bgcolor=background_color, | ||
), | ||
) | ||
|
||
selected_category = ipywidgets.Dropdown( | ||
options=categories, | ||
placeholder="", | ||
description="Token Category:", | ||
disabled=False, | ||
) | ||
|
||
def response(change): | ||
means, err_lo, err_hi = get_plot_values(selected_category.value) | ||
with g.batch_update(): | ||
g.data[0].y = means | ||
g.data[0].error_y["array"] = err_hi | ||
g.data[0].error_y["arrayminus"] = err_lo | ||
g.data[0].hovertext = get_hovertexts(means, err_lo, err_hi) | ||
|
||
selected_category.observe(response, names="value") | ||
|
||
return ipywidgets.VBox([selected_category, g]) | ||
return g |