Creating Custom Line Plots With Weights & Biases
This article provides usage and examples for wandb.plot.line(), explaining how to log a custom line plot natively in just a few lines.
Created on October 8|Last edited on November 16
Comment
Method: wandb.plot.line()
Log a custom line plot—a list of connected/ordered points (x, y) on a pair of arbitrary axes x and y—natively in a few lines:
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="Custom Y vs X Line Plot")})
You can use this to log curves on any two dimensions. Note that if you're plotting two lists of values against each other, the number of values in the lists must match exactly (i.e. each point must have an x and a y).
Toy CNN variants
6
Basic Usage Example
I finetune a CNN to predict 10 classes of living things: plants, birds, insects, etc. I want to plot the binarized average precision curve (simplifying to a binary correct/incorrect label across all 10 classes).
In my validation step, I compute the micro-averaged precision and recall using sklearn and following their example for multi-label settings to yield two arrays of the same length: precision_micro and recall_micro (see the end of this report for the full details). The line plot I want to create will show precision_micro values on the y-axis versus the matching recall_micro values on the x-axis.
I can now call:
data = [[x, y] for (x, y) in zip(recall_micro, precision_micro)]table = wandb.Table(data=data, columns = ["recall_micro", "precision_micro"])wandb.log({"my_lineplot_id" : wandb.plot.line(table, "recall_micro","precision_micro", stroke=None, title="Average Precision")})
Steps to follow:
- Create a data object: collect your points as a 2D list/array, where each row is a point and each column is a dimension. This line plot assumes two dimensions / two columns, but you could pass in more data and customize the plot further if you wish. You could also use the optional stroke= argument to wandb.plot.line() to pass in a third field corresponding to the line stroke type (solid, dash, dot...) or color.
- Pass data to a wandb.Table() object in which you name the columns in order so you can refer to them later
- Pass the table object and the same x and y column names in order to wandb.plot.line() with an optional title, which will create your custom plot under the key my_lineplot_id. To visualize multiple runs on the same plot, keep this plot key constant. Note that the table itself will also be logged in the "Media" section of your workspace, under my_lineplot_id_table.
Customized Usage
Here are some simple ones:
- rename the axis titles for clarity: add "title" : "Your Title" to the x and y fields under encoding
- change the color spectrum to reflect the increase in training data/epochs as the curves go from purple to yellow:
"color": {"type": "nominal","field" : "name","scale" : {"scheme" : "plasma"}}
On any chart, you can pan around and zoom in to see more detail, as well as see more information about a point on hover (and modify this display information as well!). You can also hover over the top right corner and click on the "eye" icon to see the full Vega spec which defines the chart. The wandb.plot.line API is defined here.
Toy CNN variants
6
P.S. Computing Average Precision for Multi-Class Models
You can compute this whenever your code has access to:
- a model's predicted scores (val_predictions) on a set of examples
- the corresponding ground truth labels (ground_truth) for those examples
from sklearn.metrics import precision_recall_curvefrom sklearn.preprocessing import label_binarize# generate binary correctness labels across classesbinary_ground_truth = label_binarize(ground_truth,classes=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])# compute a PR curve with sklearn like you normally wouldprecision_micro, recall_micro, _ = precision_recall_curve(binary_ground_truth.ravel(),val_predictions.ravel())# now you can log these values to a custom chart!
Add a comment
You can use this to log curves on any two dimensions. Note that if you're plotting two lists of values against each other, the number of values in the lists must match exactly (i.e. each point must have an x and a y).
Is there is a way to log different curve for each time step
Reply
Vega2
1 reply
To visualize multiple runs on the same plot, keep this plot key constant. It overwrites the plot and how to add tags like AP NT 100, E1 etc.
Reply
How can I create an arbitrary line plot that updates in live time, without all the data initially available (in the way you do with default metrics).
Reply
Iterate on AI agents and models faster. Try Weights & Biases today.