彼此相邻

彼此相邻

我正在尝试用 tikz 创建此图表的“展开”版本:

在此处输入图片描述

这是用以下代码实现的:

\documentclass[a4paper, 11pt]{article}

    \usepackage{float}
    \usepackage{tikz}

    \usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
    \usetikzlibrary{decorations.text}
    \usetikzlibrary{decorations.pathmorphing}
    \usetikzlibrary{fit, arrows.meta}

    \usepackage{pgfplots}
    \pgfplotsset{compat=1.14}

    \usepackage[]{algorithm2e}

    \graphicspath{ {images/} }

\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$
  },
}

    \def\layersep{2.5cm}

\begin{document}

\begin{tikzpicture}[x=1.5cm, y=1.5cm, >=stealth]

\foreach \m/\l [count=\y] in {1,2,3,4}
  \node [every neuron/.try, neuron \m/.try] (input-\m) at (0,2.5-\y) {};

\foreach \m [count=\y] in {1,2}
  \node [every neuron/.try, neuron \m/.try ] (hidden-\m) at (2,2-\y*1.25) {};

\foreach \m [count=\y] in {1,2}
  \node [every neuron/.try, neuron \m/.try ] (output-\m) at (4,2-\y*1.25) {};

\foreach \l [count=\i] in {1,2,3,n}
  \draw [<-] (input-\i) -- ++(-1,0)
    node [above, midway] {};

\foreach \l [count=\i] in {1,n}
  \node [above] at (hidden-\i.north) {};

\foreach \l [count=\i] in {1,n}
  \draw [->] (output-\i) -- ++(1,0)
    node [above, midway] {};

\foreach \i in {1,...,4}
  \foreach \j in {1,...,2}
    \draw [->] (input-\i) -- (hidden-\j);

\foreach \i in {1,...,2}
  \foreach \j in {1,...,2}
    \draw [->] (hidden-\i) -- (output-\j);

\draw[->,shorten >=1pt] (hidden-1) to [out=90,in=180,loop,looseness=8.8] (hidden-1);
\draw[->,shorten >=1pt] (hidden-2) to [out=-90,in=180,loop,looseness=8.8] (hidden-2);

\draw[->,shorten >=1pt] (hidden-1) to [out=180,in=180,loop,looseness=2.8] (hidden-2);
\draw[->,shorten >=1pt] (hidden-2) to [out=180,in=180,loop,looseness=1.8] (hidden-1);


\foreach \l [count=\x from 0] in {Input, Hidden, Ouput}
  \node [align=center, above] at (\x*2,2) {\l \\ layer};

\end{tikzpicture}


\end{document}

现在,我正在尝试做这样的事情,但无法真正弄清楚如何修改我的代码,以便拥有三个彼此相邻的网络:

在此处输入图片描述

我查看了以下链接中的问题,但它与我想做的并不完全相同:如何绘制循环神经网络

那么我可以修改我的代码来实现这一点吗?你认为可以将网络旋转 -90 度,以便它们更好地适应 A4 页面吗?

答案1

一种可能的方法是添加另一个\foreach循环,将您所做的所有事情重复三次。请注意,节点位于例如(0+\X,2.5-\y),其中\X是新外层循环的循环变量。对节点名称进行了一些调整,以适应相同的三个实例。

\foreach可能需要对新添加的箭头(最后的循环)进行一些调整。

要旋转它,只需交换 x 和 y 坐标即可。下面,我交换了单位向量。

我注释掉了一些未使用和不可见的节点。

\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\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{document}
\begin{center}
\begin{tikzpicture}[
   % swap the unit vectors
   x={(0,-1cm)},
   y={(1.5cm,0)},
   >=stealth,
   ]
