神经网络图

神经网络图

如果您能帮助我,我将不胜感激:我想插入这样的图表:

enter image description here

就我而言,我需要 3 个输入、4 个隐藏层和 2 个输出。

我知道有类似的主题,但我不知道如何修改代码。

谢谢。

编辑

例如,

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}

\begin{document}

    \begin{figure}[htp]
    \centering
    \begin{tikzpicture}[
plain/.style={
    draw=none,
    fill=none,
},
net/.style={
    matrix of nodes,
    nodes={
        draw,
        circle,
        inner sep=8.5pt
    },
    nodes in empty cells,
    column sep=0.6cm,
    row sep=-11pt
},
>=latex
]
\matrix[net] (mat)
{
    |[plain]| \parbox{1cm}{\centering Input\\layer} & |[plain]| \parbox{1cm}{\centering Hidden\\layer} & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
    & |[plain]| \\
    |[plain]| & \\
    & |[plain]| \\
    |[plain]| & |[plain]| \\
    & & \\
    |[plain]| & |[plain]| \\
    & |[plain]| \\
    |[plain]| & \\
    & |[plain]| \\
};
\foreach \ai [count=\mi ]in {2,4,...,10}
\draw[<-] (mat-\ai-1) -- node[above] {I\mi} +(-1cm,0);
\foreach \ai in {2,4,...,10}
{\foreach \aii in {3,6,9}
    \draw[->] (mat-\ai-1) -- (mat-\aii-2);
}
\foreach \ai in {3,6,9}
\draw[->] (mat-\ai-2) -- (mat-6-3);
\draw[->] (mat-6-3) -- node[above] {O1} +(1cm,0);
\end{tikzpicture}

\end{figure}
\end{document}

我不明白如何添加或删除单位。最重要的是,\ai\aii代表什么?

答案1

使用您的代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}

\begin{document}

    \begin{figure}[htp]
    \centering
    \begin{tikzpicture}[
plain/.style={
    draw=none,
    fill=none,
},
net/.style={
    matrix of nodes,
    nodes={
        draw,
        circle,
        inner sep=8.5pt
    },
    nodes in empty cells,
    column sep=0.6cm,
    row sep=-11pt
},
>=latex
]

\matrix[net] (mat)
{
    |[plain]| \parbox{1cm}{\centering Input\\layer} & |[plain]| \parbox{1cm}{\centering Hidden\\layer} & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
    |[plain]| & \\
    & |[plain]| \\
    |[plain]| & &  \\
    & |[plain]| \\
    |[plain]| & &  \\
    & |[plain]| \\
    |[plain]| & \\
};
\foreach \ai [count=\mi ]in {3,5,7}
\draw[<-] (mat-\ai-1) -- node[above] {Input \mi} +(-2cm,0);
\foreach \ai in {3,5,7}
{\foreach \aii in {2,4,...,8}
    \draw[->] (mat-\ai-1) -- (mat-\aii-2);
}
\foreach \ai in {2,4,...,8}
\draw[->] (mat-\ai-2) -- (mat-6-3);

\foreach \ai in {2,4,...,8}
\draw[->] (mat-\ai-2) -- (mat-4-3);

\foreach \ai [count=\mi ]in {4,6}
\draw[->] (mat-\ai-3) -- node[above] {Output \mi} +(2cm,0);
\foreach \ai in {3,5,7}
{\foreach \aii in {2,4,...,8}
    \draw[->] (mat-\ai-1) -- (mat-\aii-2);
}
\end{tikzpicture}

\end{figure}
\end{document}

enter image description here %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

或者:参见:http://www.texample.net/tikz/examples/neural-network/(谢尔·马格纳·福斯克)

修改:

\documentclass{article}

\usepackage{tikz}
\usepackage{verbatim}

% Basis: http://www.texample.net/tikz/examples/neural-network/
\begin{document}
\pagestyle{empty}

\def\layersep{2.5cm}

\begin{tikzpicture}[shorten >=1pt,->,draw=black!100, node distance=\layersep]
    \tikzstyle{every pin edge}=[<-,shorten <=1pt]
    \tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
    \tikzstyle{input neuron}=[neuron, fill=white!100,draw=black];
    \tikzstyle{output neuron}=[neuron, fill=white!100,draw=black];
    \tikzstyle{hidden neuron}=[neuron, fill=white!100,draw=black];
    \tikzstyle{annot} = [text width=4em, text centered]

