neural_network.tex

Download
latex 388 lines 11.1 KB
  1% neural_network.tex
  2% TikZ visualization of neural network architectures
  3% Demonstrates advanced TikZ techniques for drawing neural networks
  4
  5\documentclass[12pt,a4paper]{article}
  6
  7\usepackage{tikz}
  8\usepackage[margin=0.75in]{geometry}
  9\usepackage{amsmath}
 10
 11\usetikzlibrary{positioning, calc, arrows.meta, shapes}
 12
 13\title{Neural Network Visualizations with TikZ}
 14\author{LaTeX Student}
 15\date{\today}
 16
 17\begin{document}
 18
 19\maketitle
 20
 21\section{Simple Feedforward Neural Network}
 22
 23\begin{center}
 24\begin{tikzpicture}[
 25    neuron/.style={circle, draw=black, thick, minimum size=1cm, fill=blue!20},
 26    input/.style={circle, draw=black, thick, minimum size=1cm, fill=green!20},
 27    output/.style={circle, draw=black, thick, minimum size=1cm, fill=red!20},
 28    arrow/.style={->,>=Stealth, thick}
 29]
 30
 31    % Input layer
 32    \foreach \i in {1,2,3,4} {
 33        \node[input] (I\i) at (0, -\i*1.5) {$x_\i$};
 34    }
 35
 36    % Hidden layer 1
 37    \foreach \i in {1,2,3,4,5} {
 38        \node[neuron] (H1\i) at (3, -\i*1.2) {};
 39    }
 40
 41    % Hidden layer 2
 42    \foreach \i in {1,2,3,4} {
 43        \node[neuron] (H2\i) at (6, -\i*1.5) {};
 44    }
 45
 46    % Output layer
 47    \foreach \i in {1,2,3} {
 48        \node[output] (O\i) at (9, {-1.5-\i*1.5}) {$y_\i$};
 49    }
 50
 51    % Connections: Input to Hidden1
 52    \foreach \i in {1,2,3,4} {
 53        \foreach \j in {1,2,3,4,5} {
 54            \draw[arrow, gray!50] (I\i) -- (H1\j);
 55        }
 56    }
 57
 58    % Connections: Hidden1 to Hidden2
 59    \foreach \i in {1,2,3,4,5} {
 60        \foreach \j in {1,2,3,4} {
 61            \draw[arrow, gray!50] (H1\i) -- (H2\j);
 62        }
 63    }
 64
 65    % Connections: Hidden2 to Output
 66    \foreach \i in {1,2,3,4} {
 67        \foreach \j in {1,2,3} {
 68            \draw[arrow, gray!50] (H2\i) -- (O\j);
 69        }
 70    }
 71
 72    % Layer labels
 73    \node[above=0.5cm of I1, font=\large\bfseries] {Input Layer};
 74    \node[above=0.5cm of H11, font=\large\bfseries] {Hidden Layer 1};
 75    \node[above=0.5cm of H21, font=\large\bfseries] {Hidden Layer 2};
 76    \node[above=0.5cm of O1, font=\large\bfseries] {Output Layer};
 77
 78    % Dimension annotations
 79    \node[below=0.3cm of I4, font=\small] {(4 neurons)};
 80    \node[below=0.3cm of H15, font=\small] {(5 neurons)};
 81    \node[below=0.3cm of H24, font=\small] {(4 neurons)};
 82    \node[below=0.3cm of O3, font=\small] {(3 neurons)};
 83
 84\end{tikzpicture}
 85\end{center}
 86
 87\section{Convolutional Neural Network (CNN)}
 88
 89\begin{center}
 90\begin{tikzpicture}[
 91    scale=0.8,
 92    layer/.style={rectangle, draw=black, thick, minimum width=1.5cm, minimum height=2cm},
 93    conv/.style={layer, fill=blue!30},
 94    pool/.style={layer, fill=orange!30},
 95    fc/.style={layer, fill=green!30, minimum width=0.8cm, minimum height=1.5cm},
 96    arrow/.style={->,>=Stealth, thick}
 97]
 98
 99    % Input
