600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 【漫漫科研路\PythonTikz】画神经网络相关图

【漫漫科研路\PythonTikz】画神经网络相关图

时间:2020-03-12 23:57:24

相关推荐

【漫漫科研路\PythonTikz】画神经网络相关图

前一篇文章【图解例说机器学习】神经网络中,由于篇幅有限,我们只介绍了其理论部分。作为补充,本文主要介绍文中所有图像的绘制(使用Tikz或者Python)。对于神经网络中的框架图 (例如神经元,神经网络结构),我们采用Tikz绘制;对于坐标系的函数图像,我们采用Python绘制。注意:Tikz源代码需要在LaTeX中运行,其配置环境可以参考我之前的文章【漫漫科研路\LaTeX】使用Sublime Text3撰写科研论文(Windows系统下)或者【漫漫科研路\LaTeX】使用Vim撰写科研论文(MacOS系统下)。

Tikz/pgfplots 绘制神经网络

图1,2是M-P神经元

图1的tikz源代码为:

\documentclass{article}\usepackage{amsmath}\usepackage{tikz}\usetikzlibrary{shapes, calc, shapes, arrows}\begin{document}\tikzstyle{inputNode}=[draw,circle,minimum size=25pt,inner sep=0pt]\tikzstyle{stateTransition}=[->, thick]\begin{tikzpicture}\node[draw,circle,minimum size=35pt,inner sep=0pt] (x) at (0,0) {$\Sigma$ $f$};\node[inputNode] (x0) at (-3, 2.25) {\color{red}$x^{(0)}$};\node[inputNode] (x1) at (-3, 1.25) {\color{red}$x^{(1)}$};\node[inputNode] (xj) at (-3, -0.5) {\color{red}$x^{(j)}$};\node[inputNode] (xm) at (-3, -2.25) {\color{red}$x^{(M)}$};\draw[stateTransition] (x0) to[out=0,in=120] node [midway, sloped, above=-2] {\color{green!40!black}$\omega_0$} (x);\draw[stateTransition] (x1) to[out=0,in=150] node [midway, sloped, above=-2] {\color{green!40!black}$\omega_1$} (x);\draw[stateTransition] (xj) to[out=0,in=210] node [midway, sloped, above=-2] {\color{green!40!black}$\omega_j$} (x);\draw[stateTransition] (xm) to[out=0,in=240] node [midway, sloped, above=-2] {\color{green!40!black}$\omega_M$} (x);\color{blue}\draw[stateTransition] (x) -- (2,0) node [midway,above=-0.1cm] {y};\draw[dashed] (0,-0.5) -- (0,0.5);\node (dots) at (-3, -1.25) {$\vdots$};\node (dots) at (-3, 0.5) {$\vdots$};\end{tikzpicture}\end{document}

图2的tikz源代码:

\documentclass[crop, tikz]{standalone}\usepackage{amsmath}\usepackage{tikz}\usetikzlibrary{shapes, calc, shapes, arrows}\begin{document}\tikzstyle{inputNode}=[draw,circle,minimum size=25pt,inner sep=0pt]\tikzstyle{stateTransition}=[->, thick]\begin{tikzpicture}\node[draw,circle,minimum size=35pt,inner sep=0pt] (x) at (0,0) {$\Sigma$ $\hspace{1.5mm}sgn$};\node[inputNode] (x0) at (-3, 1.25) {\color{red}$x^{(0)}$};\node[inputNode] (x1) at (-3, 0) {\color{red}$x^{(1)}$};\node[inputNode] (x2) at (-3, -1.25) {\color{red}$x^{(2)}$};\draw[stateTransition] (x0) to node [midway, sloped, above=-2] {\color{green!40!black}$\omega_0$} (x);\draw[stateTransition] (x1) to node [midway, sloped, above=-2] {\color{green!40!black}$\omega_1$} (x);\draw[stateTransition] (x2) to node [midway, sloped, above=-2] {\color{green!40!black}$\omega_2$} (x);\color{blue}\draw[stateTransition] (x) -- (4,0) node [midway,above=-0.1cm] {$y=\color{black}sgn\left(\sum\limits_{j=0}^{2}\color{green!40!black}\omega_j\color{red}x^{(j)}\color{black}\right)$};\draw[dashed] (0,-0.5) -- (0,0.5);\end{tikzpicture}\end{document}

