神经网络 tikz 图的输出层上的多个输出居中

神经网络 tikz 图的输出层上的多个输出居中

这个问题正在构建一个解决方案这里。下面的代码需要优化,因为输出层没有位于图的其余部分的中心。

我想知道如何将输出层与图形的其余部分置于中心。

另外,我想知道如何设置输出层的正确位置,而不是像现在这样进行硬编码:

\node[output neuron, pin=right:Output \#\y] (O-\name) at (4*\layersep,-\y) {};

在此处输入图片描述

这是我目前拥有的代码:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\pagestyle{empty}

\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,...,8}
% 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) {};

% set number of hidden layers
\newcommand\Nhidden{3}

% Draw the hidden layer nodes
\foreach \N in {1,...,\Nhidden} {
   \foreach \y in {1,...,9} {
      \path[yshift=0.5cm]
          node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y cm) {};
       }
\node[annot,above of=H\N-1, node distance=1cm] (hl\N) {Hidden layer \N};
}

% Draw the output layer node
\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 (4*\layersep,-\y) {};
    %  How to calculate the exact location of the output layers instead of hardcoding the value

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

% connect all hidden stuff
\foreach [remember=\N as \lastN (initially 1)] \N in {2,...,\Nhidden}
   \foreach \source in {1,...,9}
       \foreach \dest in {1,...,9}
           \path (H\lastN-\source) edge (H\N-\dest);

% Connect every node in the hidden layer with the output layer
\foreach [remember=\N as \lastN (initially 3)] \N in {2,...,\Nhidden}
\foreach \source in {1,...,9}
    \foreach \dest in {1,...,4}
        \path (H\lastN-\source) edge (O-\dest);

% Annotate the layers

\node[annot,left of=hl1] {Input layer};
\node[annot,right of=hl\Nhidden] {Output layer};
\end{tikzpicture}
% End of code
\end{document} 

答案1

将输出层中的节点坐标从 更改(4*\layersep,-\y)(4*\layersep,-\y-2)

在此处输入图片描述

完成 MWE:

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

\begin{document}
\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,...,8}
% 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) {};

% set number of hidden layers
\newcommand\Nhidden{3}

% Draw the hidden layer nodes
\foreach \N in {1,...,\Nhidden} {
   \foreach \y in {1,...,9} {
      \path[yshift=0.5cm]
          node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y cm) {};
       }
\node[annot,above of=H\N-1, node distance=1cm] (hl\N) {Hidden layer \N};
}

% Draw the output layer node
\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 (4*\layersep,-\y-2) {};  % <-----------
    %  How to calculate the exact location of the output layers instead of hardcoding the value

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

% connect all hidden stuff
\foreach [remember=\N as \lastN (initially 1)] \N in {2,...,\Nhidden}
   \foreach \source in {1,...,9}
       \foreach \dest in {1,...,9}
           \path (H\lastN-\source) edge (H\N-\dest);

% Connect every node in the hidden layer with the output layer
\foreach [remember=\N as \lastN (initially 3)] \N in {2,...,\Nhidden}
\foreach \source in {1,...,9}
    \foreach \dest in {1,...,4}
        \path (H\lastN-\source) edge (O-\dest);

% Annotate the layers

\node[annot,left of=hl1] {Input layer};
\node[annot,right of=hl\Nhidden] {Output layer};
\end{tikzpicture}
\end{document}

答案2

很难给你一个非硬编码的答案,因为所有其他值都是硬编码的。你需要

at (4*\layersep,{-\y*1cm-(9-4-1)*0.5cm})

因为每层有 9 个隐藏节点,4 个输出节点,然后[yshift=0.5cm]在隐藏神经元的构造中有一个硬编码,因此额外的-1。结果是,如果您将输出节点的数量从更改4为另一个值,则需要将4上述公式中的更改为相同的值。

\documentclass{article}

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

\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,...,8}
% 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) {};

% set number of hidden layers
\newcommand\Nhidden{3}

% Draw the hidden layer nodes
\foreach \N in {1,...,\Nhidden} {
   \foreach \y in {1,...,9} {
      \path[yshift=0.5cm]
          node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y*1cm) {};
       }
\node[annot,above of=H\N-1, node distance=1cm] (hl\N) {Hidden layer \N};
}

% Draw the output layer node
\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 (4*\layersep,{-\y*1cm-(9-4-1)*0.5cm}) {};
    %  How to calculate the exact location of the output layers instead of hardcoding the value

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

% connect all hidden stuff
\foreach [remember=\N as \lastN (initially 1)] \N in {2,...,\Nhidden}
   \foreach \source in {1,...,9}
       \foreach \dest in {1,...,9}
           \path (H\lastN-\source) edge (H\N-\dest);

% Connect every node in the hidden layer with the output layer
\foreach [remember=\N as \lastN (initially 3)] \N in {2,...,\Nhidden}
\foreach \source in {1,...,9}
    \foreach \dest in {1,...,4}
        \path (H\lastN-\source) edge (O-\dest);

% Annotate the layers

\node[annot,left of=hl1] {Input layer};
\node[annot,right of=hl\Nhidden] {Output layer};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容