下面的代码仅将 1 个节点视为输入。我怎样才能将其转换为 11 个输入节点,只需在输入节点 3 和输入节点 11 之间添加三个点即可。
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\def\layersep{3cm}
\def\nodeinlayersep{1.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}
% 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-2.5) {}; %%% <-- MODIFIED
% set number of hidden layers
\newcommand\Nhidden{3}
% Draw the hidden layer nodes
\foreach \N in {1,...,\Nhidden} {
\foreach \y in {1,...,5} { %%% MODIFIED (1,...,12 -> 1,...,5, and the next five lines)
\ifnum \y=4
\node at (\N*\layersep,-\y*\nodeinlayersep) {$\vdots$};
\else
\node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {$\frac{1}{1+e^{-x}}$};
\fi
}
\node[annot,above of=H\N-1, node distance=1cm] (hl\N) {Hidden layer \N};
}
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H\Nhidden-3] (O) {}; %%% <-- MODIFIED (from H\Nhidden-6 to H\Nhidden-3)
% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1}
\foreach \dest in {1,...,3,5} %%% <-- MODIFIED (1,...,12 -> 1...,3,5)
\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,...,3,5} %%% <-- MODIFIED (1,...,12 -> 1...,3,5)
\foreach \dest in {1,...,3,5} %%% <-- MODIFIED (1,...,12 -> 1...,3,5)
\path (H\lastN-\source) edge (H\N-\dest);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,3,5} %%% <-- MODIFIED (1,...,12 -> 1...,3,5)
\path (H\Nhidden-\source) edge (O);
% 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
您的代码比实际的要复杂得多。无论如何,这是一个修改后的版本:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\def\layersep{3cm}
\def\nodeinlayersep{1.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/1,2/2,3/3,4/4,11/5} {
% This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
\ifnum \y=4
\node at (0,-\y*\nodeinlayersep) {$\vdots$};
\else
\node[input neuron, pin=left:Input \#\name] (I-\name) at (0,-\y*\nodeinlayersep) {}; %%% <-- MODIFIED
\fi
}
% set number of hidden layers
\newcommand\Nhidden{3}
% Draw the hidden layer nodes
\foreach \N in {1,...,\Nhidden} {
\foreach \y in {1,...,5} { %%% MODIFIED (1,...,12 -> 1,...,5, and the next five lines)
\ifnum \y=4
\node at (\N*\layersep,-\y*\nodeinlayersep) {$\vdots$};
\else
\node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {$\frac{1}{1+e^{-x}}$};
\fi
}
\node[annot,above of=H\N-1, node distance=1cm] (hl\N) {Hidden layer \N};
}
\foreach \i in {2,3,11} {
\foreach \j in {1,2,3,5} {
\draw[->] (I-\i) -- (H1-\j);
}
}
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H\Nhidden-3] (O) {}; %%% <-- MODIFIED (from H\Nhidden-6 to H\Nhidden-3)
% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1}
\foreach \dest in {1,...,3,5} %%% <-- MODIFIED (1,...,12 -> 1...,3,5)
\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,...,3,5} %%% <-- MODIFIED (1,...,12 -> 1...,3,5)
\foreach \dest in {1,...,3,5} %%% <-- MODIFIED (1,...,12 -> 1...,3,5)
\path (H\lastN-\source) edge (H\N-\dest);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,3,5} %%% <-- MODIFIED (1,...,12 -> 1...,3,5)
\path (H\Nhidden-\source) edge (O);
% 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}