图3和图4是常见的神经网络结构图:

图3的Tikz源代码为:

\documentclass[crop, tikz]{standalone}\usepackage{amsmath}\usepackage{tikz}\usetikzlibrary{shapes, calc, shapes, arrows}\begin{document}\tikzstyle{inputNode}=[draw,circle,minimum size=25pt,inner sep=0pt]\tikzstyle{stateTransition}=[->, thick]\begin{tikzpicture}\node[draw,circle,minimum size=25pt,inner sep=0pt,blue] (x) at (3,0) {$y$};\node[inputNode] (x0) at (-3, 2.25) {\color{red}$x^{(0)}$};\node[inputNode] (x1) at (-3, 0) {\color{red}$x^{(1)}$};\node[inputNode] (x2) at (-3, -2.25) {\color{red}$x^{(2)}$};\node[inputNode] (z0) at (0, 2.25) {\color{green}$z^{(0)}$};\node[inputNode] (z1) at (0, 0) {\color{green}$z^{(1)}$};\node[inputNode] (z2) at (0, -2.25) {\color{green}$z^{(2)}$};\draw[stateTransition] (x0) to node [midway, sloped, above=-2,xshift=-25] {\color{green!40!black}$\omega_{01}^{(1)}$} (z1);\draw[stateTransition] (x1) to node [midway, sloped, above=-2,xshift=-15] {\color{green!40!black}$\omega_{11}^{(1)}$} (z1);\draw[stateTransition] (x2) to node [midway, sloped, above=-2,xshift=-15] {\color{green!40!black}$\omega_{21}^{(1)}$} (z1);\draw[stateTransition] (x0) to node [midway, sloped, above=-2,xshift=-35] {\color{green!40!black}$\omega_{02}^{(1)}$} (z2);\draw[stateTransition] (x1) to node [midway, sloped, above=-2,xshift=-25] {\color{green!40!black}$\omega_{12}^{(1)}$} (z2);\draw[stateTransition] (x2) to node [midway, sloped, above=-2,xshift=-15] {\color{green!40!black}$\omega_{22}^{(1)}$} (z2);\draw[stateTransition] (z0) to node [midway, sloped, above=-2] {\color{green!60!black}$\omega_{0}^{(2)}$} (x);\draw[stateTransition] (z1) to node [midway, sloped, above=-2] {\color{green!60!black}$\omega_{1}^{(2)}$} (x);\draw[stateTransition] (z2) to node [midway, sloped, above=-2] {\color{green!60!black}$\omega_{2}^{(2)}$} (x);\color{blue}\draw[stateTransition] (x) -- (8,0) node [midway,above=1cm] {$\color{green}z^{(j)}\color{black}=\color{black}sgn\left(\sum\limits_{i=0}^{2}\color{green!40!black}\omega_{ij}^{(1)}\color{red}x^{(i)}\color{black}\right)$};\draw[stateTransition] (x) -- (8,0) node [midway,above=0.5cm] {$\color{red}x^{(0)}\color{black}=\color{green}z^{(0)}=1$};\draw[stateTransition] (x) -- (8,0) node [midway,below=1cm] {$\color{blue}y\color{black}=\color{black}sgn\left(\sum\limits_{j=0}^{2}\color{green!60!black}\omega_{i}^{(2)}\color{green}z^{(j)}\color{black}\right)$};\end{tikzpicture}\end{document}

图4的Tikz源代码为:

\documentclass[crop, tikz]{standalone}\usepackage{amsmath}\usepackage{tikz}\usetikzlibrary{shapes, calc, shapes, arrows}\begin{document}\tikzstyle{inputNode}=[draw,circle,minimum size=25pt,inner sep=0pt]\tikzstyle{inputNode1}=[draw,circle,minimum size=35pt,inner sep=0pt]\tikzstyle{stateTransition}=[->, thick]\begin{tikzpicture}\node[inputNode] (x0) at (-3, 3.4) {\color{red}$x^{(0)}$};\node[inputNode] (x1) at (-3, 1.75) {\color{red}$x^{(1)}$};\node (dots) at (-3, 0.75) {$\vdots$};\node[inputNode] (xi) at (-3, -0.75) {\color{red}$x^{(i)}$};\node (dots) at (-3, -1.75) {$\vdots$};\node[inputNode] (xI) at (-3, -3.4) {\color{red}$x^{(I)}$};\node[inputNode1] (z0) at (0, 3.4) {\color{green}$z^{(0)}$};\node[inputNode1] (z1) at (0, 1.75) {\color{green}$\alpha^{(1)} z^{(1)}$};\draw[dashed] (0,1.75-0.5) -- (0,1.75+0.5);\node (dots) at (0, 0.75) {$\vdots$};\node[inputNode1] (zj) at (0, -0.75) {\color{green}$\alpha^{(j)} z^{(j)}$};\draw[dashed] (0,-0.75-0.5) -- (0,-0.75+0.5);\node (dots) at (0, -1.75) {$\vdots$};\node[inputNode1] (zJ) at (0, -3.4) {\color{green}$\alpha^{(J)} z^{(J)}$};\draw[dashed] (0,-3.2-0.5) -- (0,-3.2+0.5);\node[inputNode1] (y1) at (3, 1.75) {\color{blue}$\beta^{(1)} \hat y^{(1)}$};\draw[dashed] (3,1.75-0.5) -- (3,1.75+0.5);\node (dots) at (3, 0.75) {$\vdots$};\node[inputNode1] (yk) at (3, -0.75) {\color{blue}$\beta^{(k)} \hat y^{(k)}$};\draw[dashed] (3,-0.75-0.5) -- (3,-0.75+0.5);\node (dots) at (3, -1.75) {$\vdots$};\node[inputNode1] (yK) at (3, -3.4) {\color{blue}$\beta^{(K)} \hat y^{(K)}$};\draw[dashed] (3,-3.4-0.5) -- (3,-3.4+0.5);\draw[stateTransition,opacity=0.2] (x0) to node [midway, sloped, above=-2,xshift=-25] {} (z1);\draw[stateTransition,opacity=0.2] (x1) to node [midway, sloped, above=-2,xshift=-15] {} (z1);\draw[stateTransition,opacity=0.2] (xi) to node [midway, sloped, above=-2,xshift=-15] {} (z1);\draw[stateTransition,opacity=0.2] (xI) to node [midway, sloped, above=-2,xshift=-15] {} (z1);\draw[stateTransition] (x0) to node [midway, sloped, above=-2,xshift=-35] {\color{red!60!green}$\omega_{0j}^{(1)}$} (zj);\draw[stateTransition] (x1) to node [midway, sloped, above=-2,xshift=-25] {\color{red!60!green}$\omega_{1j}^{(1)}$} (zj);\draw[stateTransition] (xi) to node [midway, sloped, above=-2,xshift=-15] {\color{red!60!green}$\omega_{ij}^{(1)}$} (zj);\draw[stateTransition] (xI) to node [midway, sloped, above=-2,xshift=-15] {\color{red!60!green}$\omega_{Ij}^{(1)}$} (zj);\draw[stateTransition,opacity=0.2] (x0) to node [midway, sloped, above=-2,xshift=-35] {} (zJ);\draw[stateTransition,opacity=0.2] (x1) to node [midway, sloped, above=-2,xshift=-25] {} (zJ);\draw[stateTransition,opacity=0.2] (xi) to node [midway, sloped, above=-2,xshift=-15] {} (zJ);\draw[stateTransition,opacity=0.2] (xI) to node [midway, sloped, above=-2,xshift=-15] {} (zJ);%-----------%\draw[stateTransition,opacity=0.2] (z0) to node [midway, sloped, above=-2,xshift=-25] {} (y1);\draw[stateTransition,opacity=0.2] (z1) to node [midway, sloped, above=-2,xshift=-15] {} (y1);\draw[stateTransition,opacity=0.2] (zj) to node [midway, sloped, above=-2,xshift=-15] {} (y1);\draw[stateTransition,opacity=0.2] (zJ) to node [midway, sloped, above=-2,xshift=-15] {} (y1);\draw[stateTransition] (z0) to node [midway, sloped, above=-2,xshift=-35] {\color{green!60!blue}$\omega_{0k}^{(2)}$} (yk);\draw[stateTransition] (z1) to node [midway, sloped, above=-2,xshift=-25] {\color{green!60!blue}$\omega_{1k}^{(2)}$} (yk);\draw[stateTransition] (zj) to node [midway, sloped, above=-2,xshift=-15] {\color{green!60!blue}$\omega_{jk}^{(2)}$} (yk);\draw[stateTransition] (zJ) to node [midway, sloped, above=-2,xshift=-15] {\color{green!60!blue}$\omega_{Jk}^{(2)}$} (yk);\draw[stateTransition,opacity=0.2] (z0) to node [midway, sloped, above=-2,xshift=-35] {} (yK);\draw[stateTransition,opacity=0.2] (z1) to node [midway, sloped, above=-2,xshift=-25] {} (yK);\draw[stateTransition,opacity=0.2] (zj) to node [midway, sloped, above=-2,xshift=-15] {} (yK);\draw[stateTransition,opacity=0.2] (zJ) to node [midway, sloped, above=-2,xshift=-15] {} (yK);\color{blue}\draw[stateTransition] (yk) node [midway,above=2cm,xshift=6cm] {$\color{green}\alpha^{(j)}\color{black}=\color{black}\sum\limits_{i=0}^{I}\color{red!60!green}\omega_{ij}^{(1)}\color{red}x^{(i)}\color{black}$};\draw[stateTransition] (yk) node [midway,above=1cm,xshift=6cm] {$\color{green}z^{(j)}\color{black}=\color{black}\sigma\left(\color{green}\alpha^{(j)}\color{black}\right)$};\draw[stateTransition] (yk) node [midway,above=0cm,xshift=6cm] {$\color{red}x^{(0)}\color{black}=\color{green}z^{(0)}\color{black}=1$};\draw[stateTransition] (yk) node [midway,below=0.25cm,xshift=6cm] {$\color{blue}\beta^{(k)}\color{black}=\sum\limits_{j=0}^{J}\color{green!60!blue}\omega_{jk}^{(2)}\color{green}z^{(j)}\color{black}$};\draw[stateTransition] (yk) node [midway,below=1.35cm,xshift=6cm] {$\color{blue}\hat y^{(k)}\color{black}=\color{black}\sigma\left(\color{blue}\beta^{(k)}\color{black}\right)$};\end{tikzpicture}\end{document}

