我尝试重现下图,但在将输入 x1 和 x2 与“第 1 层”的前四个块连接起来以及将第 1 层与“第 2 层”的节点连接起来时遇到了困难。非常感谢。
这是我的代码:
\documentclass[tikz, border=0.125cm]{standalone}
\usetikzlibrary{calc, fit, positioning, quotes}% new libraries
\begin{document}
\tikzset{
every neuron/.style={rectangle, draw, minimum size=8mm},
layer labels/.style={above, align=center},
cercle/.style={circle,draw,fill=black!25,minimum size=17pt,inner sep=0pt}
}
\begin{tikzpicture}[x=16mm, y=16mm, >=stealth]
% neuron nodes with part of labels
\foreach \g [count=\y] in {1,2,3,4}
\foreach \b/\l in { 0/L}
{
\node[every neuron/.try, neuron \g/.try](n\b\g) at (\b,2.5-\y){}
}
\foreach \m [count=\y] in {1,2}
\foreach \j/\l in { 2/H, 4/S}
{
\ifnum\y<3
\node [every neuron/.try, neuron \m/.try] (n\j\m) at (\j,2.5-\y) {};
\else
\node [every neuron/.try, neuron \m/.try] (n\j\m) at (\j,2.5-\y) {};
\fi
}
% neuron labels not included in neuron nodes
%\foreach \l/\k in {I_K/0, H_L/2}
%\node [above] at (n\k2.north) {$\l$};
% inputs
\foreach \l [count=\i] in {1,2}
\draw [<-] (n0\i.west) -- ++(-1.1,0) node (in\i) [above, midway] {$x_\l$};
%\node (input) [draw, inner ysep=2mm, yshift=-2mm, fit=(in1) (in2)] {};
% w and L outputs
\foreach \l [count=\i] in {1,2}
\draw [->] (n4\i.east) -- ++(1.6,0)
node (wout\i) [above, midway] {$x_\l$}
node (Lout\i) [right, draw, minimum size=8mm, label=$L_\i$] {};% Local Model
% output
\node (output) [cercle,right=16mm] at ($(Lout1.east)!0.5!(Lout2.east)$) {};
\draw [->] (output.east) to["$\hat{y}$"] ++(1.1,0);
% neurons interconection
\foreach \i in {1,2}
\foreach \j in {1,2}
{
\draw [->] (n0\i) -- (n2\j);
\draw [->] (n2\i) -- (n4\j);
}
\foreach \j [count=\i] in {1,2}
\draw [->] (Lout\i.east) to ["$\hat{y}_\j$"] (output);
% neuron layers labels
\foreach \l [count=\x from 0] in {1,2,3,4,5}
\node [layer labels] at (\x*2,2.2) {Couche \\\l};
%\node [layer labels] at (6,2.2) {Local \\ Model};
%\node [layer labels] at (8,2.2) {Output \\ Agregation};
% x-fit L-fit conections
\end{tikzpicture}
\end{document}
答案1
欢迎来到 TeX.SE!!!
一个想法是将\foreach
句子内的几乎所有元素绘制两次,重复顶部和底部的“线”,但改变 y 轴方向。
这是我的建议:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{calc}
\tikzset
{
void/.style={inner sep=0},
rect/.style={void,rectangle,draw,minimum size=6mm},
circ/.style={void,circle, draw,minimum size=6mm},
}
\begin{document}
\begin{tikzpicture}[-latex,line cap=round]
% layers
\foreach\ii in {1,...,5}
{
\ifnum\ii<5
\draw[-,very thin,red,dashed] (1.5*\ii+0.75,3-0.4*\ii) -- (1.5*\ii+0.75,4);
\fi
\node at (1.5*\ii,4) {Layer $\ii$};
}
% nodes
\foreach[count=\ii]\i/\j in {x/1,y/-1}
{
\begin{scope}[y=\j cm]
\node (\i) at (0,2) {\strut$\i$}; % x,y
\node[rect] (A\ii) at (1.5,5*\j-2*\j*\ii) {$A_\ii$}; % A1,A2
\node[rect] (B\ii) at (1.5,-5+4*\ii) {$B_\ii$}; % B1,B2
\node[circ] (pi\ii) at (3,1) {$\pi$}; % pi
\node[circ] (N\ii) at (4.5,1) {$N$}; % N
\node[rect] (R\ii) at (6,1) {}; % rectangle
\node[void] (x\ii) at ($(R\ii)+(-0.15,0.5)$) [yshift=3*\j mm] {\strut$x$};
\node[void] (y\ii) at ($(R\ii) +(0.15,0.5)$) [yshift=3*\j mm] {\strut$y$};
\node at (3.75,1.25) {\strut$\omega_\ii$};
\node at (5.25,1.25) {\strut$\bar{\omega}_\ii$};
\node at (7,0.85) {\strut$\bar{\omega}_\ii f_\ii$};
\end{scope}
}
\node[circ] (sigma) at (7.5,0) {$\Sigma$};
\node[circ] (f) at (9,0) {$f$};
% arrows
\foreach\ii in {1,2}
{
\draw (x.east) -- (A\ii.west);
\draw (y.east) -- (B\ii.west);
\draw (A\ii.east) -- (pi\ii);
\draw (B\ii.east) -- (pi\ii);
\foreach\k in{1,2}
\draw (pi\ii) -- (N\k);
\draw (N\ii) -- (R\ii);
\draw (x\ii) --++ (0,\ii-1.5);
\draw (y\ii) --++ (0,\ii-1.5);
\draw (R\ii) -- (sigma);
}
\draw (sigma) -- (f);
\end{tikzpicture}
\end{document}