Plotting with Torch7

Plotting is a key element for scientific computations. However, Lua/Torch does not provide a unified framework for plotting figures. Instead, several Lua/Torch libraries coexist. Each of them wraps a underlying plotting libraries such as gnuplot or bokeh. Therefore, they all have their strengths and weaknesses. It is time consuming to try them all. In this post, I try to list the most interesting API  to provide some pros/cons to help you make your choice.

PS Because of some WP update, plots are not well-displayed. I am working on it.

Before looking for a plotting library, one should first answer the following questions:

  • Do you “only” need to visualize the picture?
  • Do you need to store the raw data inside the picture?
  • Do you need to interact with the plot? Zooming, retrieving values, highlight some points etc.

Beware the more advanced features you need, the dirtier your hands would be. On the other side, no-one wants to loose his time on technical details to plot a cosinus. Therefore, it is worth to know what is the final goal!

Upon time, plotting libraries has evolved and two strategies co-exist to display charts.

  • Plots are displayed inside an application/windows.
  • Plots are displayed on a web-browser by using a local server.

Those strategies have a great impact while formattingcomputing data.  For instance, an external plotting application may rely on OpenGL whereas external servers require to use HTML/Javascript features. Those may appear as technical details but they greatly impact the plotting abilities of the API. In addition, some API use low level plotting features such as NVD3 whereas iTorch/Bokeh is emphasized on a user-friendly interface.

Eventually, some libraries does not natively support Torch format, aka, torch.Tensor. To be honest, that is not a big deal!

Applicative Libraries vs Server-based Libraries

Most plotting software/APIs often rely on a desktop environment. For instance, plotting a figure in Matlab or R will open a new windows. Those API are also often specialized with a single language; it is not possible to directly plot R data with Matlab.  On the other side, this enables some optimizations for plotting workflow is fully integrated. Some command-line graphing application were created to be agnostic to a programming language. Yet, they can quickly become a bit verbose! In both cases, it can also become troublesome if you want to remotely monitor your graph (Which can be very useful with Deep Learning!)

Actually, modern browsers has now extraordinary graphical abilities. Javascript APIs and HTML5 provides a great subset of tools to draw/animate pictures and charts. Therefore, a new generation of plotting libraries has been under active development. They are based on a local/remote server. The server receives data from the program and forward it to a browser (client) that will display it (cf chart below).

server1-e1440599561909

Therefore, a plotting API “only” needs to implements the server protocol for the graphical part is handled by the browser. There is also no need to wrap every single plotting functions. This architecture is quite flexible and allows highly-personalized plots.

Actually, most of Torch plotting API are based on this server/client architecture. No worries, you do not have to be web expert to use such plotting libraries! Most of the time, you only need to execute a script to start the server and to type the URL localhost:8000 in your browser. Then, you need to use standard API that would forward your data to the server!

Let’s have a look to the available library!

3 Comments on “Plotting with Torch7

  1. 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,

Leave a Reply

Your email address will not be published.