图5为多层神经网络与M-P神经元的关系图:

图5对应的Tikz源代码为:

\documentclass[crop, tikz]{standalone}\usepackage{amsmath}\usepackage{tikz}\usetikzlibrary{shapes, calc, shapes, arrows}\begin{document}\tikzstyle{inputNode}=[draw,circle,minimum size=25pt,inner sep=0pt]\tikzstyle{inputNode1}=[draw,circle,minimum size=35pt,inner sep=0pt]\tikzstyle{stateTransition}=[->, thick]\begin{tikzpicture}\node[inputNode] (x0) at (-3, 3.4) {\color{red}$x^{(0)}$};\node[inputNode] (x1) at (-3, 1.75) {\color{red}$x^{(1)}$};\node (dots) at (-3, 0.75) {$\vdots$};\node[inputNode] (xi) at (-3, -0.75) {\color{red}$x^{(i)}$};\node (dots) at (-3, -1.75) {$\vdots$};\node[inputNode] (xI) at (-3, -3.4) {\color{red}$x^{(I)}$};\node[inputNode1] (z0) at (0, 3.4) {\color{green}$z^{(0)}$};\node[inputNode1] (z1) at (0, 1.75) {\color{green}$\alpha^{(1)} z^{(1)}$};\draw[dashed] (0,1.75-0.5) -- (0,1.75+0.5);\node (dots) at (0, 0.75) {$\vdots$};\node[inputNode1] (zj) at (0, -0.75) {\color{green}$\alpha^{(j)} z^{(j)}$};\draw[dashed] (0,-0.75-0.5) -- (0,-0.75+0.5);\node (dots) at (0, -1.75) {$\vdots$};\node[inputNode1] (zJ) at (0, -3.4) {\color{green}$\alpha^{(J)} z^{(J)}$};\draw[dashed] (0,-3.2-0.5) -- (0,-3.2+0.5);\node[inputNode1] (y1) at (3, 1.75) {\color{blue}$\beta^{(1)} \hat y^{(1)}$};\draw[dashed] (3,1.75-0.5) -- (3,1.75+0.5);\node (dots) at (3, 0.75) {$\vdots$};\node[inputNode1] (yk) at (3, -0.75) {\color{blue}$\beta^{(k)} \hat y^{(k)}$};\draw[dashed] (3,-0.75-0.5) -- (3,-0.75+0.5);\node (dots) at (3, -1.75) {$\vdots$};\node[inputNode1] (yK) at (3, -3.4) {\color{blue}$\beta^{(K)} \hat y^{(K)}$};\draw[dashed] (3,-3.4-0.5) -- (3,-3.4+0.5);\draw[stateTransition,opacity=0.2] (x0) to node [midway, sloped, above=-2,xshift=-25] {} (z1);\draw[stateTransition,opacity=0.2] (x1) to node [midway, sloped, above=-2,xshift=-15] {} (z1);\draw[stateTransition,opacity=0.2] (xi) to node [midway, sloped, above=-2,xshift=-15] {} (z1);\draw[stateTransition,opacity=0.2] (xI) to node [midway, sloped, above=-2,xshift=-15] {} (z1);\draw[stateTransition] (x0) to node [midway, sloped, above=-2,xshift=-35] {\color{red!60!green}$\omega_{0j}^{(1)}$} (zj);\draw[stateTransition] (x1) to node [midway, sloped, above=-2,xshift=-25] {\color{red!60!green}$\omega_{1j}^{(1)}$} (zj);\draw[stateTransition] (xi) to node [midway, sloped, above=-2,xshift=-15] {\color{red!60!green}$\omega_{ij}^{(1)}$} (zj);\draw[stateTransition] (xI) to node [midway, sloped, above=-2,xshift=-15] {\color{red!60!green}$\omega_{Ij}^{(1)}$} (zj);\draw[stateTransition,opacity=0.2] (x0) to node [midway, sloped, above=-2,xshift=-35] {} (zJ);\draw[stateTransition,opacity=0.2] (x1) to node [midway, sloped, above=-2,xshift=-25] {} (zJ);\draw[stateTransition,opacity=0.2] (xi) to node [midway, sloped, above=-2,xshift=-15] {} (zJ);\draw[stateTransition,opacity=0.2] (xI) to node [midway, sloped, above=-2,xshift=-15] {} (zJ);%-----------%\draw[stateTransition,opacity=0.2] (z0) to node [midway, sloped, above=-2,xshift=-25] {} (y1);\draw[stateTransition,opacity=0.2] (z1) to node [midway, sloped, above=-2,xshift=-15] {} (y1);\draw[stateTransition,opacity=0.2] (zj) to node [midway, sloped, above=-2,xshift=-15] {} (y1);\draw[stateTransition,opacity=0.2] (zJ) to node [midway, sloped, above=-2,xshift=-15] {} (y1);\draw[stateTransition] (z0) to node [midway, sloped, above=-2,xshift=-35] {\color{green!60!blue}$\omega_{0k}^{(2)}$} (yk);\draw[stateTransition] (z1) to node [midway, sloped, above=-2,xshift=-25] {\color{green!60!blue}$\omega_{1k}^{(2)}$} (yk);\draw[stateTransition] (zj) to node [midway, sloped, above=-2,xshift=-15] {\color{green!60!blue}$\omega_{jk}^{(2)}$} (yk);\draw[stateTransition] (zJ) to node [midway, sloped, above=-2,xshift=-15] {\color{green!60!blue}$\omega_{Jk}^{(2)}$} (yk);\draw[stateTransition,opacity=0.2] (z0) to node [midway, sloped, above=-2,xshift=-35] {} (yK);\draw[stateTransition,opacity=0.2] (z1) to node [midway, sloped, above=-2,xshift=-25] {} (yK);\draw[stateTransition,opacity=0.2] (zj) to node [midway, sloped, above=-2,xshift=-15] {} (yK);\draw[stateTransition,opacity=0.2] (zJ) to node [midway, sloped, above=-2,xshift=-15] {} (yK);\color{blue}\draw[stateTransition] (yk) node [midway,above=2cm,xshift=6cm] {$\color{green}\alpha^{(j)}\color{black}=\color{black}\sum\limits_{i=0}^{I}\color{red!60!green}\omega_{ij}^{(1)}\color{red}x^{(i)}\color{black}$};\draw[stateTransition] (yk) node [midway,above=1cm,xshift=6cm] {$\color{green}z^{(j)}\color{black}=\color{black}\sigma\left(\color{green}\alpha^{(j)}\color{black}\right)$};\draw[stateTransition] (yk) node [midway,above=0cm,xshift=6cm] {$\color{red}x^{(0)}\color{black}=\color{green}z^{(0)}\color{black}=1$};\draw[stateTransition] (yk) node [midway,below=0.25cm,xshift=6cm] {$\color{blue}\beta^{(k)}\color{black}=\sum\limits_{j=0}^{J}\color{green!60!blue}\omega_{jk}^{(2)}\color{green}z^{(j)}\color{black}$};\draw[stateTransition] (yk) node [midway,below=1.35cm,xshift=6cm] {$\color{blue}\hat y^{(k)}\color{black}=\color{black}\sigma\left(\color{blue}\beta^{(k)}\color{black}\right)$};\end{tikzpicture}\end{document}

