3

I want to draw a neural network with 1 input layer, 1 hidden layer and one output layer. I searched a sample latex code and add the output layer, it is as follows:

\documentclass{article}

\usepackage{tikz}
\begin{document}
\pagestyle{empty}

\def\layersep{2.5cm}

\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
    \tikzstyle{every pin edge}=[<-,shorten <=1pt]
    \tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
    \tikzstyle{input neuron}=[neuron, fill=green!50];
    \tikzstyle{output neuron}=[neuron, fill=red!50];
    \tikzstyle{hidden neuron}=[neuron, fill=blue!50];
    \tikzstyle{annot} = [text width=4em, text centered]

    % Draw the input layer nodes
    \foreach \name / \y in {1,...,4}
    % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
        \node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y) {};

    % Draw the hidden layer nodes
    \foreach \name / \y in {1,...,5}
        \path[yshift=0.5cm]
            node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};

    % Draw the output layer node
    %\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-3] (O) {};

    % Draw the input layer nodes
    \foreach \name / \y in {1,...,4}
    % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
        \node[output neuron, pin=right:Output \#\y] (O-\name) at (2*\layersep,-\y cm) {};

    % Connect every node in the input layer with every node in the
    % hidden layer.
    \foreach \source in {1,...,4}
        \foreach \dest in {1,...,5}
            \path (I-\source) edge (H-\dest);

    % Connect every node in the hidden layer with the output layer
    \foreach \source in {1,...,5}
        %\path (H-\source) edge (O);
        \foreach \dest in {1,...,4}
            \path (H-\source) edge (O-\dest);

    % Annotate the layers
    \node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
    \node[annot,left of=hl] {Input layer};
    \node[annot,right of=hl] {Output layer};
\end{tikzpicture}
% End of code
\end{document}

the result is :

enter image description here

when I try to draw notation in each unit, i searched from other people's work, they use label property label=180:$x_{\pgfmathresult}$` so i declare

\pgfmathparse{\x}

and then change my code to

\documentclass{article}

\usepackage{tikz}
\begin{document}
\pagestyle{empty}

\def\layersep{2.5cm}

\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
    \tikzstyle{every pin edge}=[<-,shorten <=1pt]
    \tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
    \tikzstyle{input neuron}=[neuron, fill=green!50];
    \tikzstyle{output neuron}=[neuron, fill=red!50];
    \tikzstyle{hidden neuron}=[neuron, fill=blue!50];
    \tikzstyle{annot} = [text width=4em, text centered]
    % Draw the input layer nodes

    \pgfmathparse{\x}


    \foreach \name / \y in {1,...,4}
    % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
        \node[input neuron, label=180:$x_{\pgfmathresult}$,
        pin=left:Input \#\y] (I-\name) at (0,-\y) {};

    % Draw the hidden layer nodes
    \foreach \name / \y in {1,...,5}
        \path[yshift=0.5cm]
            node[hidden neuron, label=180:$x_{\pgfmathresult}$], (H-\name) at (\layersep,-\y cm) {};

    % Draw the output layer node
    %\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-3] (O) {};

    % Draw the input layer nodes
    \foreach \name / \y in {1,...,4}
    % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
        \node[output neuron, label=180:$x_{\pgfmathresult}$,\documentclass[10pt]{•} pin=right:Output \#\y] (O-\name) at (2*\layersep,-\y cm) {};

    % Connect every node in the input layer with every node in the
    % hidden layer.
    \foreach \source in {1,...,4}
        \foreach \dest in {1,...,5}
            \path (I-\source) edge (H-\dest);

    % Connect every node in the hidden layer with the output layer
    \foreach \source in {1,...,5}
        %\path (H-\source) edge (O);
        \foreach \dest in {1,...,4}
            \path (H-\source) edge (O-\dest);

    % Annotate the layers
    \node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
    \node[annot,left of=hl] {Input layer};
    \node[annot,right of=hl] {Output layer};
\end{tikzpicture}
% End of code
\end{document}

however, it has an error undefined control sequence \x\pgfmathparse{\x}. I don't know how to solve it. Thank you for any help.

Alex
  • 603
  • (i) you need to define \x before you calculate \pgfmathparse{\x} (ii) what should be in labels that you need to calculate subscripts, (iii) your MWE is very complicated ... – Zarko Apr 18 '17 at 23:40
  • I'm sorry but what is MWE? – Alex Apr 18 '17 at 23:43
  • MWE: Minimal Working Example, documents which you provide in your question. I (or any other) can help you if you clarify what you like to have for x indices. – Zarko Apr 18 '17 at 23:47
  • Thank you. I have 4 units in input layer, 5 units in hidden layer, 4 units in output layer – Alex Apr 18 '17 at 23:51
  • For input units, I would like to add notation fron x1 to x4 and for output units, I would like to add notation from y1 to y4 . For hidden units, i would like to add notations from h1 to h5 – Alex Apr 18 '17 at 23:55
  • Please, be so kind in give a link from where you take first MWE in your question. I'm preaty sure that I already sow this code (and also made an answer, but I'm not sure) – Zarko Apr 18 '17 at 23:55
  • I don't understand what you mean with units. can you provide a sketch, what you like to have? – Zarko Apr 18 '17 at 23:56
  • the original is here http://www.texample.net/tikz/examples/neural-network/ – Alex Apr 18 '17 at 23:57
  • unit means circle – Alex Apr 18 '17 at 23:58
  • Then see if \node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y) {$x_{\name}$}; instead existed code line (in the first your MWE) gives what you like to have. – Zarko Apr 19 '17 at 00:02
  • see, if answer below gives what you like to have. – Zarko Apr 19 '17 at 00:45

1 Answers1

7

In comment we find the solution for your problem. Since I complain that your code is (unnecessary) complex (based is on relative old example) I suggest to use the following simplified code, which use TikZ libraries chains and positioning and recent syntax for defining of styles as well as for positioning of nodes:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{chains, positioning}

\begin{document}
    \begin{tikzpicture}[shorten >=1pt,->, draw=black!50, 
        node distance = 6mm and 24mm,
          start chain = going below,
every pin edge/.style = {<-,shorten <=1pt},
        neuron/.style = {circle, fill=#1, 
                         minimum size=17pt, inner sep=0pt,
                         on chain},
         annot/.style = {text width=4em, align=center}
                        ]
% Draw the input layer nodes
\foreach \i in {1,...,4}
    \node[neuron=green!50,
          pin=180:Input \#\i] (I-\i)    {$x_{\i}$};
% Draw the hidden layer nodes
    \node[neuron=blue!50,
          above right=6mm and 24mm of I-1.center] (H-1)     {$x_{1}$};
\foreach \i [count=\j from 1] in {2,...,5}
    \node[neuron=blue!50,
          below=of H-\j]      (H-\i)    {$x_{\i}$};
% Draw the output layer node
    \node[neuron=red!50,
          pin= {[pin edge=->]0:Output \#1},
          right=of I-1 -| H-1]  (O-1)   {$x_{1}$};
\foreach \i [count=\j from 1] in {2,...,4}
    \node[neuron=red!50,
          pin= {[pin edge=->]0:Output \#\j},
          below=of O-\j]        (O-\i)  {$x_{\i}$};
% Connect input nodes with hidden nodes and 
%  hiden nodes with output nodes with the output layer
    \foreach \i in {1,...,4}
        \foreach \j in {1,...,5}
{
    \path (I-\i) edge (H-\j)
          (H-\j) edge (O-\i);
}
    \end{tikzpicture}
\end{document}

enter image description here

Zarko
  • 296,517