tests/testthat/test-nndiagram.R

test_that("nndiagram works", {
  expect_equal(nndiagram(input=3,hidden = c(3,3)), c("% To make the code work in any 'LaTeX' editor, users need to install and import two 'TeX' packages in the setting, including 'TikZ'. ",
                                                     "% Users are recommended to try the output 'LaTeX' code in Overleaf. ",
                                                     " ",
                                                     "\\usepackage{tikz} ",
                                                     "\\usepackage{ifthen} ",
                                                     "\\def\\layersep{2.5cm} ",
                                                     "\\newcommand*\\circled[1]{\\tikz[baseline=(char.base)]{ ",
                                                     "  \\node[shape=rectangle,inner sep=3pt, draw=black!100, fill= black!25] (char) {#1};}} ",
                                                     " ",
                                                     "\\begin{figure}[!ht] ",
                                                     "\\centering ",
                                                     "\\begin{tikzpicture}[shorten >=1pt,->,draw=black!100, node distance=\\layersep, scale=1] ",
                                                     "  \\tikzstyle{every pin edge}=[<-,shorten <=1pt]; ",
                                                     "  \\tikzstyle{neuron}=[circle, draw=black!100, minimum size=17pt,inner sep=0pt]; ",
                                                     "  \\tikzstyle{input neuron}=[neuron]; ",
                                                     "  \\tikzstyle{output neuron}=[neuron]; ",
                                                     "  \\tikzstyle{hidden neuron}=[neuron]; ",
                                                     "  \\tikzstyle{annot} = [text width=4em, text centered, text=black!100] ",
                                                     " ",
                                                     "  % drawing neurons ",
                                                     "  \\foreach \\name / \\y in {1,...,3} ",
                                                     "      \\node [input neuron, pin=left:\\textcolor{black!100}{Input \\y}] (I-\\name) at (0,-\\y) {};",
                                                     "  \\foreach \\name / \\y in {1/1,2/2,3/3} ",
                                                     "      \\path[yshift=0cm] ",
                                                     "          node[hidden neuron] (H-\\name) at (1* \\layersep,-\\y cm) {};",
                                                     "  \\foreach \\name / \\y in {4/1,5/2,6/3} ",
                                                     "      \\path[yshift=0cm] ",
                                                     "          node[hidden neuron] (H-\\name) at (2* \\layersep,-\\y cm) {};",
                                                     "  \\node[output neuron,pin={[pin edge={->}]right:\\textcolor{black!100}{Output}}, right of=H-5, yshift=0cm] (O) {}; ",
                                                     "",
                                                     "   % drawing arrows ",
                                                     "  \\foreach \\source in {1,...,3} ",
                                                     "      \\foreach \\dest in {1,...,3}",
                                                     "           \\path (I-\\source) edge (H-\\dest); ",
                                                     "  \\foreach \\source in {1,...,3} ",
                                                     "      \\foreach \\dest in {4,...,6}",
                                                     "           \\path (H-\\source) edge (H-\\dest); ",
                                                     "  \\foreach \\source in {4,...,6}",
                                                     "           \\path (H-\\source) edge (O);",
                                                     " ",
                                                     "   % annotations ",
                                                     "  \\node[annot,above of=I-1, node distance=2cm] {Input layer}; ",
                                                     "  \\node[annot,above of=I-1, node distance=1cm] {$\\circled{3}$}; ",
                                                     "  \\node[annot,above of=H-1, node distance=2cm] (hl1) {Hidden layer 1}; ",
                                                     "  \\node[annot,above of=H-1, node distance=1cm] (hl1) {$\\circled{3}$}; ",
                                                     "  \\node[annot,above of=H-4, node distance=2cm] (hl2) {Hidden layer 2}; ",
                                                     "  \\node[annot,above of=H-4, node distance=1cm] (hl2) {$\\circled{3}$}; ",
                                                     "  \\node[annot,above of =O, node distance=3cm] {Output layer}; ",
                                                     "  \\node[annot,above of =O, node distance=2cm] {$\\circled{1}$}; ",
                                                     "",
                                                     "\\end{tikzpicture} ",
                                                     "\\caption{} ",
                                                     "\\label{} ",
                                                     "\\end{figure} "                                                                                                    ))
})

Try the nndiagram package in your browser

Any scripts or data that you put into this service are public.

nndiagram documentation built on April 18, 2023, 5:11 p.m.