Python绘制函数图像

Python作图主要使用matplotlib,作图其实比较简单。这里主要是介绍如何作带箭头的坐标系。具体如下:

图6-7是机器学习中常用的激活函数:

图6所对应的Python源代码:

# -*- encoding: utf-8 -*-"""@File : Neural_Networks_001.py@Time : /6/8 15:06@Author : tengweitw@Email : tengweitw@"""import mpl_toolkits.axisartist as axisartistimport numpy as npimport matplotlib.pyplot as plt# Set the format of labelsdef LabelFormat(plt):ax = plt.gca()plt.tick_params(labelsize=14)labels = ax.get_xticklabels() + ax.get_yticklabels()[label.set_fontname('Times New Roman') for label in labels]font = {'family': 'Times New Roman','weight': 'normal','size': 16,}return font#Set the figure window squarefig = plt.figure(figsize=(6, 6))ax = axisartist.Subplot(fig, 111)fig.add_axes(ax)ax.axis[:].set_visible(False)font = LabelFormat(plt)ax.axis["x"] = ax.new_floating_axis(0,0)#Set properties of X axis and add arrowax.axis["x"].major_ticklabels.set_fontsize(14)ax.axis["x"].major_ticklabels.set_fontname('Times New Roman')ax.axis["x"].set_axisline_style("->", size = 1.0)ax.axis["x"].label.set_fontsize(16)#Set properties of Y axis and add arrowax.axis["y"] = ax.new_floating_axis(1,0)ax.axis["y"].major_ticklabels.set_fontsize(14)ax.axis["y"].major_ticklabels.set_fontname('Times New Roman')ax.axis["y"].set_axisline_style("->", size = 1.0)ax.axis["y"].label.set_fontsize(16)#Set the ticks' locationsax.axis["x"].set_axis_direction("bottom")ax.axis["y"].set_axis_direction("left")#Create Datasx=np.linspace(-10,10,1000)y_sigmoid=1/(1+np.exp(-x))y_sgn=np.zeros((len(x),1))for i in range(len(x)):if x[i]>=0:y_sgn[i]=1else:y_sgn[i]=0# Plot figuresplt.plot(x,y_sigmoid,linewidth=3)plt.plot(x,y_sgn,linewidth=3)plt.xlim(-10,10)plt.ylim(-0.2,1.3)plt.legend([r"Sigmoid(x)=$\frac{1}{1+\exp(-x)}$", r"$Sgn(x)=0,x<0; Sgn(x)=1, otherwise$"],loc="upper left")plt.show()

