notación de dibujo de red neuronal tikz

notación de dibujo de red neuronal tikz

Quiero dibujar una red neuronal con 1 capa de entrada, 1 capa oculta y una capa de salida. Busqué un código de látex de muestra y agregué la capa de salida, es la siguiente:

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

el resultado es :

ingrese la descripción de la imagen aquí

cuando intento dibujar notación en cada unidad, busqué en el trabajo de otras personas, usan la propiedad de etiqueta label=180:$x_{\pgfmathresult}$` así que declaro

\pgfmathparse{\x}

y luego cambiar mi código a

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

sin embargo, tiene un error en la secuencia de control indefinida \x\pgfmathparse{\x}. No sé cómo solucionarlo. Gracias por cualquier ayuda.

Respuesta1

En comentario encontramos la solución a tu problema. Como me quejo de que su código es (innecesario) complejo (basado en un ejemplo relativamente antiguo), sugiero utilizar el siguiente código simplificado, que utiliza bibliotecas TikZ chainsy positioninguna sintaxis reciente para definir estilos y posicionar nodos:

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

ingrese la descripción de la imagen aquí

información relacionada