Plotting with Torch7
Dygraph / Dislay
Dygraph is a Javascript library specialized on line-plotting chart. It provides really useful interactive features while dealing with complex time series. The API interface is quite easy to use once you have understand how it works. Unfortunately, the Lua documentation is a bit poor.
The display API encode the data from Lua and send it to the server through POST requests. This reduce the data workflow and it makes the API much easier to maintain for fewer information are hard-coded.
Display has really strict limitations. Its main drawback is that pictures cannot be saved! Furthermore, the server need to be launched to display the pictures and no cache is available. Dygraph is also limited to plot time-series figures.
- Lua API : https://github.com/szym/display
- Underlying API : http://dygraphs.com/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
disp = require("display") --The labels are a bit counter-intuitive. The first element must labeled be the X-axis, next elements are the plots'label local labels = {"time (ms)", 'Sine Wave', 'Cosine Wave'} local data = {} local x = torch.linspace(-2*math.pi,2*math.pi) for i = 1, x:size(1) do table.insert(data, { x[i], -- x-axis torch.sin(x[i]), -- y-axis for label1 0.5*torch.cos(x[i]) -- y-axis for label2 }) end local config = { title = "Dygraph example - 1", labels = labels, ylabel = "", } disp.plot(data, config) |
Once the dygraph format is well-understood, it is really easy to make the plots highly interactive! In the following example, plots are highlighted, it is possible to smooth/unsmooth the chart by using the small number. One can zoom/unzoom. It is far more difficult to reach this level of interactivity with NVD3.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
disp = require("display") local labels = {"time (ms)", 'Sine Wave', 'Cosine Wave'} local data = {} local x = torch.linspace(-2*math.pi,2*math.pi) for i = 1, x:size(1) do table.insert(data, { x[i], { torch.sin(x[i] + torch.uniform(0,1) ), math.random() }, -- we add some random noise { 0.5*torch.cos(x[i] + torch.uniform(0,0.5)), 0.5*math.random() } -- Beware of the table to display the std }) end local config = { title = "Dygraph example - 2", labels = labels, ylabel = "", errorBars = true, highlightSeriesOpts = { strokeWidth = 3, strokeBorderWidth = 1, highlightCircleSize = 5 }, rollPeriod = 14, showRoller = true, } disp.plot(data, config) |
Some tricks to know:
- The wonderful API can be found here: http://dygraphs.com/options.html
- Dygraph is really sensible to are ill-formatted data. In such case, it is worth to use the Javascript debugger in your browser (F12). The most common mistakes are the following:
- The number of column in data mismatch the number of labels. Example, no X-label is defined
- ErrorBars need an additional array
- Dygraph is well suited to plot data during the training.
- If you restart your server while plotting, its configuration is lost
Great,
I would like to ask if there is a way how to compute Recall and precision from a confusion matrix and draw them if it is possible. I am facing this problem for more than a month but I could not find an answer. So, please help.
Sure!
First, you can compute your confusion matrix my using the optim package: https://github.com/torch/optim/blob/master/ConfusionMatrix.lua
Then, if you want to plot accuracy/recall upon time, Dygraph/Display is a good library
For every steps:
– Compute the Precision/Recall from your confusion matrix
\text{Precision}=\frac{tp}{tp+fp} \,
\text{Recall}=\frac{tp}{tp+fn} \,
– Plot it !
Lua code:
local labels = {"epoch", 'accuracy', 'recall'}
local data = {}
local config =
{
title = "Global accuracy/recall upon time",
labels = labels,
ylabel = "ratio",
}
for t = 1, noEpoch do
--computation
local accuracy, recall = someFct()
--storage
table.insert(data, {t, accuracy, recall })
-- display
config.win = disp.plot(data, config)
end
Hi
Many thanks for replying but I try it many times but did not work with. Could you please give me more intuition about the code.
Regards,