使用 TikZ 进行自下而上的神经网络

使用 TikZ 进行自下而上的神经网络

我需要创建一个神经网络图。我在网上找到了这个例子:http://www.texample.net/tikz/examples/neural-network/

但是这个示例展示了如何从左到右创建图表。我的目标是从下往上创建图表。所以我想在底部显示可见层,然后向上显示隐藏层。我该如何实现呢?

编辑:

我正在尝试复制这个确切的数字:

我想要的是

答案1

更新:楼主又问了第二个问题。这个解决方案修改了第一个解决方案,并删除了一些冗余,希望这次可以编译通过。

在此处输入图片描述

代码

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\pagestyle{empty}

\def\layersep{2.5cm}

\tikzset{neuron/.style={circle,thick,fill=black!25,minimum size=17pt,inner sep=0pt},
    input neuron/.style={neuron, draw,thick, fill=gray!30},
    hidden neuron/.style={neuron,fill=white,draw},
    hoz/.style={rotate=-90}}   %<--- for labels

\begin{tikzpicture}[-,draw=black, node distance=\layersep,transform shape,rotate=90]  %<-- rotate the NN

% Draw the input layer nodes
\foreach \name / \y in {1/1,2/2,3/3,5/m}
\node[input neuron, hoz] (I-\name) at (0,-\name) {\color{red}$b_\y$};

\node[hoz] (I-4) at (0,-4) {$\dots$};

\foreach \name / \y in {1/1,2/2,3/3,5/m}
\path[hoz] (I-\name) node[below=0.5cm](0,-\name) {$v_\y$};

% Draw the hidden layer nodes
\foreach \name / \y in {1/1,2/2,3/3,6/n}
\path[yshift=0.5cm] node [hidden neuron, hoz] (H-\name) at (\layersep,-\name cm) {\color{red}$c_\y$};

\path[yshift=0.5cm]
   node[hoz] () at (\layersep,-4 cm) {$\dots$};
\path[yshift=0.5cm]
   node[hoz] () at (\layersep,-5 cm) {$\dots$};

\foreach \name / \y in {1/1,2/2,3/3,6/n}
\path[hoz] (H-\name) node[above=0.5cm]  {$h_\y$};  

\path node[hoz,right] at ($(I-5)!0.5!(H-6)$) {\color{red}$w_{nm}$};       

% Connect every node in the input layer with every node in the  hidden layer.
    \foreach \source in {1,2,3,5}
        \foreach \dest in {1,2,3,6}
            \path (I-\source.north) edge (H-\dest.south);
\end{tikzpicture}
% End of code
\end{document}

- - - - - - - - - - - - - 第一版

这是一种可能,正常绘制,完成后分别旋转tikzpicturewithtansform shape选项和label,如下图所示,

在此处输入图片描述

代码

\documentclass{article}

\usepackage{tikz}
\begin{document}
\pagestyle{empty}

\def\layersep{2.5cm}

\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep,transform shape,rotate=90]  %<-- rotate the NN
    \tikzstyle{every pin edge}=[<-,shorten <=1pt]
    \tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
    \tikzstyle{input neuron}=[neuron, fill=green!50];
    \tikzstyle{output neuron}=[neuron, fill=red!50];
    \tikzstyle{hidden neuron}=[neuron, fill=blue!50];
    \tikzstyle{annot} = [text width=4em, text centered]
    \tikzset{hoz/.style={rotate=-90}}   %<--- for labels
    % Draw the input layer nodes
    \foreach \name / \y in {1,...,4}
    % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
        \node[input neuron, pin=left:\rotatebox{-90}{\parbox[t][][r]{8mm}{\centering Input \\\#\y}}] (I-\name) at (0,-\y) {};

    % Draw the hidden layer nodes
    \foreach \name / \y in {1,...,5}
        \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:\rotatebox{-90}{Output}}, right of=H-3] (O) {};

    % Connect every node in the input layer with every node in the
    % hidden layer.
    \foreach \source in {1,...,4}
        \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);

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

