0

Can anyone pinpoint the issue here why I can't seem to make the arrows only one directional (->) between the input layer and the hidden layer, rather than bi-directional:

\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
    \tikzstyle{every pin edge}=[<-,shorten <=1pt]
    \tikzstyle{neuron}=[circle,draw,minimum size=0.8cm,inner sep=1pt]
%    \tikzstyle{input neuron}=[neuron, fill=green!50];
%    \tikzstyle{output neuron}=[draw=none];
%    \tikzstyle{hidden neuron}=[neuron, fill=blue!50];
    \tikzstyle{input neuron}=[neuron];
    \tikzstyle{output neuron}=[neuron];
    \tikzstyle{hidden neuron}=[neuron];

    \tikzstyle{annot} = [text width=4em, text centered]

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



    % Draw the hidden layer nodes
    \foreach \name / \y in {1,...,1}
        \path[yshift=0cm]
            node[hidden neuron] (H-\name) at (\layersep,-2 cm) {$\theta$} ;
             \draw  (H-1) edge node[above]{$w$=1} (I-1);
             \draw  (H-1) edge node[above]{$w$=1} (I-2);
             \draw  (H-1) edge node[below]{$v$=2} (I-3);


    % Draw the output layer node
    \foreach \name / \y in {1,...,1}
            \node[output neuron,pin={[pin edge={->}]right:Output (true value)}, right of=H-1] (O) {$O$};

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

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

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

enter image description here

  • Note that you draw all edges between the input and the hidden layer twice. – Bubaya Feb 18 at 12:32
1

for your simple neural network is sufficient the following code:

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

\begin{document}
    \begin{tikzpicture}[
    node distance = 8mm and 16mm,
                > = Straight Barb,
      start chain = going below,
    neuron/.style = {circle, draw=gray,
                   minimum size=7mm, inner sep=1pt,
                   on chain
                  },
 in-neuron/.style = {neuron, pin={[pin edge={<-,shorten <=1pt}]left: #1}
                  },
out-neuron/.style = {neuron, pin={[pin edge={->,shorten <=1pt}]right:#1}
                  },
every edge/.style = {draw=gray, ->, shorten >=1pt},
every edge quotes/.style = {anchor=south, font=\small,sloped}
                        ]
% input layer
\node (I-1) [in-neuron=$x_1$] {$-1$};
\node (I-2) [in-neuron=$x_2$] {$2$};
\node (I-3) [in-neuron=$x_3$] {$1$};
% hidden layer
\node (H-1) [neuron, right=of I-2]          {$\theta$} ;
% output layer
\node (O)   [out-neuron=Output, right=of H-1]  {$O$};
% synapses
\foreach \i [count=\j] in {w=1,w=2,v=3}
{
\path (I-\j) edge[->,"$\i$"] (H-1);
}
\path (H-1) edge[->] (O);
    % layers names
\begin{scope}[node distance=1mm,
               every node/.style={text width=4em, align=center, anchor=south}]
\node   [above=of I-1]              {Input layer};
\node   [above=of I-1.north -| H-1] {$S$};
\node   [above=of I-1.north -| O]   {Output layer};
\end{scope}
    \end{tikzpicture}
\end{document}

enter image description here

for more complex and fancy neural network see answer neural network.

  • Could you let me know how can I add an intermediate node between my \delta node and my $o$ as the output where delta connects to the new node and that one connects to the output? Thanks – Amir Feb 18 at 20:13
  • @Amir, where is \delta node? do you mean \theta? do you like to have two hidden layers? – Zarko Feb 18 at 20:19
  • my bad. yes, I meant this \node (H-1) [neuron, right=of I-2] {$\theta$} ; Nevermind, figured that out myself. Your answer is way more intuitive :) Thanks – Amir Feb 18 at 20:21
  • @Amir, you are welcome. i'm glad that you figured out yourself, how to add one more hidden layer. – Zarko Feb 18 at 20:29
2

Maybe this is because of some confusion in which direction the edges point?

Edit To bring some more structure into the code:

\documentclass{article}
\usepackage{tikz} % loading `mathtools` % loading `amsmath`

\newlength\layersep
\setlength\layersep{2cm}
\begin{document}    
\begin{tikzpicture}[
    shorten >=1pt,
    ->,
    draw=black!50,
    node distance=\layersep,
    neuron/.style ={
        circle,
        draw,
        minimum size=0.8cm,
        inner sep=1pt
    },
    input neuron/.style={
        neuron,
        pin = {
            [pin edge={<-,shorten <=1pt}]left:#1
        }
    },
    output neuron/.style={
        neuron,
        pin = {
            [pin edge={->,shorten >=1}]right:#1
        }
    },
    hidden neuron/.style = {
        neuron
    },
    annot/.style = {
        text width=4em,
        text centered
    }
]

    % Draw the input layer nodes
    \node[input neuron=$x_1$]  (I-1) at (0,-1) {-1};
    \node[input neuron=$x_2$] (I-2) at (0,-2) {2};
    \node[input neuron=$x_3$] (I-3) at (0,-3) {1};

    % Draw the hidden layer nodes
    \foreach \name / \y in {1,...,1}
        \path[yshift=0cm]
            node[hidden neuron] (H-\name) at (\layersep,-2 cm) {$\theta$} ;
     \draw  (I-1) edge node[above]{$w$=1} (H-1);
     \draw  (I-2) edge node[above]{$w$=1} (H-1);
     \draw  (I-3) edge node[below]{$v$=2} (H-1);

    % Draw the output layer node
    \foreach \name / \y in {1,...,1}
        \node[output neuron={Output (true value)}, right of=H-1] (O) {$O$};

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

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

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

\end{document}

enter image description here

  • Thanks, @Bubaya. I noticed the diff between \begin{tikzpicture}[shorten >=1pt,draw=black!50, node distance=\layersep] and \begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]and also \draw (H-1) edge[<-] node[above]{$w$=1} (I-1); but can't see to get the logic – Amir Feb 16 at 20:02
  • @Amir Well, the logic is that your arrows have to point into the right direction. If you want to set tips for the entire picture as you did, then you have to switch some of your start-target-pairs; see my edited answer. – Bubaya Feb 18 at 12:28

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.