Latex BP算法的伪代码和BP网络

BP算法的伪代码

代码

\documentclass[11pt,UTF8]{ctexart}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{amsmath}
\usepackage{amssymb}%空集符号

\floatname{algorithm}{机器学习算法}
\renewcommand{\algorithmicrequire}{\textbf{输入:}}
\renewcommand{\algorithmicensure}{\textbf{输出:}}
\begin{document}
    \begin{algorithm}
        \caption{误差逆传播算法}
        \begin{algorithmic}[1] %显示行号,1是每行都显示
            \Require 训练集$D=\left\{(\mathbf{x}_k,\mathbf{y}_k)\right\}_{k=1}^{m};$
            学习率$\eta$
            \Ensure 连接权值或阈值确定的多层前馈神经网络

            \Function {$BP$}{$D, \eta$}
            \State$(0,1)$范围内随机初始化网络中的所有连接权值和阈值
              \Repeat  
            \For {$\mathbf{all}\space (\mathbf{x}_k,\mathbf{y}_k)\in D$}
                \State计算当前样本输出$\hat{\mathbf{y}}_k=f(\beta_j-\theta_j)$
                \State计算输出神经元的梯度$$\begin{aligned}
				g_j&=-\dfrac{\partial{E_k}}{\partial{\hat{y}^k}}\cdot\dfrac{\partial{\hat{y}^k}}{\partial{\beta_j}}\\
				&=-(\hat{y}_{j}^{k}-y_{j}^{k})f'(\beta_j-\theta_j)\\
				&=\hat{y}_{j}^{k}(1-\hat{y}_{j}^{k})(\hat{y}_{j}^{k}-y_{j}^{k})
				\end{aligned}$$
                \State计算隐层的神经元梯度项$$\begin{aligned}
				e_h&=-\dfrac{\partial{E_k}}{\partial{b_h}}\cdot\dfrac{\partial{b_h}}{\alpha_h}\\
				&=-\sum_{j=1}^{\ell}\dfrac{\partial{E_k}}{\partial{\beta_j}}\cdot\dfrac{\partial{\beta_j}}{b_h}f'(\alpha_h-\gamma_h)\\
				&=\sum_{j=1}^{\ell}w_{hj}g_{j}f'(\alpha_h-\gamma_h)\\
				&=b_h(1-b_h)\sum_{j=1}^{\ell}w_{hj}g_{j}
				\end{aligned}$$
                \State更新权值
                $$
				\begin{aligned}
					\Delta{w_{hj}}&=\eta{}g_jb_h\\
					\Delta{v_{ih}}&=\eta{}e_hx_i\\
				\end{aligned}
				$$
                \State更新阈值
                $$
				\begin{aligned}
				\Delta{\theta_j}&=-\eta{}g_j\\
				\Delta{\gamma_{h}}&=-\eta{}e_h\\
				\end{aligned}
				$$
            \EndFor
            \Until{达到停止条件} 
            \EndFunction
        \end{algorithmic}
    \end{algorithm}
\end{document}

这里写图片描述

BP网络

代码