100    \node[layer, fill=gray!20, minimum width=2cm, minimum height=2.5cm] (input) at (0,0) {};
101    \node[below=0.1cm of input] {Input};
102    \node[font=\tiny] at (input.center) {32×32×3};
103
104    % Conv1
105    \node[conv, right=1cm of input, minimum width=1.8cm, minimum height=2.3cm] (conv1) {};
106    \node[below=0.1cm of conv1] {Conv 3×3};
107    \node[font=\tiny] at (conv1.center) {32×32×32};
108
109    % Pool1
110    \node[pool, right=1cm of conv1, minimum width=1.5cm, minimum height=2cm] (pool1) {};
111    \node[below=0.1cm of pool1] {MaxPool};
112    \node[font=\tiny] at (pool1.center) {16×16×32};
113
114    % Conv2
115    \node[conv, right=1cm of pool1, minimum width=1.3cm, minimum height=1.8cm] (conv2) {};
116    \node[below=0.1cm of conv2] {Conv 3×3};
117    \node[font=\tiny] at (conv2.center) {16×16×64};
118
119    % Pool2
120    \node[pool, right=1cm of conv2, minimum width=1cm, minimum height=1.5cm] (pool2) {};
121    \node[below=0.1cm of pool2] {MaxPool};
122    \node[font=\tiny] at (pool2.center) {8×8×64};
123
124    % Flatten
125    \node[layer, fill=purple!20, right=1cm of pool2, minimum width=0.5cm, minimum height=3cm] (flatten) {};
126    \node[below=0.1cm of flatten] {Flatten};
127    \node[font=\tiny, rotate=90] at (flatten.center) {4096};
128
129    % FC1
130    \node[fc, right=1cm of flatten, minimum height=2.5cm] (fc1) {};
131    \node[below=0.1cm of fc1] {FC};
132    \node[font=\tiny, rotate=90] at (fc1.center) {512};
133
134    % FC2
135    \node[fc, right=0.8cm of fc1, minimum height=2cm] (fc2) {};
136    \node[below=0.1cm of fc2] {FC};
137    \node[font=\tiny, rotate=90] at (fc2.center) {256};
138
139    % Output
140    \node[fc, right=0.8cm of fc2, minimum height=1.2cm, fill=red!30] (output) {};
141    \node[below=0.1cm of output] {Softmax};
142    \node[font=\tiny, rotate=90] at (output.center) {10};
143
144    % Arrows
145    \draw[arrow] (input) -- (conv1);
146    \draw[arrow] (conv1) -- (pool1);
147    \draw[arrow] (pool1) -- (conv2);
148    \draw[arrow] (conv2) -- (pool2);
149    \draw[arrow] (pool2) -- (flatten);
150    \draw[arrow] (flatten) -- (fc1);
151    \draw[arrow] (fc1) -- (fc2);
152    \draw[arrow] (fc2) -- (output);
153
154    % Title
155    \node[above=1cm of conv2, font=\Large\bfseries] {Convolutional Neural Network Architecture};
156
157\end{tikzpicture}
158\end{center}
159
160\section{Recurrent Neural Network (RNN)}
161
162\begin{center}
163\begin{tikzpicture}[
164    node distance=2cm,
165    rnn/.style={rectangle, draw=black, thick, minimum width=2cm, minimum height=1.5cm, fill=blue!30},
166    state/.style={circle, draw=black, thick, minimum size=1cm, fill=orange!30},
167    arrow/.style={->,>=Stealth, thick}
168]
169
170    % Time steps
171    \foreach \t in {1,2,3,4} {
172        % Input
173        \node[state, fill=green!20] (x\t) at (\t*3, 0) {$x_\t$};
174
175        % RNN cell
176        \node[rnn] (rnn\t) at (\t*3, 2) {RNN};
177
178        % Hidden state
179        \node[state] (h\t) at (\t*3, 4) {$h_\t$};
180
181        % Output
182        \node[state, fill=red!20] (y\t) at (\t*3, 6) {$y_\t$};
183
184        % Vertical connections
185        \draw[arrow] (x\t) -- (rnn\t);
186        \draw[arrow] (rnn\t) -- (h\t);
187        \draw[arrow] (h\t) -- (y\t);
188    }
189
190    % Horizontal connections (hidden states)
191    \foreach \t in {1,2,3} {
192        \pgfmathtruncatemacro{\next}{\t+1}
193        \draw[arrow, blue, thick] (h\t) -- (h\next);
194    }
195
196    % Labels
197    \node[left=0.5cm of x1] {Input};
198    \node[left=0.5cm of rnn1] {RNN Cell};
199    \node[left=0.5cm of h1] {Hidden State};
200    \node[left=0.5cm of y1] {Output};
201
202    \node[below=0.3cm of x1] {$t=1$};
203    \node[below=0.3cm of x2] {$t=2$};
204    \node[below=0.3cm of x3] {$t=3$};
205    \node[below=0.3cm of x4] {$t=4$};
206
207\end{tikzpicture}
208\end{center}
209
210\section{LSTM Cell Architecture}
211
212\begin{center}
213\begin{tikzpicture}[
214    scale=1.2,
215    gate/.style={rectangle, draw=black, thick, minimum width=1.2cm, minimum height=0.8cm, fill=yellow!30},
216    operation/.style={circle, draw=black, thick, minimum size=0.6cm, fill=blue!20},
217    arrow/.style={->,>=Stealth, thick}
218]
219
220    % Input and previous hidden state
221    \node[operation, fill=green!20] (x) at (0, 0) {$x_t$};
222    \node[operation, fill=green!20] (h_prev) at (0, 2) {$h_{t-1}$};
223
224    % Cell state
225    \draw[ultra thick, blue] (-1, 4) -- (9, 4);
226    \node[left] at (-1, 4) {$C_{t-1}$};
227    \node[right] at (9, 4) {$C_t$};
228
229    % Gates
230    \node[gate] (forget) at (2, 1) {$\sigma$};
231    \node[below=0.2cm of forget, font=\small] {Forget};
232
233    \node[gate] (input_gate) at (4, 1) {$\sigma$};
234    \node[below=0.2cm of input_gate, font=\small] {Input};
235
236    \node[gate] (cell_gate) at (5.5, 1) {$\tanh$};
237    \node[below=0.2cm of cell_gate, font=\small] {Cell};
238
239    \node[gate] (output_gate) at (7.5, 1) {$\sigma$};
240    \node[below=0.2cm of output_gate, font=\small] {Output};
241
242    % Operations
243    \node[operation] (mult1) at (2, 4) {$\times$};
244    \node[operation] (mult2) at (5.5, 3) {$\times$};
245    \node[operation] (add) at (4, 4) {$+$};
246    \node[operation] (mult3) at (7.5, 5) {$\times$};
247    \node[operation] (tanh_out) at (7.5, 4.5) {$\tanh$};
248
249    % Output
250    \node[operation, fill=red!20] (h) at (9, 5.5) {$h_t$};
251
252    % Connections from inputs
253    \draw[arrow] (x) -- ++(0, 0.5) -| (forget);
254    \draw[arrow] (x) ++(0, 0.5) -| (input_gate);
255    \draw[arrow] (x) ++(0, 0.5) -| (cell_gate);
256    \draw[arrow] (x) ++(0, 0.5) -| (output_gate);
257
258    \draw[arrow] (h_prev) -- ++(0, -0.5) -| (forget);
259    \draw[arrow] (h_prev) ++(0, -0.5) -| (input_gate);
260    \draw[arrow] (h_prev) ++(0, -0.5) -| (cell_gate);
261    \draw[arrow] (h_prev) ++(0, -0.5) -| (output_gate);
262
263    % Gate to operations
264    \draw[arrow] (forget) -- (mult1);
265    \draw[arrow] (input_gate) -- ++(0, 1.5) -| (mult2);
266    \draw[arrow] (cell_gate) -- (mult2);
267    \draw[arrow] (output_gate) -- ++(0, 3) -| (mult3);
268
269    % Cell state operations
270    \draw[arrow] (mult1) -- (add);
271    \draw[arrow] (mult2) -- (add);
272    \draw[arrow] (add) -- ++(2.5, 0);
273
274    % Output path
275    \draw[arrow] (7.5, 4) -- (tanh_out);
276    \draw[arrow] (tanh_out) -- (mult3);
277    \draw[arrow] (mult3) -- (h);
278
279    % Output feedback
280    \draw[arrow, dashed, gray] (h) -- ++(1, 0) -- ++(0, -5) -- ++(-10, 0) -- (h_prev);
281
282    % Title
283    \node[above=0.5cm of add, font=\Large\bfseries] {LSTM Cell};
284
285\end{tikzpicture}
286\end{center}
287
288\section{Autoencoder Architecture}
289
290\begin{center}
291\begin{tikzpicture}[
292    node distance=1.5cm,
293    layer/.style={circle, draw=black, thick, minimum size=0.8cm, fill=blue!20},
294    arrow/.style={->,>=Stealth, thick, gray!50}
295]
296
297    % Encoder
298    \foreach \i in {1,...,5} {
299        \node[layer, fill=green!20] (e0-\i) at (0, -\i*0.8) {};
300    }
301
302    \foreach \i in {1,...,4} {
303        \node[layer] (e1-\i) at (1.5, {-0.4-\i*0.8}) {};
304    }
305
306    \foreach \i in {1,...,3} {
307        \node[layer] (e2-\i) at (3, {-0.8-\i*0.8}) {};
308    }
309
310    % Latent space
311    \foreach \i in {1,2} {
312        \node[layer, fill=yellow!30, minimum size=1cm] (latent-\i) at (4.5, {-1.2-\i*0.8}) {};
313    }
314
315    % Decoder
316    \foreach \i in {1,...,3} {
317        \node[layer] (d1-\i) at (6, {-0.8-\i*0.8}) {};
318    }
319
320    \foreach \i in {1,...,4} {
321        \node[layer] (d2-\i) at (7.5, {-0.4-\i*0.8}) {};
322    }
323
324    \foreach \i in {1,...,5} {
325        \node[layer, fill=red!20] (d3-\i) at (9, -\i*0.8) {};
326    }
327
328    % Connections (simplified - not all connections shown)
329    \foreach \i in {1,...,5} {
330        \foreach \j in {1,...,4} {
331            \draw[arrow] (e0-\i) -- (e1-\j);
332        }
333    }
334
335    \foreach \i in {1,...,4} {
336        \foreach \j in {1,...,3} {
337            \draw[arrow] (e1-\i) -- (e2-\j);
338        }
339    }
340
341    \foreach \i in {1,...,3} {
342        \foreach \j in {1,2} {
343            \draw[arrow] (e2-\i) -- (latent-\j);
344        }
345    }
346
347    \foreach \i in {1,2} {
348        \foreach \j in {1,...,3} {
349            \draw[arrow] (latent-\i) -- (d1-\j);
350        }
351    }
352
353    \foreach \i in {1,...,3} {
354        \foreach \j in {1,...,4} {
355            \draw[arrow] (d1-\i) -- (d2-\j);
356        }
357    }
358
359    \foreach \i in {1,...,4} {
360        \foreach \j in {1,...,5} {
361            \draw[arrow] (d2-\i) -- (d3-\j);
362        }
363    }
364
365    % Labels
366    \node[above=0.5cm of e0-1, font=\bfseries] {Encoder};
367    \node[above=0.5cm of latent-1, font=\bfseries] {Latent};
368    \node[above=0.5cm of d3-1, font=\bfseries] {Decoder};
369
370    \node[below=0.2cm of e0-5, font=\small] {Input};
371    \node[below=0.2cm of latent-2, font=\small] {Compressed};
372    \node[below=0.2cm of d3-5, font=\small] {Reconstruction};
373
374\end{tikzpicture}
375\end{center}
376
377\section{Compilation Notes}
378
379To compile this document:
380\begin{verbatim}
381pdflatex neural_network.tex
382\end{verbatim}
383
384For complex networks with many neurons, consider using loops and
385programmatic approaches to reduce code duplication.
386
387\end{document}