图7对应的Python源代码:

# -*- encoding: utf-8 -*-"""@File : Neural_Networks_002.py@Time : /6/8 19:29@Author : tengweitw@Email : tengweitw@"""import mpl_toolkits.axisartist as axisartistimport numpy as npimport matplotlib.pyplot as plt# Set the format of labelsdef LabelFormat(plt):ax = plt.gca()plt.tick_params(labelsize=14)labels = ax.get_xticklabels() + ax.get_yticklabels()[label.set_fontname('Times New Roman') for label in labels]font = {'family': 'Times New Roman','weight': 'normal','size': 16,}return font#Set the figure window squarefig = plt.figure(figsize=(6, 6))ax = axisartist.Subplot(fig, 111)fig.add_axes(ax)ax.axis[:].set_visible(False)font = LabelFormat(plt)ax.axis["x"] = ax.new_floating_axis(0,0)#Set properties of X axis and add arrowax.axis["x"].major_ticklabels.set_fontsize(14)ax.axis["x"].major_ticklabels.set_fontname('Times New Roman')ax.axis["x"].set_axisline_style("->", size = 1.0)ax.axis["x"].label.set_fontsize(16)#Set properties of Y axis and add arrowax.axis["y"] = ax.new_floating_axis(1,0)ax.axis["y"].major_ticklabels.set_fontsize(14)ax.axis["y"].major_ticklabels.set_fontname('Times New Roman')ax.axis["y"].set_axisline_style("->", size = 1.0)ax.axis["y"].label.set_fontsize(16)#Set the ticks' locationsax.axis["x"].set_axis_direction("bottom")ax.axis["y"].set_axis_direction("left")#Create Datasx=np.linspace(-10,10,1000)y_tanh=(np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))y_relu=np.zeros((len(x),1))for i in range(len(x)):if x[i]>=0:y_relu[i]=x[i]else:y_relu[i]=0# Plot figuresplt.plot(x,y_tanh,linewidth=3)plt.plot(x,y_relu,linewidth=3)plt.xlim(-10,10)plt.ylim(-1.3,1.3)plt.legend([r"Tanh(x)=$\frac{\exp(x)-\exp(-x)}{\exp(x)+\exp(-x)}$", r"$Relu(x)=\max\{0,x\}$"],loc="upper left")plt.show()