\foreach [count=\xx] \X in {0,6.5,13}{
\foreach \m/\l [count=\y] in {1,2,3,4}
  \node [every neuron/.try, neuron \m/.try] (input-\m-\xx) at (0+\X,2.5-\y) {};

\foreach \m [count=\y] in {1,2}
  \node [every neuron/.try, neuron \m/.try ] (hidden-\m-\xx) at (2+\X,2-\y*1.25) {};

\foreach \m [count=\y] in {1,2}
  \node [every neuron/.try, neuron \m/.try ] (output-\m-\xx) at (4+\X,2-\y*1.25) {};

\foreach \l [count=\i] in {1,2,3,n}
  \draw [<-] (input-\i-\xx) -- ++(-1,0);
%    node [above, midway] {};

%\foreach \l [count=\i] in {1,n}
%  \node [above] at (hidden-\i-\xx.north) {};

\foreach \l [count=\i] in {1,n}
  \draw [->] (output-\i-\xx) -- ++(1,0);
%    node [above, midway] {};

\foreach \i in {1,...,4}
  \foreach \j in {1,...,2}
    \draw [->] (input-\i-\xx) -- (hidden-\j-\xx);

\foreach \i in {1,...,2}
  \foreach \j in {1,...,2}
    \draw [->] (hidden-\i-\xx) -- (output-\j-\xx);

\draw[->,shorten >=1pt] (hidden-1-\xx) to [out=0,in=90,loop,looseness=8.8] (hidden-1-\xx);
\draw[->,shorten >=1pt] (hidden-2-\xx) to [out=180,in=90,loop,looseness=8.8] (hidden-2-\xx);

\draw[->,shorten >=1pt] (hidden-1-\xx) to [out=90,in=90,loop,looseness=1.7] (hidden-2-\xx);
\draw[->,shorten >=1pt] (hidden-2-\xx) to [out=90,in=90,loop,looseness=1] (hidden-1-\xx);


\foreach \l/\txt [count=\x from 0] in {Input/input, Hidden/hidden, Output/output}
  \node [align=center] at (\txt-1-\xx-|+0,3) {\l \\ layer};
}% end of outer foreach


\foreach [evaluate=\i as \j using int(\i+1)] \i in {1,2}
{
  \draw [red] (hidden-1-\i) to[bend left] (hidden-1-\j);
  \draw [red] (hidden-1-\i) to[bend left] (hidden-2-\j);
  \draw [red] (hidden-2-\i) to[bend right] (hidden-1-\j);
  \draw [red] (hidden-2-\i) to[bend right] (hidden-2-\j);


}
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

彼此相邻

但这仍然比文本块宽。

在此处输入图片描述

\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\tikzset{%
  every neuron/.style={
    circle,
    draw,
    minimum size=7mm
  },
  neuron missing/.style={
    draw=none, 
    scale=4,
    text height=0.333cm,
    execute at begin node=\color{black}$\vdots$
  },
}

\begin{document}
\begin{center}
\begin{tikzpicture}[
   % swap the unit vectors
   x={(0,-1cm)},
   y={(1.1cm,0)},
   >=stealth,
   ]
\foreach [count=\xx] \X in {0,4,8}{
\foreach \m/\l [count=\y] in {1,2,3,4}
  \node [every neuron/.try, neuron \m/.try] (input-\m-\xx) at (0,2.5-\y+\X) {};

\foreach \m [count=\y] in {1,2}
  \node [every neuron/.try, neuron \m/.try ] (hidden-\m-\xx) at (2,2-\y*1.25+\X) {};

\foreach \m [count=\y] in {1,2}
  \node [every neuron/.try, neuron \m/.try ] (output-\m-\xx) at (4,2-\y*1.25+\X) {};

\foreach \l [count=\i] in {1,2,3,n}
  \draw [<-] (input-\i-\xx) -- ++(-1,0);
%    node [above, midway] {};

%\foreach \l [count=\i] in {1,n}
%  \node [above] at (hidden-\i-\xx.north) {};

\foreach \l [count=\i] in {1,n}
  \draw [->] (output-\i-\xx) -- ++(1,0);
%    node [above, midway] {};

\foreach \i in {1,...,4}
  \foreach \j in {1,...,2}
    \draw [->] (input-\i-\xx) -- (hidden-\j-\xx);

\foreach \i in {1,...,2}
  \foreach \j in {1,...,2}
    \draw [->] (hidden-\i-\xx) -- (output-\j-\xx);

\draw[->,shorten >=1pt] (hidden-1-\xx) to [out=0,in=90,loop,looseness=8.8] (hidden-1-\xx);
\draw[->,shorten >=1pt] (hidden-2-\xx) to [out=180,in=90,loop,looseness=8.8] (hidden-2-\xx);

\draw[->,shorten >=1pt] (hidden-1-\xx) to [out=90,in=90,loop,looseness=1.7] (hidden-2-\xx);
\draw[->,shorten >=1pt] (hidden-2-\xx) to [out=90,in=90,loop,looseness=1] (hidden-1-\xx);

}% end of outer foreach

\foreach \l/\txt [count=\x from 0] in {Input/input, Hidden/hidden, Output/output}
  \node [align=center] at (\txt-1-\xx-|0,-2.5) {\l \\ layer};

\foreach [evaluate=\i as \j using int(\i+1)] \i in {1,2}
{
  \draw [red,->] (hidden-1-\i) to[bend left] (hidden-1-\j);
  \draw [red,->] (hidden-1-\i) to[bend left] (hidden-2-\j);
  \draw [red,->] (hidden-2-\i) to[bend right] (hidden-1-\j);
  \draw [red,->] (hidden-2-\i) to[bend right] (hidden-2-\j);


}
\end{tikzpicture}
\end{center}
\end{document}

相关内容