我正在尝试使用以下代码。我想用以下代码替换隐藏层 29 个节点. 并想补充text1、text2 和 text3如下图所示。最后,我想替换Input #1 to Input #9
和Clump Thickness, Uniformity of Cell Size, Uniformity of Cell Shape, Marginal Adhesion, Single Epithelial Cell Size, Bare Nuclei, Bland Chromatin, Normal Nuclei, Mitoses.
请需要帮助。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\def\layersep{3.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,...,9}{
\node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y cm) {};
}
% set number of hidden layers
\newcommand\Nhidden{2}
\newcommand\Node{13}
\newcommand\Nod{9}
% Draw the hidden layer nodes
\foreach \N in {1,...,\Nhidden} {
\foreach \y in {1,...,\Node} {
\path[yshift=1.80cm]
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
\node[output neuron,pin={[pin edge={->}]right:Benin/Malignant}, right of=H\Nhidden-6] (O) {};
% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,9}{
\foreach \dest in {1,...,13}{
\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,...,13}
\foreach \dest in {1,...,13}
\path (H\lastN-\source) edge (H\N-\dest);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,13}
\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[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\def\layersep{3.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 [count=\y] in
{Clump Thickness, Uniformity of Cell Size, Uniformity of Cell Shape, Marginal Adhesion, Single Epithelial Cell Size, Bare Nuclei, Bland Chromatin, Normal Nuclei, Mitoses}
{ \node[input neuron, pin=left:\name] (I-\y) at (0,-\y cm) {};
}
% set number of hidden layers
\newcommand\Nhidden{2}
\newcommand\NodOne{13}
\newcommand\NodTwo{9}
\newcommand\Nod{9}
% Draw the hidden layer nodes
% \foreach \N in {1,...,\Nhidden} {
\foreach \y in {1,...,\NodOne} {
\path[yshift=1.80cm]
node[hidden neuron] (H1-\y) at (1*\layersep,-\y cm) {};
}
\node[annot,above of=H1-1, node distance=1cm] (hl1) {Hidden layer 1};
\foreach \y in {1,...,\NodTwo} {
\path[yshift=0cm]
node[hidden neuron] (H2-\y) at (2*\layersep,-\y cm) {};
}
\node[annot,above of=H2-1, node distance=1cm] (hl2) {Hidden layer 2};
% }
\node[below=0.5cm of H1-\NodOne] (text1) {text 1};
\node at (text1 -| H2-1) (text2) {text 2};
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Benin/Malignant}, right of=H\Nhidden-5] (O) {};
\node at (text1 -| O) (text3) {text 3};
% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,9}{
\foreach \dest in {1,...,\NodOne}{
\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,...,\NodOne}
\foreach \dest in {1,...,\NodTwo}
\path (H\lastN-\source) edge (H\N-\dest);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,\NodTwo}
\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}
答案2
我已经更新以进行自动分层。
\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=yellow!50},
hidden neuron/.style={neuron, fill=blue!50},
annot/.style={text width=4em, text centered}
]
% set number of hidden layers, input and output name
\csdef{N-HiddenL}{4}
\csdef{N-Input-yshift}{0.5cm}
\csdef{N-L1}{10}
\csdef{N-L1-yshift}{1.8cm}
\csdef{N-L1-annot}{1cm}
\csdef{N-L2}{8}
\csdef{N-L2-yshift}{1.30cm}
\csdef{N-L2-annot}{1cm}
\csdef{N-L3}{4}
\csdef{N-L3-yshift}{1.30cm}
\csdef{N-L3-annot}{1cm}
\csdef{N-L4}{4}
\csdef{N-L4-yshift}{1.30cm}
\csdef{N-L4-annot}{1cm}
\csdef{N-Output-yshift}{0.5cm}
\newcommand\InputList{x\textsubscript{1}, x\textsubscript{2}, x\textsubscript{3}}
\newcommand\HiddenList{Camada oculta \i}
\newcommand\OutputList{y\textsubscript{1}, y\textsubscript{2}, y\textsubscript{3}}
% draw the input layer nodes
\foreach \name [count=\i] in \InputList
\node[input neuron, pin=left:\name, yshift=(\csuse{N-Input-yshift})] (I-\i) at (0,-\i cm) {};
\cslet{N-Input}{\i}
% draw the hidden layer nodes
\foreach \i in {1,...,\csuse{N-HiddenL}}
{
\foreach \j in {1,...,\csuse{N-L\i}} {
\path[yshift=(\csuse{N-L\i-yshift})]
node[hidden neuron] (H\i-\j) at (\i*\layersep,-\j cm) {};
}
\node[annot,above of=H\i-1, node distance=(\csuse{N-L\i-annot})] (hl\i) {\HiddenList};
}
% draw the output layer node
\foreach \name [count=\i] in \OutputList
\node[output neuron,pin={[pin edge={->}]right:\name}, right of=H\csuse{N-HiddenL}-1, yshift=(\csuse{N-Output-yshift} - \i cm)] (O-\i) {};
\cslet{N-Output}{\i}
% annotate below the layers (draw text)
\foreach \name [count=\i] in
{test 1, test 2, text 3, text 4}
{
\ifnum \i=1
\node[below=0.5cm of H1-\csuse{N-L1}] (text1) {\name};
\else
\node at (text1 -| H\i-1) (text\i) {\name};
\fi
}
\node at (text1 -| O-1) (textO) {text out {\number\numexpr \csuse{N-HiddenL}+1}};
% connect every node in the input layer with every node in the hidden layer
\foreach \source in {1,...,\csuse{N-Input}}
\foreach \dest in {1,...,\csuse{N-L1}}
\path (I-\source) edge (H1-\dest);
% connect all hidden stuff
\foreach [remember=\N as \lastN (initially 1)] \N in {2,...,\csuse{N-HiddenL}}
\foreach \source in {1,...,\csuse{N-L\lastN}}
\foreach \dest in {1,...,\csuse{N-L\N}}
\path (H\lastN-\source) edge (H\N-\dest);
% connect every node in the hidden layer with the output layer
\foreach \source in {1,...,\csuse{N-L\csuse{N-HiddenL}}}
\foreach \dest in {1,...,\csuse{N-Output}}
\path (H\csuse{N-HiddenL}-\source) edge (O-\dest);
% annotate the layers
\node[annot,left of=hl1, yshift=-1cm] {Camada de entrada};
\node[annot,right of=hl\csuse{N-HiddenL}, yshift=-0.5cm] {Camada de saída};
\end{tikzpicture}