TylerYep / torchinfo

View model summaries in PyTorch!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

print model summary to latex

yangtzech opened this issue · comments

Is your feature request related to a problem? Please describe.
Every time I want to put the specific structure of the model I use into a thesis, I will use torchinfo to print it, and make a latex table manually. Is it possible to add a to_latex() function to automatically deal with this situation?

Could you give an example of what input model and what the output in latex formatting looks like (with a screenshot too?).

PRs for this are welcome!

First off @yangtzech, I would like to say that for visualization, torchview is excellent (it's based on / inspired by torchinfo, though it gives an image instead of a table).

As for the question at hand, it might be possible / sensible to encode the data contained in the output string produced by torchinfo.summary into a dict (or OrderedDict, or whatever makes sense). This would be saved as an attribute of ModelStatistics (the return type of torchinfo.summary). Then, the data could easily be used by other libraries. For example, the user could feed that dict into a pandas.DataFrame and call pandas.DataFrame.to_latex to get LaTeX.

Doing it this way would, I believe, solve more problems than just the one in this PR, because you could easily use that information in many different ways — yes, to print to LaTeX, but also to many other formats (csv, html, etc.). It would also make the information captured by torchinfo.summary as part of a larger program, without explicitely printing it to a human-readable format.

I think that such a dict / whatever might also make the __repr__-method more readable, or at least it has the potential to.

Thoughts @TylerYep?

Could you give an example of what input model and what the output in latex formatting looks like (with a screenshot too?).

PRs for this are welcome!

For example, the model summary is as follows:

===========================================================================
Layer (type (var_name):depth-idx)             Input Shape     Output Shape
===========================================================================
LSTMPlus (LSTMPlus)                           [2048, 10, 2]   [2048, 1]
+ _RNN_Backbone (backbone): 1-1               [2048, 10, 2]   [2048, 100, 2]
|    + Identity (to_cat_embed): 2-1           [2048, 10, 2]   [2048, 10, 2]
|    + Identity (feature_extractor): 2-2      [2048, 10, 2]   [2048, 10, 2]
|    + Transpose (transpose): 2-3             [2048, 10, 2]   [2048, 2, 10]
|    + Sequential (rnn): 2-4                  [2048, 2, 10]   [2048, 2, 100]
|    |    + LSTM (0): 3-1                     [2048, 2, 10]   [2048, 2, 100]
|    |    + LSTMOutput (1): 3-2               [2048, 2, 100]  [2048, 2, 100]
|    + Transpose (transpose): 2-5             [2048, 2, 100]  [2048, 100, 2]
+ Sequential (head): 1-2                      [2048, 100, 2]  [2048, 1]
|    + LastStep (0): 2-6                      [2048, 100, 2]  [2048, 100]
|    + Dropout (1): 2-7                       [2048, 100]     [2048, 100]
|    + Linear (2): 2-8                        [2048, 100]     [2048, 1]
===========================================================================

The latex table might be:

\begin{table}[tbp]
    \small
    \caption{Structure}
    {
        \begin{tabular*}{\columnwidth}{l@{\extracolsep{\fill}}cc}
            \toprule
            {Layers} & {Input} & {Output} \\
            \midrule
            Identity & [2048, 10, 2] & [2048, 10, 2] \\
            Identity & [2048, 10, 2] & [2048, 10, 2] \\
            Transpose & [2048, 10, 2] & [2048, 2, 10] \\
            Backbone & [2048, 2, 10] & [2048, 2, 100] \\
            RNN output & [2048, 2, 100] & [2048, 2, 100] \\
            Transpose & [2048, 2, 100] & [2048, 100, 2] \\
            LastStep & [2048, 100, 2] & [2048, 100] \\
            Dropout & [2048, 100] & [2048, 100] \\
            Linear & [2048, 100] & [2048, 1] \\
            \bottomrule
        \end{tabular*}
    }
    \label{tab.Structure}
\end{table}

image

@snimu Thanks for your suggestion on torchview. I just tried it, and it is excellent. But I need to customize the figure whici it returns to conform to my thesis layout, such as language (To Chinese), fig size, or font.

As for the idea to make full use of pandas.DataFrame.to_latex, I think it's great, as we can use pandas.DataFrame.to_latex or pandas.io.formats.style.Styler.to_latex to deal with output format more flexibly.