Debian Codegraph @

texmf-dist/tex/latex/nndraw/nndraw.sty

raw
1 %%
2 %% This is file `nndraw.sty',
3 %% generated with the docstrip utility.
4 %%
5 %% The original source files were:
6 %%
7 %% nndraw.dtx (with options: `package')
8 %%
9 %% This is a generated file.
10 %%
11 %% Copyright (C) 2021 by Carlos Cardoso Dias <[email protected]>
12 %% --------------------------------------------------------------------------
13 %% This work may be distributed and/or modified under the
14 %% conditions of the LaTeX Project Public License, either version 1.3
15 %% of this license or (at your option) any later version.
16 %% The latest version of this license is in
17 %% http://www.latex-project.org/lppl.txt
18 %% and version 1.3 or later is part of all distributions of LaTeX
19 %% version 2005/12/01 or later.
20 %%
21 \NeedsTeXFormat{LaTeX2e}[1999/12/01]
22 \ProvidesPackage{nndraw}
23 [2021/06/22 1.0 nndraw is a LaTeX package which provides utilities to draw neural networks.]
24 \textbf{nndraw} is a \LaTeX package which provides utilities to draw neural networks in an efficient way.
25 Currently this package provides utilities to draw fully connected feedforward neural networks with an arbitrary
26 number of layers described inside the `fullyconnectednn` environment using the command `nnlayer`.
27 An example of usage is shown below, in this example, a fully connected feedforward
28 deep neural network is provided with two inputs in the first layer, one hidden layer
29 with four neurons and one output layer with one output. This example shows how easy it is
30 to customize the presence/absence of biases in any layer as well as its position.
31
32 \begin{verbatim}
33 \begin{fullyconnectednn}[biasy = -4.5,
34 titley = 1,
35 inout = false]
36 \nnlayer[title = Input layer]{2}
37 \nnlayer[title = Hidden layer]{4}
38 \nnlayer[title = Ouput layer, hasbias = false]{1}
39 \end{fullyconnectednn}
40 \end{verbatim}
41
42 \noindent Which is drawn as
43
44 \begin{fullyconnectednn}[biasy = -4.5,
45 titley = 1,
46 inout = false]
47 \nnlayer[title = Input layer]{2}
48 \nnlayer[title = Hidden layer]{4}
49 \nnlayer[title = Ouput layer, hasbias = false]{1}
50 \end{fullyconnectednn}
51 \NeedsTeXFormat{LaTeX2e}
52 \ProvidesPackage{nndraw}[2021/06/20 Neural Networks Draw LaTeX package]
53 \RequirePackage{tikz}
54 \pgfdeclarelayer{back}
55 \pgfsetlayers{back,main}
56 \newcommand\nnlayerNoText[1]{}
57 \newif\ifnnlayerHasBias
58 \pgfkeys{
59 /nnlayer/.is family, /nnlayer,
60 default/.style = {title = , text = \nnlayerNoText, hasbias = true, bias = , color = , biascolor = },
61 title/.estore in = \nnlayerTitle,
62 text/.store in = \nnlayerText,
63 hasbias/.is if = nnlayerHasBias,
64 bias/.estore in = \nnlayerBias,
65 color/.estore in = \nnlayerColor,
66 biascolor/.estore in = \nnlayerBiasColor,
67 }
68 \newcounter{numlayers}
69 \newcounter{nninputlayer}
70 \newcounter{lastnnsize}
71 \tikzstyle{neuron}=[circle, minimum size=6mm, fill=gray!70]
72 \tikzstyle{neuron_connection}=[->, shorten >=1pt, gray!70]
73 \newcommand\nnlayer[2][]{
74 \pgfkeys{/nnlayer, default, #1}
75 \node[text width = \fullyconnectednnTextWidth, align = center] at (\fullyconnectednnLayersep *
76 \thenumlayers,\fullyconnectednnTitleY) {\nnlayerTitle};
77 \if\thenninputlayer0
78 \setcounter{nninputlayer}{#2}
79 \fi
80 \foreach \i in {1,...,#2}
81 {
82 \node[neuron, yshift=(#2 - \thenninputlayer)*5 mm, fill=\nnlayerColor] (\thenumlayers-\i) at (\thenumlayers * \fullyconnectednnLayersep,-\i) {\nnlayerText{\i}};
83 }
84 \if\thelastnnsize0
85 \else
86 \ifnnlayerHasBias
87 \node[neuron, yshift=5mm, fill=\nnlayerBiasColor] (bias-\thenumlayers) at (\thenumlayers * \fullyconnectednnLayersep -\fullyconnectednnLayersep + \fullyconnectednnBiasX, \fullyconnectednnBiasY) {\nnlayerBias};
88 \fi
89 \begin{pgfonlayer}{back}
90 \foreach \i in {1,...,\thelastnnsize}
91 {
92 \foreach \j in {1,...,#2}
93 {
94 \draw[neuron_connection] (\the\numexpr\thenumlayers-1\relax-\i) -- (\thenumlayers-\j);
95 }
96 }
97 \ifnnlayerHasBias
98 \foreach \j in {1,...,#2}
99 {
100 \draw[neuron_connection] (bias-\thenumlayers) -- (\thenumlayers-\j);
101 }
102 \fi
103 \end{pgfonlayer}
104 \fi
105 \setcounter{lastnnsize}{#2}
106 \stepcounter{numlayers}
107 }
108 \newif\iffullyconnectednnInout
109 \pgfkeys{
110 /fullyconnectednn/.is family, /fullyconnectednn,
111 default/.style = {input = \nnlayerNoText, output = \nnlayerNoText, layersep = 3, biasx = 0.75, biasy = -8.75, titley = 0.75, inout = true, text width = 2cm},
112 input/.store in = \fullyconnectednnInput,
113 output/.store in = \fullyconnectednnOutput,
114 layersep/.estore in = \fullyconnectednnLayersep,
115 biasx/.estore in = \fullyconnectednnBiasX,
116 biasy/.estore in = \fullyconnectednnBiasY,
117 titley/.estore in = \fullyconnectednnTitleY,
118 inout/.is if = fullyconnectednnInout,
119 text width/.estore in = \fullyconnectednnTextWidth,
120 }
121 \newenvironment{fullyconnectednn}[1][]
122 {
123 \pgfkeys{/fullyconnectednn, default, #1}
124 \begin{center}
125 \begin{tikzpicture}
126 \setcounter{numlayers}{0}
127 \setcounter{nninputlayer}{0}
128 \setcounter{lastnnsize}{0}
129 }
130 {
131 \iffullyconnectednnInout
132 \begin{pgfonlayer}{back}
133 \foreach \i in {1,...,\thenninputlayer}
134 {
135 \draw[<-, shorten <=1pt] (0-\i) -- ++(-1,0) node[left]{\fullyconnectednnInput{\i}};
136 }
137
138 \foreach \i in {1,...,\thelastnnsize}
139 {
140 \draw[->, shorten <=1pt] (\the\numexpr\thenumlayers-1\relax-\i) -- ++(1,0) node[right]{\fullyconnectednnOutput{\i}};
141 }
142 \end{pgfonlayer}
143 \fi
144 \end{tikzpicture}
145 \end{center}
146 }
147
148 \endinput
149 %%
150 %% End of file `nndraw.sty'.