图8-9是异或问题的二分类:

图8对应的Python源代码:

# -*- encoding: utf-8 -*-"""@File : Neural_Networks_003.py@Time : /6/8 19:44@Author : tengweitw@Email : tengweitw@"""import mpl_toolkits.axisartist as axisartistimport numpy as npimport matplotlib.pyplot as plt# Set the format of labelsdef LabelFormat(plt):ax = plt.gca()plt.tick_params(labelsize=14)labels = ax.get_xticklabels() + ax.get_yticklabels()[label.set_fontname('Times New Roman') for label in labels]font = {'family': 'Times New Roman','weight': 'normal','size': 16,}return font#Set the figure window squarefig = plt.figure(figsize=(6, 6))ax = axisartist.Subplot(fig, 111)fig.add_axes(ax)ax.axis[:].set_visible(False)font = LabelFormat(plt)ax.axis["x"] = ax.new_floating_axis(0,0)#Set properties of X axis and add arrowax.axis["x"].major_ticklabels.set_fontsize(14)ax.axis["x"].major_ticklabels.set_fontname('Times New Roman')ax.axis["x"].set_axisline_style("->", size = 1.0)# ax.axis["x"].label.set_text('$x^{(1)}$')ax.axis["x"].label.set_fontsize(16)#Set properties of Y axis and add arrowax.axis["y"] = ax.new_floating_axis(1,0)ax.axis["y"].major_ticklabels.set_fontsize(14)ax.axis["y"].major_ticklabels.set_fontname('Times New Roman')ax.axis["y"].set_axisline_style("->", size = 1.0)# ax.axis["y"].label.set_text('$x^{(2)}$')ax.axis["y"].label.set_fontsize(16)#Set the ticks' locationsax.axis["x"].set_axis_direction("bottom")ax.axis["y"].set_axis_direction("left")#Create Datax1=0y1=1x2=1y2=0x3=0y3=0x4=1y4=1x11=np.linspace(-0.2,0.7,100)y11=0.5-x11x22=np.linspace(-0.2,1.7,100)y22=1.5-x22x33=np.linspace(-0.2,1.7,100)for i in range(len(x22)):y33=np.linspace(0.5-x33[i],1.5-x33[i],100)y33[y33<=-0.2]=np.infplt.plot(x33[i]*np.ones((len(y33),1)),y33,'g+',alpha=0.2)# Plot figuresplt.plot(x1,y1,'rs')plt.text(x1+0.05,y1+0.1,r'$\mathrm{x}_1$',fontsize=20)plt.plot(x2,y2,'rs')plt.text(x2+0.05,y2+0.1,r'$\mathrm{x}_2$',fontsize=20)plt.plot(x3,y3,'bo')plt.text(x3+0.05,y3+0.1,r'$\mathrm{x}_3$',fontsize=20)plt.plot(x4,y4,'bo')plt.text(x4+0.05,y4+0.1,r'$\mathrm{x}_4$',fontsize=20)plt.plot(x11,y11,'g')plt.plot(x22,y22,'g')plt.text(-0.2,1.8,r'$x^{(2)}$',fontsize=16,rotation=90,color='k')plt.text(1.8,-0.2,r'$x^{(1)}$',fontsize=16,rotation=0,color='k')plt.xlim(-0.5,2)plt.ylim(-0.5,2)plt.show()