答案2

PSTricks 解决方案:

\documentclass{article}

\usepackage{bm}           % for the bold \cdots
\usepackage{pstricks-add} % for the rest of the drawing

\def\pointTop(#1)#2#3{%
  \pscircle[
    linewidth = 1.7\pslinewidth
  ](#1){0.35}
  \rput(#1){\textcolor{red}{#2}}
  \uput[90](#1){#3}}
\def\pointBottom(#1)#2#3{%
  \pscircle[
    linewidth = 1.7\pslinewidth,
    fillstyle = solid,
    fillcolor = gray!50
  ](#1){0.35}
  \rput(#1){\textcolor{red}{#2}}
  \uput[270](#1){#3}}

\begin{document}

\begin{pspicture}(-0.35,-0.69)(5.95,2.77) % found manually
\psset{labelsep = 14pt}
  \pnodes{P}(0,2)(1,2)(2,2)(5.5,2)(3.75,2)(0.5,0)(1.5,0)(2.5,0)(5,0)(3.75,0)
  % nodes and labels
  \multido{\iA = 0+1, \iB = 5+1, \iC = 1+1}{3}{%
    \pointTop(P\iA){$c_{\iC}$}{$h_{\iC}$}
    \pointBottom(P\iB){$b_{\iC}$}{$v_{\iC}$}}
  \rput(P4){$\bm\cdots$}
  \pointTop(P3){$c_{n}$}{$h_{n}$}
  \rput(P9){$\bm\cdots$}
  \pointBottom(P8){$b_{m}$}{$v_{m}$}
  % connections
  \multido{\i = 0+1}{3}{%
    \multido{\r = 0.5+1}{3}{%
      \psline(\i,1.65)(5,0.35)
      \psline(\i,1.65)(\r,0.35)
      \psline(5.5,1.65)(\r,0.35)}}
  \psline(5.5,1.65)(5,0.35)
  \pcline[
    linestyle = none,
    offset = 12pt
  ](P3)(P8)
  \ncput{\textcolor{red}{$w_{nm}$}}
\end{pspicture}

\end{document}

输出

代码较短但(可能)可读性较差:

\documentclass{article}

\usepackage{bm}
\usepackage{pstricks-add}

\def\pointTop(#1)#2#3{%
  \pscircle[
    linewidth = 1.7\pslinewidth
  ](#1){0.35}
  \rput(#1){\textcolor{red}{$c_{#2}$}}
  \uput[90](#1){$h_{#3}$}}
\def\pointBottom(#1)#2#3{%
  \pscircle[
    linewidth = 1.7\pslinewidth,
    fillstyle = solid,
    fillcolor = gray!50
  ](#1){0.35}
  \rput(#1){\textcolor{red}{$b_{#2}$}}
  \uput[270](#1){$v_{#3}$}}

\begin{document}

\begin{pspicture}(-0.35,-0.69)(5.95,2.77)
\psset{labelsep = 14pt}
  \pnodes{P}(0,2)(1,2)(2,2)(5.5,2)(3.75,2)(0.5,0)(1.5,0)(2.5,0)(5,0)(3.75,0)
  \multido{\iA = 0+1, \iB = 5+1, \iC = 1+1}{3}{%
    \multido{\r = 0.5+1}{3}{%
      \psline(\iA,1.65)(5,0.35)
      \psline(\iA,1.65)(\r,0.35)
      \psline(5.5,1.65)(\r,0.35)}
    \pointTop(P\iA){\iC}{\iC}
    \pointBottom(P\iB){\iC}{\iC}}
  \rput(P4){$\bm\cdots$}
  \pointTop(P3){n}{n}
  \rput(P9){$\bm\cdots$}
  \pointBottom(P8){m}{m}
  \psline(5.5,1.65)(5,0.35)
  \pcline[linestyle = none, offset = 12pt](P3)(P8)
  \ncput{\textcolor{red}{$w_{nm}$}}
\end{pspicture}

\end{document}

相关内容