我想在放置点的位置添加文本,例如w_1, w_2, ... w_n
。这是代码:
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{%
neuron missing/.style={
draw=none,
scale=2,
text height=0.333cm,
execute at begin node=\color{black}$\vdots$
},
}
\newcommand{\DrawNeuronalNetwork}[2][]{
\xdef\Xmax{0}
\foreach \Layer/\X/\Col/\Miss/\Lab/\Count [count=\Y] in {#2}
{\pgfmathsetmacro{\Xmax}{max(\X,\Xmax)}
\xdef\Xmax{\Xmax}
\xdef\Ymax{\Y}
}
\foreach \Layer/\X/\Col/\Miss/\Lab/\Count [count=\Y] in {#2}
{\node[anchor=south] at ({2*\Y},{\Xmax/2+0.1}) {\Layer};
\foreach \m in {1,...,\X}
{
\ifnum\m=\Miss
\node [neuron missing] (neuron-\Y-\m) at ({2*\Y},{\X/2-\m}) {};
\else
\node [circle, draw=black, thick, inner sep=0pt,minimum size=1cm] (neuron-\Y-\m) at
({2*\Y},{\X/2-\m}) {};
\ifnum\Y=1
\else
\pgfmathtruncatemacro{\LastY}{\Y-1}
\foreach \Z in {1,...,\LastX}
{
\ifnum\Z=\LastMiss
\else
\draw[->] (neuron-\LastY-\Z) -- (neuron-\Y-\m);
\fi
}
\fi
\fi
\ifnum\Y=1
\ifnum\m=\X
\draw [<-] (neuron-\Y-\m) -- ++(-1,0) node [above, midway] {$\Lab_{\Count}$};
\else
\ifnum\m=\Miss
\else
\draw [<-] (neuron-\Y-\m) -- ++(-1,0) node [above, midway] {$\Lab_{\m}$};
\fi
\fi
\else
\ifnum\Y=\Ymax
\ifnum\m=\X
\draw [->] (neuron-\Y-\m) -- ++(1,0) node [above, midway] {$\Lab_{\Count}$};
\else
\ifnum\m=\Miss
\else
\draw [->] (neuron-\Y-\m) -- ++(1,0) node [above, midway] {$\Lab_{\m}$};
\fi
\fi
\else
\ifnum\m=1
\node[above=0pt of neuron-\Y-\m] {$\Lab_1$};
\fi
\ifnum\m=\X
\node[below=0pt of neuron-\Y-\m] {$\Lab_{\Count}$};
\fi
\fi
\fi
}
\xdef\LastMiss{\Miss}
\xdef\LastX{\X}
}
}
\begin{tikzpicture}[x=2.5cm, y=1.5cm, >=stealth,font=\sffamily]
\DrawNeuronalNetwork{
Capa de entrada/1/black/8/x/,
Capa oculta/7/black/6/H/k,
%Bottleneck Layer/3/blue/2/T/f,
%Demapping Layer/7/red/4/G/f,
Capa de salida/1/black/4/y(x)/}
\end{tikzpicture}
\end{document}
答案1
您的代码中发生了很多事情。也许一种更像 TikZ 的节点放置方式可以帮助您。
这里我使用chains
图书馆自动放置节点:
- 链
main
始终只有三个节点。 - 从这三个的中间(参见
start branch 2
样式)开始两个分支:main/up
和main/down
。
在main/up
我们将放置up nodes
− 1 个额外节点,在main/down
相同的 上down nodes
。
对于down
分支,我们定义一种样式down <down nodes - 1>
,设置\vdots
与总数的关系,down nodes
以便它始终是倒数第二个节点。
在最后一个\foreach
循环中也进行了类似的计算,我们希望\vdots
在绘制箭头时跳过该节点。
当然,您可以像我添加箭头一样x
,y(x)
手动绘制这些箭头。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta, quotes, chains, scopes}
\begin{document}
\begin{tikzpicture}[
x = 2.5cm, y = 1.5cm,
node distance = .5cm and 2.5cm,
%
vertex/.style={
shape = circle, draw, inner sep=+0pt, minimum size=1cm,
node contents=},
start branch 2/.style={
insert path={
{[start branch = up going above]}
{[start branch = down going below]}}},
%
> = {Stealth [sep=2pt 1]},
start chain = main going right,
up nodes/.initial = 4,
down nodes/.initial = 4,
declare function={
upnum(\x) = int(-\x + \pgfkeysvalueof{/tikz/up\space nodes}+1);
downnum(\x) = \x == \pgfkeysvalueof{/tikz/down\space nodes} ? "k"
: int( \x + \pgfkeysvalueof{/tikz/up\space nodes}-1);
}
]
\foreach \i in {1, 2, 3}
\node[on chain, vertex] [start branch \i/.try];
\path (main-1) edge[<-, "$x$" above] ++ (left:1)
(main-3) edge[->, "$y(x)$"above] ++ (right:1);
{ [continue branch=up]
\foreach \i in {2,...,\pgfkeysvalueof{/tikz/up nodes}}
\node[on chain, vertex]; }
{ [continue branch=down,
% second to last:
down \the\numexpr\pgfkeysvalueof{/tikz/down nodes}-1\relax/.style
= {node contents = $\vdots$, draw = none}]
\foreach \i in {2,...,\pgfkeysvalueof{/tikz/down nodes}}
\node[on chain, vertex, down \i/.try]; }
\path (main/up-end) [late options = {label = above:$H_1$}]
(main/down-end) [late options = {label = below:$H_k$}];
{ [nodes={node font=\scriptsize, sloped}]
\foreach \t[
evaluate={\n=upnum \t;}
] in {1,...,\pgfkeysvalueof{/tikz/up nodes}}
\path[->] (main-1) edge["$ w _{\n} $"] (main/up-\t)
(main/up-\t) edge["$ w'_{\n} $"] (main-3);
\foreach \t[
expand list,
evaluate={\n=downnum \t;}
] in {2, ..., % third to last, skip second to last, last
\the\numexpr\pgfkeysvalueof{/tikz/down nodes}-2\relax,
\pgfkeysvalueof{/tikz/down nodes}}
\path[->] (main-1) edge["$ w _{\n} $"] (main/down-\t)
(main/down-\t) edge["$ w'_{\n} $"] (main-3);
}
\end{tikzpicture}
\end{document}