% Draw the input layer nodes
\foreach \name / \y in {1,...,3}
% This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
    \node[input neuron, pin=left:Input \y] (I-\name) at (0,-\y) {};

% Draw the hidden layer nodes
\foreach \name / \y in {1,...,4}
    \path[yshift=0.5cm]
        node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};

% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-2] (O1) {};
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-3] (O2) {};

% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,3}
    \foreach \dest in {1,...,4}
        \path (I-\source) edge (H-\dest);

% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,4}
    \path (H-\source) edge (O1);
\foreach \source in {1,...,4}
    \path (H-\source) edge (O2);

% Annotate the layers
\node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl] {Output layer};

\end{tikzpicture}
% End of code
\end{document}

enter image description here

答案2

由于没有人真正演示过如何包含 4 个隐藏层,因此这里是另一个。这也没有 4 个隐藏层,但确实有 1 个以上。

内部连接强度由不透明度表示,从 25% 到 100% 随机变化。

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows.meta,quotes,shadows,decorations.pathreplacing,positioning,fadings}
\usepackage{cfr-lm}
\begin{document}
\colorlet{mewnol}{blue!75!cyan}%
\colorlet{allanol}{blue!50!black}%
\begin{tikzpicture}
  [
    lliw nn/.code={\colorlet{lliwnn}{#1}},
    lliw nn=mewnol,
    nn/.style={draw=lliwnn, inner color=white, outer color=lliwnn!5, circular drop shadow},
    font=\sffamily\plstyle,
    >=Latex,
    every edge/.append style={->},
    every edge quotes/.append style={font=\sffamily\plstyle\footnotesize,},
    semithick,
  ]
  \matrix (n) [matrix of nodes, nodes={circle, minimum size=20pt, thick}, nodes in empty cells, column sep=7.5mm, column 1/.append style={nodes={coordinate}}, column 12/.append style={nodes={coordinate}}, column 2/.append style={lliw nn=allanol}, column 11/.append style={lliw nn=allanol}]
  {
    &[10mm]&&|[nn]|&&|[nn]|&&|[nn]|&&&&[10mm]\\
    &&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&&\\
    &|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&\\
    &&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&\\
    &|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&\\
    &&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&\\
    &|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&\\
    &&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&&\\
    &&&|[nn]|&&|[nn]|&&|[nn]|&&&&\\
  };
  \foreach \i [count=\j from 1] in {3,5,7} \draw [allanol]  (n-\i-1) edge ["Input \j"] (n-\i-2) ;
  \foreach \i [count=\j from 1] in {4,6} \draw [allanol] (n-\i-11) edge ["Output \j"] (n-\i-12)  ;
  \begin{scope}[every edge/.append style={draw=mewnol, opacity={random(25,100)/100}}]
    \foreach \i in {3,5,7} \draw (n-\i-10) edge (n-4-11) edge (n-6-11);
    \foreach \i in {3,5,7} \foreach \j in {2,4,6,8} \draw (n-\i-2) edge (n-\j-3) (n-\j-9) edge (n-\i-10);
    \foreach \k [evaluate=\k as \m using {int(\k+1)}, evaluate=\k as \n using {int(\k+2)} ] in {3,5,7} \foreach \i in {2,4,6,8} \foreach \j in {1,3,5,7,9} \draw  (n-\i-\k) edge (n-\j-\m) (n-\j-\m) edge (n-\i-\n) ;
  \end{scope}
  \begin{scope}[decoration={brace, amplitude=7.5pt}, thick, every node/.append style={align=center}]
    \draw [decorate, mewnol] ([xshift=-2.5pt]n-1-3.west |- n-1-3.north) ++(0,7.5pt) coordinate (l) -- ([xshift=2.5pt]n-1-10.east |- l) node [above=7.5pt,midway] {Hidden\\Layers};
    \draw [decorate, allanol] ([xshift=-2.5pt]n-1-2.west |- l) -- ([xshift=2.5pt]n-1-2.east |- l) node [above=7.5pt,midway]  {Input\\Layer};
    \draw [decorate, allanol] ([xshift=-2.5pt]n-1-11.west |- l) -- ([xshift=2.5pt]n-1-11.east |- l) node [above=7.5pt,midway]  {Output\\Layer};
  \end{scope}
  \draw [thick, ->, allanol] ([xshift=-2.5pt,yshift=-10pt]current bounding box.south -| n-9-2.west) coordinate (ll) -- ([xshift=2.5pt]n-9-11.east |- ll) node [midway, below] {Flow of Information};
  \node (cs) [every edge quotes, anchor=base west, xshift=50mm, mewnol] at ([xshift=5pt,yshift=-12.5pt]n-1-1 |- current bounding box.south) {Connection Strength};
  \fill [fill=mewnol, path fading=west] (cs.base west) ++(-5pt,\pgflinewidth) rectangle ++(-50mm,5pt) ;
\end{tikzpicture}
\end{document}

neural net

答案3

这是更广泛的评论和理解的练习鲍勃·鲍勃答案比新答案更准确。我的发现:

  • 图像代码的内核是一个三列八行的矩阵
  • 第一行是神经网络层的名称
  • 在其他行中,空单元格用于显示神经网络中的节点
  • 未使用的单元格在他的答案中被指定为|[plain]|
  • 矩阵中并非所有单元格都明确定义(这会造成混淆)
  • 节点之间的连接用嵌套循环绘制(外层代表左层,内层代表右层)
  • 输入和输出在单循环中绘制

让我更好地理解答案的变化:

  • 严格定义矩阵中的所有单元格
  • 对于标记未使用的单元格定义新样式,例如ec(作为空单元格)
  • 重新定义风格plain,以便在使用中可以消除\parbox{...}

随着这些变化鲍勃·鲍勃的 MWE 变成:

\documentclass[tikz, margin=3mm] {standalone}
\usetikzlibrary{arrows.meta, % for edge arrows 
                matrix,      % nodes will set as matrix elements
                }
\begin{document}
    \begin{tikzpicture}[
           > = Stealth, semithick,
plain/.style = {draw=none, fill=none, yshift=11mm,
                text width=7ex,  align=center},% for text in images, 
   ec/.style = {draw=none},% for emty cells
  net/.style = {% for matrix style
    matrix of nodes,
    nodes={circle, draw, semithick, minimum size=12mm, inner sep=0mm},% circles in image
    nodes in empty cells,% for not used cells in matrix
  column sep = 16mm, % distance between columns in matrix 
     row sep = -3mm  % distance between rows in matrix
            },
]
\matrix[net] (m)% m is matrix name, it is used for names of cell: firs has name m-1-1
                % in empty space between ampersands will show circles: 
                % i.e.: nodes of the neural network
{
|[plain]| Input layer & |[plain]| Hidden layer  & |[plain]| Output layer  \\
|[ec]|                &                         & |[ec]|                  \\
                      & |[ec]|                  & |[ec]|                  \\
|[ec]|                &                         &                         \\
                      & |[ec]|                  & |[ec]|                  \\
|[ec]|                &                         &                         \\
                      & |[ec]|                  & |[ec]|                  \\
|[ec]|                &                         & |[ec]|                  \\
};
\% inputs
\foreach \in [count=\ir from 1] in {3,5,7}
\draw[<-] (m-\in-1.west) -- node[above] {Input \ir} +(-22mm,0);
% connections between nodes in the first and second layer
\foreach \j in {3,5,7}
{
\foreach \k in {2,4,6,8} \draw[->] (m-\j-1) -- (m-\k-2);
}
% connections between nodes in the second and third layer
\foreach \j in {2,4,6,8}
{
\foreach \k in {4,6} \draw[->] (m-\j-2) -- (m-\k-3);
}
% output
\draw[->] (m-4-3.east) -- node[above] {Output 1} +(22mm,0);
\draw[->] (m-6-3.east) -- node[above] {Output 2} +(22mm,0);
    \end{tikzpicture}
\end{document}

结果(几乎)与鲍勃·鲍勃的回答:

enter image description here

两个 MWE 之间也存在一些细微的、不重要的差异,例如使用的文档类别、节点大小以及确定用于输入和输出的坐标(添加了节点名称的锚点)。

相关内容