\documentclass[UTF8]{ctexart}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\begin{document}
    \thispagestyle{empty}
    % 定义基本形状
    \tikzstyle{results}=[ellipse ,text centered,draw=black]
    \tikzstyle{textareas} =[rectangle, rounded corners,text centered]
    \tikzstyle{inoutput} =[ellipse,draw=black]
    % 箭头形式
    \tikzstyle{arrow} = [-]

    \tikzstyle{arrow1} = [-,draw=red]
    \begin{tikzpicture}
    %输出层
    \node[textareas,font=\small](y1_text){$y_1$};
    \node[textareas,right of=y1_text,font=\small](yj_text){$y_j$};
    \node[textareas,right of=yj_text,font=\small](yl_text){$y_\ell$};

    \node[inoutput,below of=y1_text,yshift=0.5cm](y1){};

    \node[textareas,left of=y1,xshift=-0.5cm,font=\small](outputtext){输出层};
    \node[textareas,right of=y1,xshift=-0.5cm,font=\small](dot11){$\dots$};
    \node[inoutput,below of=yj_text,yshift=0.5cm](yj){};
    \node[textareas,right of=yj,xshift=-0.5cm,font=\small](dot12){$\dots$};
    \node[inoutput,below of=yl_text,yshift=0.5cm](yl){};
    %隐藏层

    \node[inoutput,below of=y1,xshift=-0.5cm,yshift=-0.5cm](b1){};
    \node[textareas,right of=b1,xshift=-0.65cm,yshift=0.1cm,font=\small](b1_text){$b_1$};
    \node[textareas,below of=outputtext,yshift=-0.5cm,font=\small](hidetext){隐层};
    \node[inoutput,below of=y1,xshift=0.5cm,yshift=-0.5cm](b2){};

    \node[textareas,right of=b2,xshift=-0.65cm,yshift=0.1cm,font=\small](b2_text){$b_2$};
    \node[textareas,right of=b2,xshift=-0.35cm,font=\small](dot21){$\dots$};
    \node[inoutput,below of=yj,xshift=0.65cm,yshift=-0.5cm](bh){};
    \node[textareas,right of=bh,xshift=-0.65cm,yshift=0.1cm,font=\small](bh_text){$b_h$};
    \node[textareas,right of=bh,xshift=-0.35cm,font=\small](dot22){$\dots$};
    \node[inoutput,below of=yl,xshift=0.65cm,yshift=-0.5cm](bq){};
    \node[textareas,right of=bq,xshift=-0.65cm,yshift=0.1cm,font=\small](bq_text){$b_q$};
    %输出层
    \node[inoutput,below of=y1,yshift=-2cm](x1){};
    \node[textareas,below of=hidetext,yshift=-0.5cm,font=\small](inputtext){输入层};
    \node[textareas,right of=x1,xshift=-0.5cm,font=\small](dot31){$\dots$};
    \node[inoutput,below of=yj,yshift=-2cm](xj){};
    \node[textareas,right of=xj,xshift=-0.5cm,font=\small](dot32){$\dots$};
    \node[inoutput,below of=yl,yshift=-2cm](xl){};

    \node[textareas,below of=x1,yshift=0.5cm,font=\small](x1_text){$x_1$};
    \node[textareas,below of=xj,yshift=0.5cm,font=\small](xj_text){$x_i$};
    \node[textareas,below of=xl,yshift=0.5cm,font=\small](xl_text){$x_d$};
    %连接形状
    \draw [arrow] (y1) -- node [left,font=\small] {} (b1);
    \draw [arrow] (y1) -- node [left,font=\small] {} (b2);
    \draw [arrow] (y1) -- node [left,font=\small] {} (bh);
    \draw [arrow] (y1) -- node [left,font=\small] {} (bq);

    \draw [arrow] (yl) -- node [left,font=\small] {} (b1);
    \draw [arrow] (yl) -- node [left,font=\small] {} (b2);
    \draw [arrow] (yl) -- node [left,font=\small] {} (bh);
    \draw [arrow] (yl) -- node [left,font=\small] {} (bq);

    \draw [arrow1] (yj) -- node [left,font=\tiny,red] {$w_{1j}$} (b1);
    \draw [arrow1] (yj) -- node [left,font=\tiny,red] {$w_{2j}$} (b2);
    \draw [arrow1] (yj) -- node [right,font=\tiny,red] {$w_{hj}$} (bh);
    \draw [arrow1] (yj) -- node [right,font=\tiny,red] {$w_{qj}$} (bq);

    \draw [arrow] (x1) -- node [left,font=\small] {} (b1);
    \draw [arrow] (xl) -- node [left,font=\small] {} (b1);
    \draw [arrow] (xj) -- node [left,font=\tiny] {} (b1);

    \draw [arrow] (x1) -- node [left,font=\small] {} (b2);
    \draw [arrow] (xl) -- node [left,font=\small] {} (b2);
    \draw [arrow] (xj) -- node [left,font=\tiny] {} (b2);

    \draw [arrow1] (x1) -- node [left,font=\small,red] {$v_{1h}$} (bh);
    \draw [arrow1] (xl) -- node [right,font=\small,red] {$v_{ih}$} (bh);
    \draw [arrow1] (xj) -- node [right,font=\small,red] {$v_{dh}$} (bh);

    \draw [arrow] (xl) -- node [left,font=\small] {} (bq);
    \draw [arrow] (x1) -- node [left,font=\small] {} (bq);
    \draw [arrow] (xj) -- node [right,font=\small] {} (bq);

    \end{tikzpicture}
\end{document}

这里写图片描述

  • 9
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值