建立一个可工作的神经网络

建立一个可工作的神经网络

我有以下两个 MWE:

\documentclass[border=5pt,tikz]{standalone}
   \begin{document}

        \begin{tikzpicture}

        %%% %%% %%%
        %>> We are building a perceptron
        %%% %%% %%%


            % input vectors
            \xdef\firstinputarray{{1,2}}
            \xdef\secondinputarray{{4,8}}

            % output vector
            \xdef\outputarray{{1,0}} % The 1 means that for \firstinputarray the statement is true, for \secondinputarray is wrong

            % weights
            \xdef\zerow{.5}
            \xdef\firstw{2}
            \xdef\secondw{1.75}

            % bias
            \xdef\bias{1}

            % Heaviside

            \newcommand{\heaviside}[1]{
                \ifnum#1>0
                    \pgfmathsetmacro{\a}{1}
                \fi
                \ifnum#1=0
                    \pgfmathsetmacro{\a}{1}
                \fi
                \ifnum#1<0
                    \pgfmathsetmacro{\a}{0}
                \fi
            }

            % Floor function
            \newcommand{\floor}[1]{
                \pgfmathsetmacro{\floornumber}{int(floor(#1))}
            }

            \newcommand{\forheavi}[1]{
                \pgfmathsetmacro{\forheavinumber}{int(floor(#1))}
            }

            % new weights
            \pgfmathsetmacro{\firstway}{\zerow*\bias+\firstinputarray[0]*\firstw+\firstinputarray[1]*\secondw}

            \forheavi{\firstway}
            \heaviside{\forheavinumber}

            \node (a) {\texttt{firstway}: \pgfmathprintnumber{\firstway}\qquad$\mathrm{H}(\mathtt{firstway})$:\pgfmathprintnumber{\a}};

            % set new weights
            \ifnum\a>0
                \pgfmathsetmacro{\firstw}{.5*(\firstway-0)*\firstinputarray[0]}
            \fi
                \forheavi{\firstway}
                \heaviside{\forheavinumber}
            \node[above left,yshift=1cm] at (a.north east) {new \texttt{firstw}: \pgfmathprintnumber{\firstw}\qquad$\mathrm{H}(\mathtt{new fistw})$: \pgfmathprintnumber{\a}};

    %           \forheavi{0}
    %           \heaviside{\forheavinumber}
    %               \node[red,draw,below=.5cm] at (a) {\pgfmathprintnumber{\a}};

        \end{tikzpicture}

    \end{document}

and

    \documentclass[border=5pt,tikz]{standalone}
    \usetikzlibrary{decorations.pathreplacing}
    \tikzset{
        neuron/.style={
            draw,circle,inner sep=.3cm,fill=white
        },
        brace/.style={
            decorate,decoration={brace,amplitude=.3cm},thick
        }
    }
    \begin{document}
        \begin{tikzpicture}
            \foreach \x in {0,1,...,8}
            {
                \node[neuron] (a\x) at (0,\x) {};
            }
            \foreach \y in {-4,-3,...,12}
            {
                \node[neuron] (b\y) at (8,\y) {};
            }
            \foreach \z in {-2,-1,...,10}
            {
                \node[neuron] (c\z) at (16,\z) {};
                \pgfmathsetmacro{\n}{10-\z}
                    \draw[->] (c\z) --+ (1,0) node[right] {\pgfmathprintnumber{\n}};
            }
            \foreach \x in {0,1,...,8}
            \foreach \y in {-4,-3,...,12}
            \foreach \z in {-2,-1,...,10}
            {
                \draw[->] (a\x) -- (b\y);
                \draw[->] (b\y) -- (c\z);
            }
            \draw[brace] ([xshift=-1cm]a0.south) -- ([xshift=-1cm]a8.north) node[midway,text width=2.5cm,left=.5cm] {
                \begin{tabular}{c}
                    input layer \\
                    (784 neurons)
                \end{tabular}
            };
            \node[above=1cm] at (b12) {
                \begin{tabular}{c}
                    hidden layer \\
                    ($n = 15$ neurons)
                \end{tabular}
            };
            \node[above=1cm] at (c10) {
                \begin{tabular}{c}
                    output layer
                \end{tabular}
            };
        \end{tikzpicture}
    \end{document}

输出如下:

截屏

我有以下问题:

  • 我怎样才能输入输入数组和起始权重,然后第一个代码进行计算(更准确地说:我怎样才能“延长”循环,foreach以便算法在获得正确结果时停止)以及
  • 我怎样才能突出权重的视觉效果,例如我让 TiZ 绘制两个节点(代表神经元),并且越“重要”的键,线(连接两个神经元)就越粗(这一切都是一步一步发生的,这样我们就可以创建一个小动画)?

PS:前馈网络不需要有这么大量的神经元,这只是一个例子。

相关内容