3

a post (Drawing neural network with tikz) gives a nice solution to draw a neural network diagram.

enter image description here

Is it possible to draw a neural network diagram with rectangles? something like this

enter image description here

it seems that tikz uses node to denote each neuron, how to draw a rectangle to represent the whole layer?

  • @Schrödinger'scat LOL. – user197952 Apr 3 at 2:46
6
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,bending,chains,decorations.markings}
\tikzset{% 
    attach arrow/.style={
    decoration={
        markings,
         mark=at position 0 with {\pgfextra{%
         \pgfmathsetmacro{\tmpArrowTime}{\pgfkeysvalueof{/tikz/arc arrow/length}/(\pgfdecoratedpathlength)}%
         \xdef\tmpArrowTime{\tmpArrowTime}}},
        mark=at position {#1-3*\tmpArrowTime} with {\coordinate(@1);},
        mark=at position {#1-2*\tmpArrowTime} with {\coordinate(@2);},
        mark=at position {#1-1*\tmpArrowTime} with {\coordinate(@3);},
        mark=at position {#1+\tmpArrowTime/2} with {\coordinate(@4);
        \draw[-{Stealth[length=\pgfkeysvalueof{/tikz/arc arrow/length},bend]}] plot[smooth]
         coordinates {(@1) (@2) (@3) (@4)};},
        },
     postaction=decorate,
     },
     attach arrow/.default=0.5,
     arc arrow/.cd,length/.initial=2mm,
}

\begin{document}
\begin{tikzpicture}[font=\sffamily,>={Stealth[bend]},
    block/.style={draw, rectangle,minimum height=1.6em,minimum width=9em,
    rotate=90,},decide/.code={\ifnum#1<7
     \tikzset{fill=purple!10,execute at begin node={$\mathsf{3}\times\mathsf{3}$ conv, 64}}
    \else
     \tikzset{fill=green!10,execute at begin node={$\mathsf{3}\times\mathsf{3}$ conv, 128}}
    \fi}]
 \begin{scope}[start chain=A placed {at={(\tikzchaincount*3em,0)}},
    nodes={on chain,block,join= by {thick,->},decide=\tikzchaincount}]
  \path foreach \X in {1,...,6}{node{}}
    node{/2}
    foreach \X in {1,...,7}{node{}};
 \end{scope}
 \foreach \X in {1,...,7}
 {\draw[thick,attach arrow] \ifnum\X=4 [dashed]\fi 
 (-4.5em+\X*6em,0) to[out=90,in=180] 
 (-1.5em+\X*6em,7em) to[out=0,in=90] (1.5em+\X*6em,0);}
 \path (A-1.north) -- ++ (-2em,0) node[anchor=south,rotate=90](pool){pool}
  -- ++ (-4em,0) node[block,anchor=south,fill=orange!10](A-0)
     {$\mathsf{3}\times\mathsf{3}$ conv, 64};
 \draw[thick,->] (A-0) -- (pool);
 \draw[thick,->] (pool) -- (A-1);
 \draw[thick,<-] (A-0) -- ++ (-6em,0);
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.