[Tex/LaTex] Recurrent neural network with tikz

tikz-pgf

I would like to draw one curve arrow from the output to the input. Could anyone please help me? thank you beforehand

\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) {};

    % 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);

    % 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}

Best Answer

I guess, that you like to obtain something similar:

enter image description here

but I'm not sure (you not defined where is your "input")

\documentclass{article}
\usepackage{tikz}

\begin{document}
\pagestyle{empty}
\usetikzlibrary{calc}

\def\layersep{2.5cm}

    \begin{tikzpicture}[shorten >=1pt,->,draw=black!50, 
    node distance = \layersep,
every pin edge/.style = {<-,shorten <=1pt},
        neuron/.style = {circle,fill=black!25,minimum size=17pt,inner sep=0pt},
  input neuron/.style = {neuron, fill=green!50},
 output neuron/.style = {neuron, fill=red!50},
 hidden neuron/.style = {neuron, fill=blue!50},
         annot/.style = {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) {};

% 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);

% 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};

% curve from output to input
\draw[line width=1mm, gray,->]  
    ([xshift=21mm] O.east) .. controls + (2,-1) and + (4,0) .. ([yshift=-7mm] H-5.south)
                           .. controls + (-5,0) and + (-2,-1) .. ([xshift=-27mm]$(I-1)!0.5!(I-4)$);
    \end{tikzpicture}
% End of code
\end{document}

Addendum: From OP comment follows that my guessing what curve should connect, was wrong (sorry, but my crystal ball is on strike :( ). Even now I'm not sure, if the curve lie as OP desire. However, the principle is in the first and this case the same and if also now the curve is not exactly where should be, only is necessary to accordingly change the start and end coordinates of the curve.

In version below are in comparison to the first one changed arrow type. For it is now used arrows.meta and bending library which enable more smooth positioned arrowhead with option flex. Also the curve, since now cross texts at inputs, is drawn on background layer. For this serves libraries backgrounds and scopes:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\pagestyle{empty}
\usetikzlibrary{arrows.meta, bending, backgrounds, scopes}

\def\layersep{2.5cm}

    \begin{tikzpicture}[shorten >=1pt,->,draw=black!50, 
    node distance = \layersep,
every pin edge/.style = {<-,shorten <=1pt},
        neuron/.style = {circle,fill=black!25,minimum size=17pt,inner sep=0pt},
  input neuron/.style = {neuron, fill=green!50},
 output neuron/.style = {neuron, fill=red!50},
 hidden neuron/.style = {neuron, fill=blue!50},
         annot/.style = {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) {};

% 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);

% 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};

% curve from output to input
\scoped[on background layer]
\draw[line width=1mm, gray!50, shorten >=1mm, shorten <=1mm, -{Latex[flex]}]  
    (O.east) .. controls + (1,-0.5) and + (3.5,0) .. ([yshift=-7mm] H-5.south)
             .. controls + (-6,0) and + (-2,-0.5) .. (I-1);
    \end{tikzpicture}
% End of code
\end{document}

enter image description here