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]
\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}
图
![这里写图片描述](https://img-blog.csdn.net/20171226224838121?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdW5jbGVfZ3k=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
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}
图
![这里写图片描述](https://img-blog.csdn.net/20171226224920089?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdW5jbGVfZ3k=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)