![TikZ:使用贝叶斯神经网络分布注释边缘](https://linux22.com/image/400026/TikZ%EF%BC%9A%E4%BD%BF%E7%94%A8%E8%B4%9D%E5%8F%B6%E6%96%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%E5%88%86%E5%B8%83%E6%B3%A8%E9%87%8A%E8%BE%B9%E7%BC%98.png)
有没有办法用“分布”来注释 TikZ 中的边缘,就像这张图的右半部分一样,说明了常规神经网络和贝叶斯神经网络之间的区别?
这是我目前所拥有的。
\documentclass[tikz]{standalone}
\def\layersep{2.5cm}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
\tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
\tikzstyle{input neuron}=[neuron, fill=green!40];
\tikzstyle{output neuron}=[neuron, fill=red!40];
\tikzstyle{hidden neuron}=[neuron, fill=blue!40];
% Input layer
\foreach \name / \y in {1,...,2}
\node[input neuron] (I-\name) at (0,0.5-2*\y) {$i\y$};
% Hidden layer
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[hidden neuron] (H-\name) at (2.5,-\y cm) {$h\y$};
% Output node
\node[output neuron, right of=H-3] (O) {$o$};
% Connect every node in the input layer with every node in the hidden layer.
\foreach \source in {1,...,2}
\foreach \dest in {1,...,5}
\path (I-\source) edge (H-\dest);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,5}
\path (H-\source) edge (O);
\begin{scope}[xshift=7cm]
\tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
\tikzstyle{input neuron}=[neuron, fill=green!40];
\tikzstyle{output neuron}=[neuron, fill=red!40];
\tikzstyle{hidden neuron}=[neuron, fill=blue!40];
% Input layer
\foreach \name / \y in {1,...,2}
\node[input neuron] (I-\name) at (0,0.5-2*\y) {$i\y$};
% Hidden layer
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[hidden neuron] (H-\name) at (2.5,-\y cm) {$h\y$};
% Output node
\node[output neuron, right of=H-3] (O) {$o$};
% Connect every node in the input layer with every node in the hidden layer.
\foreach \source in {1,...,2}
\foreach \dest in {1,...,5}
\path (I-\source) edge (H-\dest);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,5}
\path (H-\source) edge (O);
\end{scope}
\end{tikzpicture}
\end{document}
答案1
当然可以。您只需添加一个带有绘制函数的路径图的节点即可。为了方便起见,它附带了一种样式graph
,可以用作
\path (I-1) -- (H-1) node[graph={-0.3+0.6*exp(-6*\t*\t)}];
也就是说,您需要做的就是指定函数。如果您反复使用非常相似的函数,您可以使用它declare function
来让您的工作更轻松。
\documentclass[tikz]{standalone}
\def\layersep{2.5cm}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep,
neuron/.style={circle,fill=black!25,minimum size=17pt,inner sep=0pt},
input neuron/.style={neuron, fill=green!40},
output neuron/.style={neuron, fill=red!40},
hidden neuron/.style={neuron, fill=blue!40},
graph/.style={node contents={},midway,minimum size=1.1cm,
path picture={\draw[double=orange,white,thick,double distance=1pt,shorten
>=0pt] plot[variable=\t,domain=-0.5:0.5,samples=51]
({\t},{#1});}}]
% Input layer
\foreach \name / \y in {1,...,2}
\node[input neuron] (I-\name) at (0,0.5-2*\y) {$i\y$};
% Hidden layer
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[hidden neuron] (H-\name) at (2.5,-\y cm) {$h\y$};
% Output node
\node[output neuron, right of=H-3] (O) {$o$};
% Connect every node in the input layer with every node in the hidden layer.
\foreach \source in {1,...,2}
\foreach \dest in {1,...,5}
\path (I-\source) edge (H-\dest);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,5}
\path (H-\source) edge (O);
\begin{scope}[xshift=7cm]
% Input layer
\foreach \name / \y in {1,...,2}
\node[input neuron] (I-\name) at (0,0.5-2*\y) {$i\y$};
% Hidden layer
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[hidden neuron] (H-\name) at (2.5,-\y cm) {$h\y$};
% Output node
\node[output neuron, right of=H-3] (O) {$o$};
% Connect every node in the input layer with every node in the hidden layer.
\foreach \source in {1,...,2}
\foreach \dest in {1,...,5}
\path (I-\source) edge (H-\dest);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,5}
\path (H-\source) edge (O);
\path (I-1) -- (H-1) node[graph={-0.3+0.6*exp(-6*\t*\t)}];
\path (I-2) -- (H-2) node[graph={-0.3+0.6*exp(-25*(\t+0.15)*(\t+0.15))}];
\end{scope}
\end{tikzpicture}
\end{document}
或者,您可能想要使用pic
。语法非常相似,但有时pic
s 危害较小(但也不包含所有预定义锚点)。如果您遇到上述问题,请改用pic
s。
\documentclass[tikz]{standalone}
\def\layersep{2.5cm}
\begin{document}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep,
neuron/.style={circle,fill=black!25,minimum size=17pt,inner sep=0pt},
input neuron/.style={neuron, fill=green!40},
output neuron/.style={neuron, fill=red!40},
hidden neuron/.style={neuron, fill=blue!40},
pics/graph/.style={code={\draw[double=orange,white,thick,double distance=1pt,shorten
>=0pt] plot[variable=\t,domain=-0.5:0.5,samples=51]
({\t},{#1});}}]
% Input layer
\foreach \name / \y in {1,...,2}
\node[input neuron] (I-\name) at (0,0.5-2*\y) {$i\y$};
% Hidden layer
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[hidden neuron] (H-\name) at (2.5,-\y cm) {$h\y$};
% Output node
\node[output neuron, right of=H-3] (O) {$o$};
% Connect every node in the input layer with every node in the hidden layer.
\foreach \source in {1,...,2}
\foreach \dest in {1,...,5}
\path (I-\source) edge (H-\dest);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,5}
\path (H-\source) edge (O);
\begin{scope}[xshift=7cm]
% Input layer
\foreach \name / \y in {1,...,2}
\node[input neuron] (I-\name) at (0,0.5-2*\y) {$i\y$};
% Hidden layer
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[hidden neuron] (H-\name) at (2.5,-\y cm) {$h\y$};
% Output node
\node[output neuron, right of=H-3] (O) {$o$};
% Connect every node in the input layer with every node in the hidden layer.
\foreach \source in {1,...,2}
\foreach \dest in {1,...,5}
\path (I-\source) edge (H-\dest);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,5}
\path (H-\source) edge (O);
\path (I-1) -- (H-1) pic[midway]{graph={-0.3+0.6*exp(-6*\t*\t)}};
\path (I-2) -- (H-2) pic[midway]{graph={-0.3+0.6*exp(-25*(\t+0.15)*(\t+0.15))}};
\end{scope}
\end{tikzpicture}
\end{document}
答案2
在获取用户 121799 的很好的建议在每个边上使用plot
节点,然后稍微清理代码以保持其干燥,这就是我最终得到的结果。
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\def\layersep{2.5cm}
\newcommand\nn[1]{
% Input layer
\foreach \y in {1,...,2}
\node[neuron, fill=green!40] (i\y-#1) at (0,\y+1) {$i\y$};
% Hidden layer
\foreach \y in {1,...,4}
\path node[neuron, fill=blue!40] (h\y-#1) at (\layersep,\y) {$h\y$};
% Output node
\node[neuron, fill=red!40] (o-#1) at (2*\layersep,2.5) {$o$};
% Connect every node in the input layer with every node in the hidden layer.
\foreach \source in {1,...,2}
\foreach \dest in {1,...,4}
\path (i\source-#1) edge (h\dest-#1);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,4}
\path (h\source-#1) edge (o-#1);
}
\begin{document}
\begin{tikzpicture}[
shorten >=1pt,->,draw=black!70, node distance=\layersep,
neuron/.style={circle,fill=black!25,minimum size=20,inner sep=0},
edge/.style 2 args={pos={(mod(#1+#2,2)+1)*0.33}, font=\tiny},
distro/.style 2 args={
edge={#1}{#2}, node contents={}, minimum size=0.6cm, path picture={\draw[double=orange,white,thick,double distance=1pt,shorten >=0pt] plot[variable=\t,domain=-1:1,samples=51] ({\t},{0.2*exp(-100*(\t-0.05*(#1-1))^2 - 3*\t*#2))});}
},
weight/.style 2 args={
edge={#1}{#2}, node contents={\pgfmathparse{0.35*#1-#2*0.15}\pgfmathprintnumber[fixed]{\pgfmathresult}}, fill=white, inner sep=2pt
}
]
\nn{regular}
\begin{scope}[xshift=7cm]
\nn{bayes}
\end{scope}
% Draw weights for all regular edges.
\foreach \i in {1,...,2}
\foreach \j in {1,...,4}
\path (i\i-regular) -- (h\j-regular) node[weight={\i}{\j}];
\foreach \i in {1,...,4}
\path (h\i-regular) -- (o-regular) node[weight={\i}{1}];
% Draw distros for all Bayesian edges.
\foreach \i in {1,...,2}
\foreach \j in {1,...,4}
\path (i\i-bayes) -- (h\j-bayes) node[distro={\i}{\j}];
\foreach \i in {1,...,4}
\path (h\i-bayes) -- (o-bayes) node[distro={\i}{1}];
\end{tikzpicture}
\end{document}