Debian Codegraph @

texmf-dist/source/latex/nndraw/nndraw.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2021 by Carlos Cardoso Dias <[email protected]>
4 % ---------------------------------------------------------------------------
5 % This work may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either version 1.3
7 % of this license or (at your option) any later version.
8 % The latest version of this license is in
9 % http://www.latex-project.org/lppl.txt
10 % and version 1.3 or later is part of all distributions of LaTeX
11 % version 2005/12/01 or later.
12 %
13 % This work has the LPPL maintenance status `maintained'.
14 %
15 % The Current Maintainer of this work is Carlos Cardoso Dias.
16 %
17 % This work consists of the files nndraw.dtx and nndraw.ins
18 % and the derived filebase nndraw.sty.
19 %
20 % \fi
21 %
22 % \iffalse
23 %<*driver>
24 \ProvidesFile{nndraw.dtx}
25 %</driver>
26 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
27 %<package>\ProvidesPackage{nndraw}
28 %<*package>
29 [2021/06/22 1.0 nndraw is a LaTeX package which provides utilities to draw neural networks.]
30 %</package>
31 %
32 %<*driver>
33 \documentclass{ltxdoc}
34 \usepackage{nndraw}[2021/06/22]
35 \EnableCrossrefs
36 \CodelineIndex
37 \RecordChanges
38 \begin{document}
39 \DocInput{nndraw.dtx}
40 \PrintChanges
41 \PrintIndex
42 \end{document}
43 %</driver>
44 % \fi
45 %
46 % \CheckSum{129}
47 %
48 % \CharacterTable
49 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
50 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
51 % Digits \0\1\2\3\4\5\6\7\8\9
52 % Exclamation \! Double quote \" Hash (number) \#
53 % Dollar \$ Percent \% Ampersand \&
54 % Acute accent \' Left paren \( Right paren \)
55 % Asterisk \* Plus \+ Comma \,
56 % Minus \- Point \. Solidus \/
57 % Colon \: Semicolon \; Less than \<
58 % Equals \= Greater than \> Question mark \?
59 % Commercial at \@ Left bracket \[ Backslash \\
60 % Right bracket \] Circumflex \^ Underscore \_
61 % Grave accent \` Left brace \{ Vertical bar \|
62 % Right brace \} Tilde \~}
63 %
64 %
65 % \changes{1.0}{2021/06/22}{Converted to DTX file}
66 %
67 % \DoNotIndex{\newcommand,\newenvironment}
68 %
69 % \providecommand*{\url}{\texttt}
70 % \GetFileInfo{nndraw.dtx}
71 % \title{The \textsf{nndraw} package}
72 % \author{Carlos Cardoso Dias \\ \url{[email protected]}}
73 % \date{\fileversion~from \filedate}
74 %
75 % \maketitle
76 %
77 % \section{Introduction}
78 %
79 \textbf{nndraw} is a \LaTeX package which provides utilities to draw neural networks in an efficient way.
80 Currently this package provides utilities to draw fully connected feedforward neural networks with an arbitrary
81 number of layers described inside the `fullyconnectednn` environment using the command `nnlayer`.
82 %
83 % \section{Usage}
84 %
85 An example of usage is shown below, in this example, a fully connected feedforward
86 deep neural network is provided with two inputs in the first layer, one hidden layer
87 with four neurons and one output layer with one output. This example shows how easy it is
88 to customize the presence/absence of biases in any layer as well as its position.
89
90 \begin{verbatim}
91 \begin{fullyconnectednn}[biasy = -4.5,
92 titley = 1,
93 inout = false]
94 \nnlayer[title = Input layer]{2}
95 \nnlayer[title = Hidden layer]{4}
96 \nnlayer[title = Ouput layer, hasbias = false]{1}
97 \end{fullyconnectednn}
98 \end{verbatim}
99
100 \noindent Which is drawn as
101
102 \begin{fullyconnectednn}[biasy = -4.5,
103 titley = 1,
104 inout = false]
105 \nnlayer[title = Input layer]{2}
106 \nnlayer[title = Hidden layer]{4}
107 \nnlayer[title = Ouput layer, hasbias = false]{1}
108 \end{fullyconnectednn}
109 %
110 %
111 % \StopEventually{}
112 %
113 % \section{Implementation}
114 %
115 % \iffalse
116 %<*package>
117 % \fi
118 %
119 % \begin{macrocode}
120 \NeedsTeXFormat{LaTeX2e}
121 \ProvidesPackage{nndraw}[2021/06/20 Neural Networks Draw LaTeX package]
122 \RequirePackage{tikz}
123 \pgfdeclarelayer{back}
124 \pgfsetlayers{back,main}
125 % \end{macrocode}
126 %
127 % \begin{macro}{\nnlayerNoText}
128 % \begin{macrocode}
129 \newcommand\nnlayerNoText[1]{}
130 % \end{macrocode}
131 % \end{macro}
132 %
133 % \begin{macrocode}
134 \newif\ifnnlayerHasBias
135 \pgfkeys{
136 /nnlayer/.is family, /nnlayer,
137 default/.style = {title = , text = \nnlayerNoText, hasbias = true, bias = , color = , biascolor = },
138 title/.estore in = \nnlayerTitle,
139 text/.store in = \nnlayerText,
140 hasbias/.is if = nnlayerHasBias,
141 bias/.estore in = \nnlayerBias,
142 color/.estore in = \nnlayerColor,
143 biascolor/.estore in = \nnlayerBiasColor,
144 }
145 \newcounter{numlayers}
146 \newcounter{nninputlayer}
147 \newcounter{lastnnsize}
148 \tikzstyle{neuron}=[circle, minimum size=6mm, fill=gray!70]
149 \tikzstyle{neuron_connection}=[->, shorten >=1pt, gray!70]
150 % \end{macrocode}
151 %
152 % \begin{macro}{\nnlayer}
153 % \begin{macrocode}
154 \newcommand\nnlayer[2][]{
155 \pgfkeys{/nnlayer, default, #1}
156 \node[text width = \fullyconnectednnTextWidth, align = center] at (\fullyconnectednnLayersep *
157 \thenumlayers,\fullyconnectednnTitleY) {\nnlayerTitle};
158 \if\thenninputlayer0
159 \setcounter{nninputlayer}{#2}
160 \fi
161 \foreach \i in {1,...,#2}
162 {
163 \node[neuron, yshift=(#2 - \thenninputlayer)*5 mm, fill=\nnlayerColor] (\thenumlayers-\i) at (\thenumlayers * \fullyconnectednnLayersep,-\i) {\nnlayerText{\i}};
164 }
165 \if\thelastnnsize0
166 \else
167 \ifnnlayerHasBias
168 \node[neuron, yshift=5mm, fill=\nnlayerBiasColor] (bias-\thenumlayers) at (\thenumlayers * \fullyconnectednnLayersep -\fullyconnectednnLayersep + \fullyconnectednnBiasX, \fullyconnectednnBiasY) {\nnlayerBias};
169 \fi
170 \begin{pgfonlayer}{back}
171 % \end{macrocode}
172 % node connections
173 % \begin{macrocode}
174 \foreach \i in {1,...,\thelastnnsize}
175 {
176 \foreach \j in {1,...,#2}
177 {
178 \draw[neuron_connection] (\the\numexpr\thenumlayers-1\relax-\i) -- (\thenumlayers-\j);
179 }
180 }
181 % \end{macrocode}
182 % bias connections
183 % \begin{macrocode}
184 \ifnnlayerHasBias
185 \foreach \j in {1,...,#2}
186 {
187 \draw[neuron_connection] (bias-\thenumlayers) -- (\thenumlayers-\j);
188 }
189 \fi
190 \end{pgfonlayer}
191 \fi
192 \setcounter{lastnnsize}{#2}
193 \stepcounter{numlayers}
194 }
195 % \end{macrocode}
196 % \end{macro}
197 %
198 % \begin{macrocode}
199 \newif\iffullyconnectednnInout
200 \pgfkeys{
201 /fullyconnectednn/.is family, /fullyconnectednn,
202 default/.style = {input = \nnlayerNoText, output = \nnlayerNoText, layersep = 3, biasx = 0.75, biasy = -8.75, titley = 0.75, inout = true, text width = 2cm},
203 input/.store in = \fullyconnectednnInput,
204 output/.store in = \fullyconnectednnOutput,
205 layersep/.estore in = \fullyconnectednnLayersep,
206 biasx/.estore in = \fullyconnectednnBiasX,
207 biasy/.estore in = \fullyconnectednnBiasY,
208 titley/.estore in = \fullyconnectednnTitleY,
209 inout/.is if = fullyconnectednnInout,
210 text width/.estore in = \fullyconnectednnTextWidth,
211 }
212 % \end{macrocode}
213 %
214 % \begin{environment}{fullyconnectednn}
215 % \begin{macrocode}
216 \newenvironment{fullyconnectednn}[1][]
217 {
218 \pgfkeys{/fullyconnectednn, default, #1}
219 \begin{center}
220 \begin{tikzpicture}
221 \setcounter{numlayers}{0}
222 \setcounter{nninputlayer}{0}
223 \setcounter{lastnnsize}{0}
224 }
225 {
226 \iffullyconnectednnInout
227 \begin{pgfonlayer}{back}
228 % \end{macrocode}
229 % Inputs
230 % \begin{macrocode}
231 \foreach \i in {1,...,\thenninputlayer}
232 {
233 \draw[<-, shorten <=1pt] (0-\i) -- ++(-1,0) node[left]{\fullyconnectednnInput{\i}};
234 }
235
236 % \end{macrocode}
237 % Outputs
238 % \begin{macrocode}
239 \foreach \i in {1,...,\thelastnnsize}
240 {
241 \draw[->, shorten <=1pt] (\the\numexpr\thenumlayers-1\relax-\i) -- ++(1,0) node[right]{\fullyconnectednnOutput{\i}};
242 }
243 \end{pgfonlayer}
244 \fi
245 \end{tikzpicture}
246 \end{center}
247 }
248 % \end{macrocode}
249 % \end{environment}
250 %
251
252 %
253 % \iffalse
254 %</package>
255 % \fi
256 %
257 % \Finale
258 \endinput