신경망 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})

[yshift=0.5cm]레이어당 9개의 숨겨진 노드, 4개의 출력 노드가 있고 숨겨진 뉴런 구성에 하드 코딩되어 추가 -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}

여기에 이미지 설명을 입력하세요

관련 정보