我正在尝试使用 tikz 绘制自回归 NN,但遇到了一些困难。这是我目前所做的:
\tikzset{%
every neuron/.style={
circle,
draw,
minimum size=1cm
},
neuron missing/.style={
draw=none,
scale=4,
text height=0.333cm,
execute at begin node=\color{black}$\vdots$
},
}
\begin{tikzpicture}[x=1.5cm, y=1.5cm, >=stealth]
\foreach \m/\l [count=\y] in {1,2,missing,3}
\node [every neuron/.try, neuron \m/.try] (input-\m) at (0,2.5-\y) {};
\foreach \m [count=\y] in {,2,3,4}
\node [every neuron/.try, neuron \m/.try ] (hidden-\m) at (2,2-\y*1.25) {};
\foreach \m [count=\y] in {1,missing,missing,3}
\node [every neuron/.try, neuron \m/.try ] (output-\m) at (4,2.5-\y) {};
\foreach \l [count=\i] in {1,2,p}
\draw [<-] (input-\i) -- ++(-1,0)
node [above, midway] {$y_{t-\l}$};
\foreach \l [count=\i] in {1,}
\draw [->] (output-\i) -- ++(1,0)
node [above, midway] {$y_t$};
\foreach \i in {1,...,3}
\foreach \j in {1,...,4}
\draw [->] (input-\i) -- (hidden-\j);
\foreach \i in {1,...,4}
\foreach \j in {1,...,1}
\draw [->] (hidden-\i) -- (output-\j);
\end{tikzpicture}
正如您所看到的,我得到了一些错误,但结构与我所寻找的类似,正是这样的:
如果有人能帮助我,我将不胜感激。我对 tikz 还很陌生,但愿意学习。谢谢!!
答案1
而不是一个完整的解决方案,请找到一些调试分析你的代码,以及一些观察。
我添加了arrows.meta
库,因此箭头提示会好一些。下面标记为注释。//我styles
为您的\draw
语句引入了箭头,故意留下一些颜色以支持调试。请稍后删除。您当前的stealth
语句可能已过时或没有效果。//我输入了一些注释以更好地构造代码,例如% input layer
。//您的一些问题似乎源于选项count
,如果以不同的方式完成,选项可能会过时。至少循环索引(如)\m
和计数器(如\y
)经常不同。//在您的方法中,在从和到的连接中,hidden-layer
计数器必须从 2 开始,而不是 1。
这是我的结果,来自下面的代码。请在代码后面查看观察结果。
\documentclass[10pt, border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}% to change the arrow tip
\begin{document}
\tikzset{%
every neuron/.style={
circle,
draw,
minimum size=1cm
},
neuron missing/.style={
draw=none,
scale=4,
text height=0.333cm,
execute at begin node=\color{black}$\vdots$
},
% arrow styles; color fordebugging
arr1/.style={-{Stealth}, orange},
arr2/.style={{Stealth}-, brown}
}
\begin{tikzpicture}[x=1.5cm, y=1.5cm, >=stealth]
% input layer
\foreach \m/\l [count=\y] in {1,2,missing,3}
\node [every neuron/.try, neuron \m/.try] (input-\m) at (0,2.5-\y) {};
% hidden layer
\foreach \m [count=\y] in {,2,3,4}
\node [every neuron/.try, neuron \m/.try ] (hidden-\m) at (2,2-\y*1.25) {};
% output layer
\foreach \m [count=\y] in {1,missing,missing,3}
\node [every neuron/.try, neuron \m/.try ] (output-\m) at (4,2.5-\y) {};
% CONNECTIONS
% inputs
\foreach \l [count=\i] in {1,2,p}
\draw [arr2] (input-\i) -- ++(-1,0)% <<< arrow style
node [above, midway] {$y_{t-\l}$};
%output
\foreach \l [count=\i] in { 3}% has to be 3 for a reason I don't understand
\draw [arr1] (output-\i) -- ++(1,0)% <<< arrow style
node [above, midway] {$y_t$};
% input to hidden
\foreach \i in {1,...,3}
\foreach \j in {2,...,4}% has to start at 2, not 1
\draw [->] (input-\i) -- (hidden-\j);
% hidden to output
\foreach \i in {2,...,4}% has to start at 2, not 1
\foreach \j in {1,...,1}
\draw [arr1] (hidden-\i) -- (output-\j);% <<< arrow style
\end{tikzpicture}
\end{document}
观察结果:当您将循环变量输出为节点文本时,隐藏层中未连接的节点既过时又有些晦涩难懂。// 对于输出,我会用两个简单的绘制语句替换循环。您也不希望那里有点。// 我可以使用额外的箭头样式来创建虚线。// 关于连接器处的标签,eta 是唯一一个可以是线本身的标签的标签。对于其他的,最好放置一些只放置标签的节点,因为您不想循环这种输出。