4

I want to create an FNN and DNN figures using tikz, I found on Internet this example for FNN and I like it: enter image description here

\documentclass{article}
\usepackage{tikz}

\begin{document}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\usetikzlibrary{positioning,calc}
\tikzset{%
  every neuron/.style={circle,draw,minimum size=1cm},
  neuron missing/.style={draw=none,scale=4,text height=0.333cm,execute at begin node=\color{black}$\vdots$},
}
\begin{figure}
    \centering
    \begin{tikzpicture}[
        plain/.style={
          draw=none,
          fill=none,
          },
        dot/.style={draw,shape=circle,minimum size=3pt,inner sep=0,fill=black
          },
        net/.style={
          matrix of nodes,
          nodes={
            draw,
            circle,
            inner sep=8.5pt
            },
          nodes in empty cells,
          column sep=0.5cm,
          row sep=-11pt
          },
        >=latex
        ]
        \matrix[net] (mat)
        {
        |[plain]| \parbox{1cm}{\centering Input\\layer} 
                    & |[plain]| \parbox{1cm}{\centering Hidden\\layer} 
                                &  |[plain]| \parbox{1cm}{\centering ...} 
                                            &  |[plain]| \parbox{2cm}{\centering Hidden\\layer n} 
                                                            & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
                    & |[plain]| & |[plain]| & |[plain]|     & |[plain]|                                   \\
        |[plain]|   &           & |[plain]| &               & |[plain]|    \\
                    & |[plain]| & |[plain]| & |[plain]|     &              \\
        |[plain]|   & |[dot]|   & |[plain]| & |[dot]|                 \\
                    & |[plain]| & |[plain]| & |[plain]|     & |[dot]|      \\
        |[plain]|   & |[dot]|   & |[plain]| & |[dot]|       & |[plain]|    \\
        |[dot]|     & |[plain]| & |[plain]| & |[plain]|     & |[dot]|      \\
        |[dot]|     & |[dot]|   & |[plain]| & |[dot]|       & |[plain]|    \\
        |[dot]|     & |[plain]| & |[plain]| & |[plain]|     &              \\
        |[plain]|   &           & |[plain]| &               & |[plain]|    \\
                    & |[plain]| & |[plain]|            \\
        };






        \foreach \ai/\mi in {2/Input feature 1,4/Input feature 2,6/Input feature 3,12/Input feature n}
          \draw[<-] (mat-\ai-1) -- node[above] {\mi} +(-3cm,0);
        \foreach \ai in {2,4,6,12}
        {\foreach \aii/\mii in {3/N1,11/Nn}
          \draw[->] (mat-\ai-1) -- (mat-\aii-2) node[yshift=0.6cm] {\mii};
        }
        \foreach \ai in {3,11}
        {  \draw[->] (mat-4-3) -- node[above] {Output 1} +(2cm,0);}
        \foreach \ai in {3,11}
        {  \draw[->] (mat-\ai-2) -- (mat-10-3);
          \draw[->] (mat-10-3) -- node[above] {Output n} +(2cm,0);}
    \end{tikzpicture}
    \caption{Feedforward Neural Network structure.}
    \label{fig:FNN}
\end{figure}
\end{document}

But I am not so familiar with Latex and tikz so I do not know how to modify it so it can work for my DNN too. This is how it looks my code after my modifications: enter image description here

Here is how I want to look like: enter image description here

How can I do that? Thank you!

Sim
  • 61
  • 1
    welcome to tex.se! Please extend your code snipet with small but complete document beginning with \documentclass ... and ending with \end{document} which contain your image only. Searching this site will give you solution for many similar neural networks. – Zarko Nov 13 '18 at 09:11
  • see, if https://tex.stackexchange.com/questions/365404 can help you. – Zarko Nov 13 '18 at 09:16
  • H, I updated my code and what I want as a result tried a lot but cannot get my desired result. Can you help me? – Sim Nov 14 '18 at 02:30
  • well, as I see you have already found a solution ... +1 for it :-) – Zarko Nov 14 '18 at 08:15

1 Answers1

1

I found my solution after this way:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\usetikzlibrary{positioning,calc}
\tikzset{%
  every neuron/.style={circle,draw,minimum size=1cm},
  neuron missing/.style={draw=none,scale=4,text height=0.333cm,execute at begin node=\color{black}$\vdots$},
}
\begin{center}
    \begin{figure}
        \begin{tikzpicture}[
            plain/.style={
              draw=none,
              fill=none,
              },
            dot/.style={draw,shape=circle,minimum size=3pt,inner sep=0,fill=black
              },
            net/.style={
              matrix of nodes,
              nodes={
                draw,
                circle,
                inner sep=8.5pt
                },
              nodes in empty cells,
              column sep=0.1cm,
              row sep=-11pt
              },
            >=latex
            ]
            \matrix[net] (mat)
            {
            |[plain]| \parbox{1cm}{\centering Input\\layer} 
                        & |[plain]| \parbox{1.2cm}{\centering Hidden\\layer 1} 
                                    &  |[plain]| \parbox{0cm}{\centering ...} 
                                                &  |[plain]| \parbox{1.2cm}{\centering Hidden\\layer n} 
                                                                & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
                        & |[plain]| & |[plain]| & |[plain]|     & |[plain]|                                   \\
            |[plain]|   &           & |[plain]| &               & |[plain]|    \\
                        & |[plain]| & |[plain]| & |[plain]|     &              \\
            |[plain]|   & |[dot]|   & |[dot]|   & |[dot]|                 \\
                        & |[plain]| & |[plain]| & |[plain]|     & |[dot]|      \\
            |[plain]|   & |[dot]|   & |[dot]|   & |[dot]|       & |[plain]|    \\
            |[dot]|     & |[plain]| & |[plain]| & |[plain]|     & |[dot]|      \\
            |[dot]|     & |[dot]|   & |[dot]|   & |[dot]|       & |[plain]|    \\
            |[dot]|     & |[plain]| & |[plain]| & |[plain]|     &              \\
            |[plain]|   &           & |[plain]| &               & |[plain]|    \\
                        & |[plain]| & |[plain]|            \\
            };
            \foreach \ai/\mi in {2/Input feature 1,4/Input feature 2,6/Input feature 3,12/Input feature n}
              \draw[<-] (mat-\ai-1) -- node[above] {\mi} +(-3cm,0);

            \foreach \ai in {2,4,6,12}
            {\foreach \aii/\mii in {3/N1,11/Nn}
                \draw[->] (mat-\ai-1) -- (mat-\aii-2) node[yshift=0cm] {\mii};
            }
            \foreach \ai in {3,11}
            {  
                \draw[->] (mat-\ai-4) -- (mat-4-5);
                \draw[->] (mat-4-5) -- node[above] {Output 1} +(2cm,0);\
            }
            \foreach \ai in {3,11}
            {
                \draw[->] (mat-\ai-4) -- (mat-10-5);
                \draw[->] (mat-10-5) -- node[above] {Output n} +(2cm,0);
            }
        \end{tikzpicture}
        \caption{Deep Neural Network structure.}
        \label{fig:DNN}
    \end{figure}
\end{center}
\end{document}

enter image description here

Sim
  • 61