图9对应的Python源代码:

# -*- encoding: utf-8 -*-"""@File : Neural_Networks_004.py@Time : /6/8 20:11@Author : tengweitw@Email : tengweitw@"""import mpl_toolkits.axisartist as axisartistimport numpy as npimport matplotlib.pyplot as plt# Set the format of labelsdef LabelFormat(plt):ax = plt.gca()plt.tick_params(labelsize=14)labels = ax.get_xticklabels() + ax.get_yticklabels()[label.set_fontname('Times New Roman') for label in labels]font = {'family': 'Times New Roman','weight': 'normal','size': 16,}return font# Set the figure window squarefig = plt.figure(figsize=(6, 6))ax = axisartist.Subplot(fig, 111)fig.add_axes(ax)ax.axis[:].set_visible(False)font = LabelFormat(plt)ax.axis["x"] = ax.new_floating_axis(0, 0)# Set properties of X axis and add arrowax.axis["x"].major_ticklabels.set_fontsize(14)ax.axis["x"].major_ticklabels.set_fontname('Times New Roman')ax.axis["x"].set_axisline_style("->", size=1.0)# ax.axis["x"].label.set_text('$x^{(1)}$')ax.axis["x"].label.set_fontsize(16)# Set properties of Y axis and add arrowax.axis["y"] = ax.new_floating_axis(1, 0)ax.axis["y"].major_ticklabels.set_fontsize(14)ax.axis["y"].major_ticklabels.set_fontname('Times New Roman')ax.axis["y"].set_axisline_style("->", size=1.0)# ax.axis["y"].label.set_text('$x^{(2)}$')ax.axis["y"].label.set_fontsize(16)# Set the ticks' locationsax.axis["x"].set_axis_direction("bottom")ax.axis["y"].set_axis_direction("left")# Create Datax1 = 0y1 = 1x2 = 1y2 = 0x3 = 0y3 = 0x4 = 1y4 = 1x11 = np.linspace(-0.2, 0.7, 100)y11 = 0.5 - x11# Plot figuresplt.plot(x1, y1, 'rs')plt.text(x1 + 0.02, y1 + 0.05, r'$\mathrm{z}_1$', fontsize=20)plt.plot(x2, y2, 'rs')plt.text(x2 + 0.0, y2 + 0.05, r'$\mathrm{z}_2$', fontsize=20)plt.plot(x3, y3, 'bo')plt.text(x3 + 0.05, y3 + 0.1, r'$\mathrm{z}_3,\mathrm{z}_4$', fontsize=20)plt.plot(x11, y11, 'g')plt.text(-0.2, 1.1, r'$z^{(2)}$', fontsize=16, rotation=90, color='k')plt.text(1.1, -0.2, r'$z^{(1)}$', fontsize=16, rotation=0, color='k')plt.xlim(-0.2, 1.25)plt.ylim(-0.2, 1.25)plt.show()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。