使用 TikZ 绘制带有偏差的神经网络

使用 TikZ 绘制带有偏差的神经网络

我知道这里已经有一些关于绘制神经网络的问题,但我正尝试绘制类似这样的内容: 在此处输入图片描述

我尝试应用一些教程并进行了一些修改,但这是我的结果,它与上面的结果仍然相差甚远。 在此处输入图片描述

这是我的代码:

\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\begin{document}
\def\layersep{5cm}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
 \tikzstyle{every pin edge}=[<-,shorten <=1pt]
 \tikzstyle{neuron}=[circle,fill=black!25,minimum size=25pt,inner sep=0pt]
 \tikzstyle{Cauche d'entrée}=[neuron, fill=gray!33];
 \tikzstyle{Cauche cachée}=[neuron, fill=gray!66];
 \tikzstyle{Cauche de sortie}=[neuron, fill=gray!99];
 \tikzstyle{annot} = [text width=6em, text centered]

 % Draw the rectangle
\filldraw[fill=gray!33!white, draw=black] (0,0) rectangle (1.4,6);
 % Draw the input layer nodes
 \foreach \name / \y in {1,2,3,4,5,6}
 % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
 \node[Cauche d'entrée] (I-\name) at (0,-\y) {};

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

 % Draw the output layer node
 \node[Cauche de sortie, pin={[pin edge={->}]right:Prévision}, right of=H-3] (O) {};

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

 % Annotate the layers
 \node[annot,above of=H-1, node distance=1cm] (hl) {Cauche cachée};
 \node[annot,left of=hl] {Cauche d'entrée};
 \node[annot,right of=hl] {Cauche de sortie};
 \end{tikzpicture}
 \end{document}

正如你所见,我离我想要画的东西还很远。任何帮助或建议都会很感激。

答案1

作为起点:

在此处输入图片描述

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                calc, chains,
                decorations.pathreplacing,
                calligraphy,% had to be after decorations.pathreplacing
                positioning}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}

\begin{document}
    \begin{tikzpicture}[
             font = \sffamily,
        shorten > = 1pt,
                > = Stealth,
    node distance = 6mm and 18mm,
      start chain = going below,
 every pin/.style = {pin distance=11mm, pin edge={thin, black, ->}},
    neuron/.style = {circle, draw, fill=#1,
                     minimum size=17pt, inner sep=1pt,
                     },
     annot/.style = {text width=6em, align=center},
        BC/.style = {decorate,
                            decoration={calligraphic brace, amplitude=4pt,
                            pre =moveto, pre  length=1pt,
                            post=moveto, post length=1pt,
                            raise=6mm, mirror},
                            thick,
                            pen colour={red}
                         }
                        ]
% input variables
\foreach \i in {1,...,9}
{
 \ifnum\i=5
    \node[neuron=white,draw=none,on chain]   (in-\i)        {$\vdots$};
 \else
     \ifnum\i<9
        \node[neuron=white,on chain]             (in-\i)    {};
     \else
        \node[neuron=white,draw=none,on chain]   (in-\i)    {$\vdots$};
     \fi
 \fi
}
% Draw the rectangle
\path   let \p1 = ($(in-1.north) - (in-9.south)$),
            \n1 = {veclen(\y1,\x1)} in
        node (r) [minimum height=\n1,
                  minimum width=9mm, draw, fill=gray!10,
                  below right=0mm and 18 mm of in-1.north]
                  {\rotatebox{90}{Variables selection}};
% input layer neurons
\node[neuron=gray!22,right=of in-1 -| r.east] (I-1) {};
\node[neuron=gray!22,right=of in-3 -| r.east] (I-2) {};
\node[neuron=gray!22,right=of in-6 -| r.east] (I-3) {};
\node[neuron=gray!22,right=of in-7 -| r.east] (I-4) {};
% hiden layer neurons
\node[neuron=gray!44,right=of in-2 -| I-1.east] (H-1) {};
\node[neuron=gray!44,right=of in-5 -| I-1.east] (H-2) {};
\node[neuron=gray!44,right=of in-8 -| I-1.east] (H-3) {};
% out layer neuron
\node[neuron=gray!66,right=of H-2,pin=right:Prévision] (out) {};
% bias nodes
\path (I-1) -| (H-1) node (B1) [pos=0.25,neuron=blue!33] {Bias};
\path (I-1 -| H-1) -| (out) node (B2) [pos=0.25,neuron=blue!33] {Bias};
% Annotations of the layers
\node[annot,above=of in-1]                  {pre-input layer};
\node[annot,above=of I-1]                   {Cauche d'entrée};
\node[annot,above=of I-1.north -| H-1]      {Cauche cachée};
\node[annot,above=of I-1.north -| out]    {Cauche de sortie};

% input labels
\draw[BC] (in-1.north) --
    node[left=11mm,align=right] {xxx\\ yyy\\ zzz} (in-5.south) ;
\draw[BC] (in-6.north) --
    node[left=11mm,align=right] {xxx\\ yyy\\ zzz} (in-9.south);
% nodes connections
% input
\foreach \i in {1,...,4,6,7,8}
    {\draw[->]   (in-\i) -- (in-\i -| r.west);}
\foreach \i in {1,3,6,7}
    {\draw[->]   (in-\i) -- (in-\i -| r.west);}
% input layer
\foreach \i in {1,...,4}
    {\draw[->]   (I-\i -| r.east) -- (I-\i);}
% hidden layer
\foreach \i in {1,...,4}
    \foreach \j in {1,...,3}
    \draw[->] (I-\i) edge (H-\j);
% output layer
 \foreach \i in {1,2,3}
    \draw[->] (H-\i) edge (out);
% bias connection.
 \foreach \i in {1,2,3}
    \draw[->,dashed,blue] (B1) edge (H-\i);
    \path[->,dashed,blue] (B2) edge (out);
   \end{tikzpicture}
\end{document}

我尝试重现显示的图像......但图像中可能缺少某些元素,例如输入端的文本等。添加到代码中这应该不是什么大问题:)

请注意,以上姆韦使用最新的tikz语法,而不是过时的语法,因为它在你的姆韦

相关内容