<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="https://wyundi.github.io/atom.xml" rel="self" type="application/atom+xml" /><link href="https://wyundi.github.io/" rel="alternate" type="text/html" /><updated>2020-11-10T06:26:19+00:00</updated><id>https://wyundi.github.io/atom.xml</id><title type="html">Wyundi’s Blog</title><subtitle>Wyundi's Blog</subtitle><author><name>Wyundi</name></author><entry><title type="html">Ubuntu环境配置</title><link href="https://wyundi.github.io/2020/07/19/Ubuntu%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE/" rel="alternate" type="text/html" title="Ubuntu环境配置" /><published>2020-07-19T18:38:00+00:00</published><updated>2020-07-19T18:38:00+00:00</updated><id>https://wyundi.github.io/2020/07/19/Ubuntu%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE</id><content type="html" xml:base="https://wyundi.github.io/2020/07/19/Ubuntu%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE/">&lt;p&gt;Ubuntu 环境配置相关问题&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;目录：&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Ubuntu ibus输入法无法按数字键取词问题&lt;/li&gt;
  &lt;li&gt;GitHub 添加ssh密钥&lt;/li&gt;
  &lt;li&gt;Anaconda配置&lt;/li&gt;
  &lt;li&gt;GRUB2引导顺序调整&lt;/li&gt;
  &lt;li&gt;gcc版本降级&lt;/li&gt;
  &lt;li&gt;GPG error: The following signatures couldn’t be verified because the public key is not available.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;ubuntu-ibus输入法无法按数字键取词问题&quot;&gt;Ubuntu ibus输入法无法按数字键取词问题&lt;/h3&gt;

&lt;p&gt;参考：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/chen_minghui/article/details/80690821&quot;&gt;ubuntu自带输入法ibus 无法按数字键取词&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;操作系统：Ubuntu 18.04&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;rm -rf ~/.cache/ibus/libpinyin&lt;/strong&gt; 
有个人习惯词先备份user_bigram.db 
重启 ibus restart&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;github-添加ssh密钥&quot;&gt;GitHub 添加ssh密钥&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;生成ssh key&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ssh-keygen &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; rsa &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;you@example.com&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;密钥存储位置: ~/.ssh/id_rsa.pub&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;GitHub上添加密钥&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;anaconda配置&quot;&gt;Anaconda配置&lt;/h3&gt;

&lt;h5 id=&quot;安装&quot;&gt;安装&lt;/h5&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;bash Anaconda3-2020.02-Linux-x86_64.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;anaconda自动进入虚拟环境base&quot;&gt;anaconda自动进入虚拟环境base&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;解决方法:&lt;/p&gt;

    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;conda config &lt;span class=&quot;nt&quot;&gt;--set&lt;/span&gt; auto_activate_base &lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;创建虚拟环境失败代理问题&quot;&gt;创建虚拟环境失败（代理问题）：&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;解决方法;&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;向 ~/.bashrc 中添加：&lt;/p&gt;

        &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;all_proxy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;socks5://127.0.0.1:1080&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;创建虚拟环境&quot;&gt;创建虚拟环境&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;注意要写明python版本，否则虚拟环境中没有python&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;conda create &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; &amp;lt;env-name&amp;gt; &lt;span class=&quot;nv&quot;&gt;python&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;3.7
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;conda安装opencv&quot;&gt;conda安装opencv&lt;/h5&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;conda &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; menpo opencv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;或者&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ conda install py-opencv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;虚拟环境中调用opencv-python报错error--2unspecified-error-the-function-is-not-implemented&quot;&gt;虚拟环境中调用opencv-python报错：error: (-2:Unspecified error) The function is not implemented.&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;conda uninstall py-opencv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;PYPI官网下载适当版本的opencv，此处为&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;opencv_python-4.3.0.36-cp37-cp37m-manylinux2014_x86_64.whl&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pip3 &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;opencv_python-4.3.0.36-cp37-cp37m-manylinux2014_x86_64.whl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;虚拟环境中使用-pip3-install-报错missing-dependencies-for-socks-support&quot;&gt;虚拟环境中使用 pip3 install 报错：Missing dependencies for SOCKS support&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;conda &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;pysocks
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;grub2引导顺序调整&quot;&gt;GRUB2引导顺序调整&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;修改 /etc/default/grub 中的 GRUB_DEFAULT&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;使用以下命令更新菜单&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;update-grub2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;gcc版本降级&quot;&gt;gcc版本降级&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Ubuntu20.04自带gcc版本为gcc9，但CUDA编程需要gcc7环境，故将其降级&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;安装gcc7&lt;/p&gt;

    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;gcc-7 g++-7
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;进行版本切换（改变优先级）&lt;/p&gt;

    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;update-alternatives &lt;span class=&quot;nt&quot;&gt;--install&lt;/span&gt; /usr/bin/gcc gcc /usr/bin/gcc-7 100
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;update-alternatives &lt;span class=&quot;nt&quot;&gt;--install&lt;/span&gt; /usr/bin/gcc gcc /usr/bin/gcc-9 50
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;查看优先级&lt;/p&gt;

    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;update-alternatives &lt;span class=&quot;nt&quot;&gt;--config&lt;/span&gt; gcc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;查看当前版本&lt;/p&gt;

    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gcc &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;gpg-error-the-following-signatures-couldnt-be-verified-because-the-public-key-is-not-available&quot;&gt;GPG error: The following signatures couldn’t be verified because the public key is not available.&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;sudo apt update 出现该问题&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;解决方法：&lt;/p&gt;

    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;gpg &lt;span class=&quot;nt&quot;&gt;--keyserver&lt;/span&gt; keyserver.ubuntu.com &lt;span class=&quot;nt&quot;&gt;--recv&lt;/span&gt; &amp;lt;your public key&amp;gt;					&lt;span class=&quot;c&quot;&gt;#plic key 根据错误提示写&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;gpg &lt;span class=&quot;nt&quot;&gt;--export&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--armor&lt;/span&gt; &amp;lt;your public key&amp;gt; | &lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-key add -
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;</content><author><name>Wyundi</name></author><category term="Ubuntu" /><summary type="html">Ubuntu 环境配置相关问题</summary></entry><entry><title type="html">Tensorflow &amp;amp; CUDA</title><link href="https://wyundi.github.io/2020/06/28/TensorFlow/" rel="alternate" type="text/html" title="Tensorflow &amp;amp; CUDA" /><published>2020-06-28T14:35:00+00:00</published><updated>2020-06-28T14:35:00+00:00</updated><id>https://wyundi.github.io/2020/06/28/TensorFlow</id><content type="html" xml:base="https://wyundi.github.io/2020/06/28/TensorFlow/">&lt;p&gt;pip环境和conda环境中TensorFlow及对应版本的CUDA和cuDNN的安装&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;环境：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;操作系统：Ubunty 20.04&lt;/p&gt;

&lt;p&gt;GPU：RTX2080Ti x2&lt;/p&gt;

&lt;p&gt;python：3.7&lt;/p&gt;

&lt;h3 id=&quot;pip环境&quot;&gt;pip环境&lt;/h3&gt;

&lt;h5 id=&quot;cuda&quot;&gt;CUDA:&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;在官网下载cuda_10.0.130_410.48_linux.run&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;在ubuntu20.04中，系统自带gcc版本高于CUDA要求的版本，安装时跳过版本检查即可&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;bash cuda_10.0.130_410.48_linux.run &lt;span class=&quot;nt&quot;&gt;--override&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;安装完成以后添加CUDA到环境变量&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;向 ~/.bashrc 中添加：
	export  PATH=/usr/local/cuda-10.0/bin:$PATH
	export  LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64$LD_LIBRARY_PATH
    
然后在终端： source ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;nvcc -V 可以查看CUDA版本，检查是否正确安装&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;cudnn&quot;&gt;cuDNN:&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;解压到当前目录&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;tar&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-xzvf&lt;/span&gt; cudnn-10.0-linux-x64-v7.6.5.32.tgz
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;复制cuDNN头文件&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo cp &lt;/span&gt;cuda/include/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; /usr/local/cuda-10.0/include/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;复制cuDNN库&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo cp &lt;/span&gt;cuda/lib64/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; /usr/local/cuda-10.0/lib64/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;添加可执行权限&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo chmod&lt;/span&gt; +x /usr/local/cuda-10.0/include/cudnn.h
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo chmod&lt;/span&gt; +x /usr/local/cuda-10.0/lib64/libcudnn&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;校验是否安装成功&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat&lt;/span&gt; /usr/local/cuda/include/cudnn.h | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;CUDNN_MAJOR &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;成功则会出现如下信息：&lt;/p&gt;

        &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#define CUDNN_MAJOR 7&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define CUDNN_MINOR 6&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define CUDNN_PATCHLEVEL 5&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)&lt;/span&gt;
    
&lt;span class=&quot;c&quot;&gt;#include &quot;driver_types.h&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;tensorflow&quot;&gt;tensorflow:&lt;/h5&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;pip3 &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;tensorflow-gpu&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;2.0.0beta0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;conda环境&quot;&gt;conda环境&lt;/h3&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;conda &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;cudatoolkit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;10.1
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;conda &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;cudnn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;7.6
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;conda &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;tensorflow-gpu&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>Wyundi</name></author><category term="机器学习" /><category term="神经网络" /><category term="tensorflow" /><summary type="html">pip环境和conda环境中TensorFlow及对应版本的CUDA和cuDNN的安装</summary></entry><entry><title type="html">Keras卷积神经网络</title><link href="https://wyundi.github.io/2020/01/17/Keras%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/" rel="alternate" type="text/html" title="Keras卷积神经网络" /><published>2020-01-17T15:25:00+00:00</published><updated>2020-01-17T15:25:00+00:00</updated><id>https://wyundi.github.io/2020/01/17/Keras%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C</id><content type="html" xml:base="https://wyundi.github.io/2020/01/17/Keras%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/">&lt;p&gt;基于Keras的卷积神经网络&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;参考：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://keras.io/&quot;&gt;Keras官方文档&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://keras-cn.readthedocs.io/en/latest/&quot;&gt;Keras中文文档&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;环境：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;操作系统：Ubuntu 18.04&lt;/p&gt;

&lt;p&gt;CUDA: 10.0&lt;/p&gt;

&lt;p&gt;TensorFLow: 1.13&lt;/p&gt;

&lt;p&gt;Keras: 2.3.1&lt;/p&gt;

&lt;h2 id=&quot;keras构建卷积神经网络&quot;&gt;Keras构建卷积神经网络&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;使用Keras构建卷积神经网络, 并将训练模型保存.&lt;/li&gt;
  &lt;li&gt;加载模型, 用于预测新图片&lt;/li&gt;
&lt;/ol&gt;

&lt;h5 id=&quot;构建网络&quot;&gt;构建网络&lt;/h5&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/env python
# -*- coding:utf-8 -*-
&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.models&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.layers.core&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Flatten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Activation&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.layers.convolutional&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Conv2D&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.layers.pooling&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MaxPooling2D&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;backend&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.utils&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np_utils&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.models&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_model&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.layers&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/home/wyundi/Project/Git/MachineLearning/Keras/Tutorial/mnist&quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 获取数据集 
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/data/x_train.npy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/data/y_train.npy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/data/x_test.npy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/data/y_test.npy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 显示图片
&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'''
plt.figure()
plt.imshow(x_train[20])
plt.show()
'''&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# channles_last
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_cols&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image_data_format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'channels_first'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ori_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ori_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 数据预处理
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ori_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ori_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;255.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;255.0&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# one-hot编码
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;classes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np_utils&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_categorical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;classes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np_utils&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_categorical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;classes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 设置模型参数
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filters&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;kernel_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;pool_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;epochs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 构建模型
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 第一层卷积
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;Conv2D&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kernel_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ori_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'relu'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;MaxPooling2D&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pool_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pool_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 第二层卷积
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;Conv2D&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kernel_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'relu'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;MaxPooling2D&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pool_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pool_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 全连接层
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;Flatten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'relu'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# 输出层
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;classes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'softmax'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'adam'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'categorical_crossentropy'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'accuracy'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 训练并验证模型
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epochs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;epochs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;validation_split&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;evaluate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;verbose&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Loss: &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Accuracy: &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 模型结构
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 保存模型
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/models/mnist_CNN.h5&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;n&quot;&gt;save_path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/models/mnist_CNN.h5&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Save path: &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;save_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;返回结果&quot;&gt;返回结果&lt;/h5&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Using TensorFlow backend.
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;60000, 28, 28&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;60000,&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;from tensorflow.python.framework.ops&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; is deprecated and will be removed &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;a future version.
Instructions &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;from tensorflow.python.ops.math_ops&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; is deprecated and will be removed &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;a future version.
Instructions &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;updating:
Use tf.cast instead.
2020-01-17 19:17:00.826600: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-01-17 19:17:00.847100: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2893305000 Hz
2020-01-17 19:17:00.847476: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x2a03750 executing computations on platform Host. Devices:
2020-01-17 19:17:00.847508: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: &amp;lt;undefined&amp;gt;, &amp;lt;undefined&amp;gt;
Train on 57000 samples, validate on 3000 samples
Epoch 1/5
57000/57000 &lt;span class=&quot;o&quot;&gt;[==============================]&lt;/span&gt; - 28s 489us/step - loss: 0.6047 - accuracy: 0.8037 - val_loss: 0.0893 - val_accuracy: 0.9780
Epoch 2/5
57000/57000 &lt;span class=&quot;o&quot;&gt;[==============================]&lt;/span&gt; - 27s 471us/step - loss: 0.2332 - accuracy: 0.9293 - val_loss: 0.0611 - val_accuracy: 0.9847
Epoch 3/5
57000/57000 &lt;span class=&quot;o&quot;&gt;[==============================]&lt;/span&gt; - 27s 469us/step - loss: 0.1834 - accuracy: 0.9438 - val_loss: 0.0522 - val_accuracy: 0.9880
Epoch 4/5
57000/57000 &lt;span class=&quot;o&quot;&gt;[==============================]&lt;/span&gt; - 26s 447us/step - loss: 0.1543 - accuracy: 0.9521 - val_loss: 0.0459 - val_accuracy: 0.9873
Epoch 5/5
57000/57000 &lt;span class=&quot;o&quot;&gt;[==============================]&lt;/span&gt; - 27s 474us/step - loss: 0.1381 - accuracy: 0.9571 - val_loss: 0.0412 - val_accuracy: 0.9903
10000/10000 &lt;span class=&quot;o&quot;&gt;[==============================]&lt;/span&gt; - 1s 143us/step
Loss:  0.042725288298050876
Accuracy:  0.9861999750137329
Model: &lt;span class=&quot;s2&quot;&gt;&quot;sequential_1&quot;&lt;/span&gt;
_________________________________________________________________
Layer &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                 Output Shape              Param &lt;span class=&quot;c&quot;&gt;#   &lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=================================================================&lt;/span&gt;
conv2d_1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Conv2D&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 26, 26, 32&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;        320       
_________________________________________________________________
max_pooling2d_1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;MaxPooling2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 13, 13, 32&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;        0         
_________________________________________________________________
conv2d_2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Conv2D&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 11, 11, 16&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;        4624      
_________________________________________________________________
max_pooling2d_2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;MaxPooling2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 5, 5, 16&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          0         
_________________________________________________________________
dropout_1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Dropout&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 5, 5, 16&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          0         
_________________________________________________________________
flatten_1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Flatten&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 400&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;               0         
_________________________________________________________________
dense_1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Dense&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;              &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 128&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;               51328     
_________________________________________________________________
dropout_2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Dropout&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 128&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;               0         
_________________________________________________________________
dense_2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Dense&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;              &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 10&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                1290      
_________________________________________________________________
activation_1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Activation&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 10&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                0         
&lt;span class=&quot;o&quot;&gt;=================================================================&lt;/span&gt;
Total params: 57,562
Trainable params: 57,562
Non-trainable params: 0
_________________________________________________________________
Save path:  /home/wyundi/Project/Git/MachineLearning/Keras/Tutorial/mnist/models/mnist_CNN.h5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;使用保存的模型预测新图片&quot;&gt;使用保存的模型预测新图片&lt;/h5&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/env python
# -*- coding:utf-8 -*-
&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;backend&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.models&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_model&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/home/wyundi/Project/Git/MachineLearning/Keras/Tutorial/mnist&quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 获取数据集 
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/data/x_train.npy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/data/y_train.npy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/data/x_test.npy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/data/y_test.npy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 显示图片
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;figure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imshow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# plt.show()
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# channles_last
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_cols&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image_data_format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'channels_first'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ori_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ori_shape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img_rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img_cols&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 数据预处理
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ori_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ori_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;255.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;255.0&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 加载模型
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;load_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/models/mnist_CNN.h5&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 整理图片尺寸
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_pri&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x_pri&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_pri&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ori_shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_pri&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 预测
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Predict: &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_pri&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Label: &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;预测结果&quot;&gt;预测结果&lt;/h5&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Using TensorFlow backend.
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;60000, 28, 28, 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;60000,&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;from tensorflow.python.framework.ops&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; is deprecated and will be removed &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;a future version.
Instructions &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;updating:
Colocations handled automatically by placer.
2020-01-17 19:22:16.737457: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-01-17 19:22:16.759050: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2893305000 Hz
2020-01-17 19:22:16.759443: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x25a9620 executing computations on platform Host. Devices:
2020-01-17 19:22:16.759475: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;: &amp;lt;undefined&amp;gt;, &amp;lt;undefined&amp;gt;
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;from tensorflow.python.ops.math_ops&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; is deprecated and will be removed &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;a future version.
Instructions &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;updating:
Use tf.cast instead.
Model: &lt;span class=&quot;s2&quot;&gt;&quot;sequential_1&quot;&lt;/span&gt;
_________________________________________________________________
Layer &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                 Output Shape              Param &lt;span class=&quot;c&quot;&gt;#   &lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=================================================================&lt;/span&gt;
conv2d_1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Conv2D&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 26, 26, 32&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;        320       
_________________________________________________________________
max_pooling2d_1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;MaxPooling2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 13, 13, 32&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;        0         
_________________________________________________________________
conv2d_2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Conv2D&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 11, 11, 16&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;        4624      
_________________________________________________________________
max_pooling2d_2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;MaxPooling2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 5, 5, 16&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          0         
_________________________________________________________________
dropout_1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Dropout&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 5, 5, 16&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          0         
_________________________________________________________________
flatten_1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Flatten&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 400&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;               0         
_________________________________________________________________
dense_1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Dense&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;              &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 128&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;               51328     
_________________________________________________________________
dropout_2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Dropout&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 128&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;               0         
_________________________________________________________________
dense_2 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Dense&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;              &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 10&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                1290      
_________________________________________________________________
activation_1 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Activation&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;None, 10&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                0         
&lt;span class=&quot;o&quot;&gt;=================================================================&lt;/span&gt;
Total params: 57,562
Trainable params: 57,562
Non-trainable params: 0
_________________________________________________________________
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;1, 28, 28, 1&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Predict:  7
Label:  7
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>Wyundi</name></author><category term="机器学习" /><category term="神经网络" /><category term="tensorflow" /><category term="Keras" /><category term="卷积神经网络" /><summary type="html">基于Keras的卷积神经网络</summary></entry><entry><title type="html">Keras基础</title><link href="https://wyundi.github.io/2020/01/17/Keras%E5%9F%BA%E7%A1%80/" rel="alternate" type="text/html" title="Keras基础" /><published>2020-01-17T10:30:00+00:00</published><updated>2020-01-17T10:30:00+00:00</updated><id>https://wyundi.github.io/2020/01/17/Keras%E5%9F%BA%E7%A1%80</id><content type="html" xml:base="https://wyundi.github.io/2020/01/17/Keras%E5%9F%BA%E7%A1%80/">&lt;ol&gt;
  &lt;li&gt;Keras基础结构&lt;/li&gt;
  &lt;li&gt;以mnist数据库为例构建Keras神经网络&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;参考：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://keras.io/&quot;&gt;Keras官方文档&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://keras-cn.readthedocs.io/en/latest/&quot;&gt;Keras中文文档&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://keras.io/zh/getting-started/sequential-model-guide/&quot;&gt;Sequential顺序模型&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://keras.io/zh/layers/core/&quot;&gt;核心网络层&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://keras.io/optimizers/&quot;&gt;优化器(optimizer)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://keras.io/losses/&quot;&gt;损失函数(loss)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://keras.io/models/sequential/&quot;&gt;模型方法(训练/测试等)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/42340680&quot;&gt;dropout中noise_shape参数的作用&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;环境：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;操作系统：Ubuntu 18.04&lt;/p&gt;

&lt;p&gt;CUDA: 10.0&lt;/p&gt;

&lt;p&gt;TensorFLow: 1.13&lt;/p&gt;

&lt;p&gt;Keras: 2.3.1&lt;/p&gt;

&lt;h2 id=&quot;keras构建神经网络框架&quot;&gt;Keras构建神经网络框架&lt;/h2&gt;

&lt;h3 id=&quot;sequential-model-顺序模型&quot;&gt;Sequential model (顺序模型)&lt;/h3&gt;

&lt;p&gt;顺序模型是多个神经网络层的堆叠.&lt;/p&gt;

&lt;h5 id=&quot;构建方法&quot;&gt;构建方法:&lt;/h5&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;1. 将网络层构建成列表, 作为参数传入model.Sequential()
 	2. 创建空的Sequential模型后使用model.add()方法向其中添加层
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.models&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.layers&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Activation&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 方法1
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;784&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,)),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'relu'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'softmax'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 方法2
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;784&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'relu'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;设定输入数据维数&quot;&gt;设定输入数据维数&lt;/h5&gt;

&lt;p&gt;向Sequential模型添加网络层时, 需要在第一层定义输入数据的维数. (仅在第一层需要, 因为后面的层可以自动获取前一层的输出维度)&lt;/p&gt;

&lt;p&gt;有以下几种方法:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;在第一层的参数中定义, input_shape = (x, y)&lt;/li&gt;
  &lt;li&gt;类似Dense这种2维的层支持通过参数input_dim指定输入尺寸. 某些3维层支持input_dim和input_length参数&lt;/li&gt;
  &lt;li&gt;如果需要为输入的数据指定一个固定的batch大小, 可以给层传递一个参数batch_size. 例如: 在第一层的参数中包含 (batch_size = 32, input_shape=(6, 8)), 那么每一批输入第一层的数据维度就是(32, 6, 8)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;因此, 下面两种方法是等价的:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 方法1
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;784&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,)))&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 方法2
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;784&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;模型编译&quot;&gt;模型编译&lt;/h5&gt;

&lt;p&gt;在训练模型之前, 需要配置学习过程. 这个过程通过compile方法完成. 他接收三个参数:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://keras.io/optimizers/&quot;&gt;优化器(optimizer)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://keras.io/losses/&quot;&gt;损失函数(loss)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;评估标准(metrics)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;这三个参数可以是现有优化器的字符串标识符, 也可以是具体的函数.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 多分类问题
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'rmsprop'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'categorical_crossentropy'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'accuracy'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 二分类问题
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'rmsprop'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'binary_crossentropy'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'accuracy'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 均方误差回归问题
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'rmsprop'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'mse'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 自定义评估标准函数
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.backend&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mean_pred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_pred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;K&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y_pred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'rmsprop'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'binary_crossentropy'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'accuracy'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean_pred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;模型训练和验证&quot;&gt;模型训练和验证&lt;/h5&gt;

&lt;p&gt;使用model.fit()进行模型训练. fit函数的参数包括训练集数据,标签等&lt;/p&gt;

&lt;p&gt;使用model.evaluate()进行模型验证, 参数使用测试集数据和标签.&lt;/p&gt;

&lt;p&gt;参数versbose用于设置日志现实方式:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;0: 不在标准输出流输出日志信息&lt;/li&gt;
  &lt;li&gt;1: 输出进度条记录&lt;/li&gt;
  &lt;li&gt;2: 每个epoch输出一行记录&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 训练模型，以 32 个样本为一个 batch 进行迭代
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;labels&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epochs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 模型验证
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;evaluate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;verbose&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;模型&lt;/p&gt;

&lt;h3 id=&quot;自定义模型&quot;&gt;自定义模型&lt;/h3&gt;

&lt;p&gt;给定输入和输出的的张量, 可以创建一个新的模型:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.models&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.layers&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;对于多个输入和输出数据, 可以使用列表进行创建:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;outputs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;核心网络层&quot;&gt;核心网络层&lt;/h3&gt;

&lt;p&gt;常用核心层包括 Dense/Activation/Dropout/Flatten. 其余核心层查看&lt;a href=&quot;https://keras.io/zh/layers/core/&quot;&gt;官方文档&lt;/a&gt;.&lt;/p&gt;

&lt;h5 id=&quot;dense&quot;&gt;Dense&lt;/h5&gt;

&lt;p&gt;全连接层. 实现以下操作:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中, activation是激活函数, kernal和bias分别是由网络层创建的权重矩阵和偏置向量.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;参数:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;units&lt;/strong&gt;: 正整数，输出空间维度。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;activation&lt;/strong&gt;: 激活函数 (详见 &lt;a href=&quot;https://keras.io/zh/activations/&quot;&gt;activations&lt;/a&gt;)。 若不指定，则不使用激活函数 (即，「线性」激活: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a(x) = x&lt;/code&gt;)。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;use_bias&lt;/strong&gt;: 布尔值，该层是否使用偏置向量。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;kernel_initializer&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kernel&lt;/code&gt; 权值矩阵的初始化器 (详见 &lt;a href=&quot;https://keras.io/zh/initializers/&quot;&gt;initializers&lt;/a&gt;)。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;bias_initializer&lt;/strong&gt;: 偏置向量的初始化器 (see &lt;a href=&quot;https://keras.io/zh/initializers/&quot;&gt;initializers&lt;/a&gt;).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;kernel_regularizer&lt;/strong&gt;: 运用到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kernel&lt;/code&gt; 权值矩阵的正则化函数 (详见 &lt;a href=&quot;https://keras.io/zh/regularizers/&quot;&gt;regularizer&lt;/a&gt;)。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;bias_regularizer&lt;/strong&gt;: 运用到偏置向的的正则化函数 (详见 &lt;a href=&quot;https://keras.io/zh/regularizers/&quot;&gt;regularizer&lt;/a&gt;)。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;activity_regularizer&lt;/strong&gt;: 运用到层的输出的正则化函数 (它的 “activation”)。 (详见 &lt;a href=&quot;https://keras.io/zh/regularizers/&quot;&gt;regularizer&lt;/a&gt;)。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;kernel_constraint&lt;/strong&gt;: 运用到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kernel&lt;/code&gt; 权值矩阵的约束函数 (详见 &lt;a href=&quot;https://keras.io/zh/constraints/&quot;&gt;constraints&lt;/a&gt;)。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;bias_constraint&lt;/strong&gt;: 运用到偏置向量的约束函数 (详见 &lt;a href=&quot;https://keras.io/zh/constraints/&quot;&gt;constraints&lt;/a&gt;)。&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;activation&quot;&gt;Activation&lt;/h5&gt;

&lt;p&gt;激活函数. 输入尺寸和输出尺寸一致, 都和上一层的输出尺寸相同.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;keras.layers.Activation(activation)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;dropout&quot;&gt;Dropout&lt;/h5&gt;

&lt;p&gt;按比例将输入下一层的数据随机设置为0. 有助于防止过拟合.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;参数:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;rate: 丢弃数据的比例, 范围0-1&lt;/li&gt;
  &lt;li&gt;noise_shape: &lt;a href=&quot;https://zhuanlan.zhihu.com/p/42340680&quot;&gt;dropout中noise_shape参数的作用&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;seed: 作为随机种子的python整数&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;flatten&quot;&gt;Flatten&lt;/h5&gt;

&lt;p&gt;将输入层展平, 不影响批量的大小&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;参数:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;data_format&lt;/strong&gt;：一个字符串，其值为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;channels_last&lt;/code&gt;（默认值）或者 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;channels_first&lt;/code&gt;。它表明输入的维度的顺序。此参数的目的是当模型从一种数据格式切换到另一种数据格式时保留权重顺序。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;channels_last&lt;/code&gt; 对应着尺寸为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(batch, ..., channels)&lt;/code&gt; 的输入，而 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;channels_first&lt;/code&gt; 对应着尺寸为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(batch, channels, ...)&lt;/code&gt; 的输入。默认为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image_data_format&lt;/code&gt; 的值，你可以在 Keras 的配置文件 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.keras/keras.json&lt;/code&gt; 中找到它。如果你从未设置过它，那么它将是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;channels_last&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Conv2D&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                 &lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'same'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 现在：model.output_shape == (None, 64, 32, 32)
&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Flatten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 现在：model.output_shape == (None, 65536)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;构建mnist神经网络&quot;&gt;构建mnist神经网络&lt;/h2&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#!/usr/bin/env python
# -*- coding:utf-8 -*-
&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.models&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;keras.layers&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;.../mnist/data&quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 获取数据集 
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/x_train.npy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/y_train.npy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/x_test.npy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/y_test.npy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 数据归一化
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;255.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;255.0&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 构建模型
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;keras&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Flatten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_shape&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;28&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;keras&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'relu'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;keras&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dropout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;keras&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dense&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'softmax'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'adam'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;loss&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'sparse_categorical_crossentropy'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;metrics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'accuracy'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 训练并验证模型
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;epochs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;evaluate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;verbose&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 模型结构
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 预测
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name>Wyundi</name></author><category term="机器学习" /><category term="神经网络" /><category term="tensorflow" /><category term="Keras" /><summary type="html">Keras基础结构 以mnist数据库为例构建Keras神经网络</summary></entry><entry><title type="html">Machine Learning: 神经网络</title><link href="https://wyundi.github.io/2019/04/10/Machine-Learning-%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/" rel="alternate" type="text/html" title="Machine Learning: 神经网络" /><published>2019-04-10T10:54:00+00:00</published><updated>2019-04-10T10:54:00+00:00</updated><id>https://wyundi.github.io/2019/04/10/Machine-Learning-%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C</id><content type="html" xml:base="https://wyundi.github.io/2019/04/10/Machine-Learning-%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/">&lt;p&gt;全连接神经网络基础及相关数学计算。&lt;/p&gt;

&lt;h1 id=&quot;machine-learning-神经网络&quot;&gt;Machine Learning: 神经网络&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;参考：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.kongfy.com/2014/11/machine-learning%E5%B0%8F%E7%BB%931%EF%BC%9A%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E3%80%81%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92%E5%92%8C%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/&quot;&gt;Machine Learning小结(1)：线性回归、逻辑回归和神经网络&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.ruanyifeng.com/blog/2017/07/neural-network.html&quot;&gt;神经网络入门_阮一峰&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.jiqizhixin.com/articles/2017-10-10-3&quot;&gt;26种神经网络激活函数可视化_机器之心&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/32610035&quot;&gt;常用激活函数的比较&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/25723112&quot;&gt;详解softmax函数以及相关求导过程&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/juanjuan1314/article/details/78063984&quot;&gt;代价函数总结&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/21462488?refer=intelligentunit&quot;&gt;CS231n课程笔记翻译：神经网络笔记1（上）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/mike112223/article/details/75126402&quot;&gt;机器学习（五）- 对于cost function的思考&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://fangs.in/post/thinkstats/likelihood/&quot;&gt;似然与极大似然估计&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.csdn.net/zengxiantao1994/article/details/72787849&quot;&gt;极大似然估计详解&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/27223959&quot;&gt;Softmax函数与交叉熵&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://study.163.com/course/courseMain.htm?courseId=1004570029&quot;&gt;吴恩达机器学习&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mooc.study.163.com/learn/2001281002?tid=2001392029&amp;amp;_trace_c_p_k2_=db6bf4087fc04a09aa4143e4e92e172a&quot;&gt;深度学习工程师_01.神经网络和深度学习&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.ituring.com.cn/book/2661&quot;&gt;卷积神经网络的Python实现&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;环境：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;操作系统：Ubuntu 18.04&lt;/p&gt;

&lt;p&gt;神经网络绘图：&lt;a href=&quot;http://alexlenail.me/NN-SVG/index.html&quot;&gt;NN-SVG&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;神经网络neural-network&quot;&gt;&lt;strong&gt;神经网络（Neural Network）&lt;/strong&gt;&lt;/h2&gt;

&lt;h3 id=&quot;从线性回归和逻辑回归说起&quot;&gt;&lt;strong&gt;从线性回归和逻辑回归说起&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;先给出线性回归和逻辑回归的模型：&lt;/p&gt;

&lt;p&gt;线性回归：&lt;/p&gt;

\[h = \theta^Tx\]

&lt;p&gt;逻辑回归：&lt;/p&gt;

\[z(x) = \theta^Tx\]

\[h(x) = \sigma(z)\]

&lt;p&gt;在这两个模型中都包含$\theta^Tx$。在线性回归中，这个方程表示对实际数据走向的预测。而在逻辑回归中，这个方程表示数据的决策边界。如果数据图像或数据边界近似为一条直线，则可以用一次函数对其进行预测，如果图像近似为一条曲线，就需要用高幂次的方程替换一次函数，才能产生较好的效果。比如，图像近似为一个圆形，就需要用类似：&lt;/p&gt;

\[h_\theta(x) = g(\theta_0 + \theta_1x1 + \theta_2x2 + \theta_3x_1^2 + \theta_4x_2^2)\]

&lt;p&gt;这样的圆方程来对其进行预测。我们可以使用非常复杂的模型对数据分布进行预测，或者来适应复杂的决策边界。&lt;/p&gt;

&lt;p&gt;但无论是线性回归还是逻辑回归，当特征太多时，计算负荷都会非常大。如果我们有100个特征，并希望用这些特征构建一个非线性的模型，就需要数量非常惊人的特征组合。即使只采用两两组合（$x_1x_2, x_2x_3,…x_{99}x_{100}$），也会有接近5000个特征。这对于一个简单模型来说太多了。这时候我们需要神经网络。&lt;/p&gt;

&lt;h3 id=&quot;神经网络结构&quot;&gt;&lt;strong&gt;神经网络结构&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;生物的神经中枢进行思考需要进行以下几个步骤：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;外部刺激通过神经末梢，转化为电信号，传导到神经细胞（神经元）。&lt;/li&gt;
    &lt;li&gt;无数神经元构成神经中枢。&lt;/li&gt;
    &lt;li&gt;神经中枢综合各种信号，作出判断。&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;基于对神经元的模拟，建立了称为“感知器”的模型：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning3_NeuralNetwork/bg2017071202.png&quot; alt=&quot;bg2017071202&quot; /&gt;&lt;/p&gt;

&lt;p&gt;图中的圆圈即代表神经元（感知器）。它接受多个输入（$x_1, x_2, x_3$），并产生一个输出（$output$）。&lt;/p&gt;

&lt;p&gt;多个神经元构成神经网络（神经中枢）：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning3_NeuralNetwork/bg2017071205.png&quot; alt=&quot;bg2017071205&quot; /&gt;&lt;/p&gt;

&lt;p&gt;图中每一个圆圈代表一个神经元，所有的神经元相互连接，构成神经网络。这个神经网络接受5个外部输入，数据经过神经网络处理后获得一个输出。&lt;/p&gt;

&lt;h3 id=&quot;逻辑回归和神经网络&quot;&gt;&lt;strong&gt;逻辑回归和神经网络&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;逻辑回归可以看作是一个非常小的神经网络。逻辑回归模型接受输入以后，首先要对数据赋予权重（weight）和偏置（bias），然后使用激活函数（$\alpha(z)$）对其进行激活，并将结果作为输出值。这个过程和神经元的运算过程是一样的，区别只是使用的激活函数不同，所以可以将逻辑回归模型看作是一个只包含一个神经元的神经网络。对于大规模的神经网络，只需要将多个类似逻辑回归的神经元组合在一起即可。&lt;/p&gt;

&lt;p&gt;将三个神经元堆叠在一起，然后将其输出作为另一个神经元的输入值，即可产生一个基础的神经网络。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning3_NeuralNetwork/Screenshot from 2019-03-26 15-27-37.png&quot; alt=&quot;Screenshot from 2019-03-26 15-27-37&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;直观理解&quot;&gt;&lt;strong&gt;直观理解&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;从本质上讲，神经网络能够通过学习得出其自身的一系列特征。在普通的逻辑回归中，我们被限制使用数据中的原始特征$x_1, x_2, x_3, …, x_n​$，虽然我们可以使用一些二项式来组合这些特征，但仍受这些原始特征的限制，无法表示所有可能的情况。在神经网络中，原始特征只作为输入层，只有第一层神经网络直接利用原始特征作为数据。从第二层开始，所有后面的神经网络的基础数据都是前一层通过学习后得出的新特征，即不受原始特征的限制。所以，更深的神经网络可以学到更多的特征，模拟更复杂的函数。&lt;/p&gt;

&lt;p&gt;神经网络中，单个神经元可以表示简单逻辑运算 (and/or/not)。&lt;/p&gt;

&lt;p&gt;对于AND运算：&lt;/p&gt;

\[input = x_1, x_2 \in \{0, 1\} \\
output = x_1 \  AND \  x_2\]

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning3_NeuralNetwork/Screenshot from 2019-03-26 18-02-57.png&quot; alt=&quot;Screenshot from 2019-03-26 18-02-57&quot; /&gt;&lt;/p&gt;

&lt;p&gt;其中，令$\theta_0 = -3​$，$\theta_1 = 2​$ ，$\theta_2 = 2​$，则$h_{\theta}(x) = sigmoid(-3 + 2x_1 + 2x_2)​$。&lt;/p&gt;

&lt;p&gt;则有真值表：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;$x_1$&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;$x_2$&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;$h_{\theta}(x)$&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;可以看出，$h_{\theta}(x) = x_1 \ AND \ x_2$。&lt;/p&gt;

&lt;p&gt;同样：&lt;/p&gt;

&lt;p&gt;对于OR运算：取$\theta_0 = -1$，$\theta_1 = 2$ ，$\theta_2 = 2$，则$h_{\theta}(x) = sigmoid(-1 + 2x_1 + 2x_2) = x_1 \ OR \ x_2$；&lt;/p&gt;

&lt;p&gt;对于NOT运算：取$\theta_0 = 1$，$\theta_1 = -2$，则$h_{\theta}(x) = sigmoid(1 - 2x_1) = NOT \ x_1​$。&lt;/p&gt;

&lt;p&gt;但如果只使用一个神经元，就无法表示复杂的逻辑运算，例如同或 (XNOR)，因为同或不是一个线性可分结构。构造XNOR运算需要使用神经网络。&lt;/p&gt;

&lt;p&gt;XNOR运算逻辑如下：&lt;/p&gt;

\[XNOR = (x_1 \ AND \ x_2)OR((NOT \ x_1)AND(NOT \ x_2))\]

&lt;p&gt;首先构造一个能表达$((NOT \ x_1)AND(NOT \ x_2))$部分的神经元，取$\theta_0 = 3$，$\theta_1 = -2$ ，$\theta_2 = -2$，则$h_{\theta}(x) = sigmoid(3 - 2x_1 - 2x_2) = (NOT \ x_1)AND(NOT \ x_2)$。&lt;/p&gt;

&lt;p&gt;然后将该神经元的结果和AND神经元的结果作为输入值构建OR神经元，即可得到能表示XNOR运算的神经网络。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning3_NeuralNetwork/Screenshot from 2019-03-26 18-55-32.png&quot; alt=&quot;Screenshot from 2019-03-26 18-55-32&quot; /&gt;&lt;/p&gt;

&lt;p&gt;其中，隐藏层的两个神经元即为$((NOT \ x_1)AND(NOT \ x_2))$部分和AND部分。&lt;/p&gt;

&lt;p&gt;通过这种方法，我们就可以构建更复杂的神经网络来模拟更复杂的函数关系。&lt;/p&gt;

&lt;h3 id=&quot;神经网络的表示&quot;&gt;&lt;strong&gt;神经网络的表示&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning3_NeuralNetwork/Screenshot from 2019-03-26 15-27-37.png&quot; alt=&quot;Screenshot from 2019-03-26 15-27-37&quot; /&gt;&lt;/p&gt;

&lt;p&gt;在上图的神经网络中，共包含（$ x_1, x_2, x_3 $）三个输入变量，他们竖直的堆叠起来成为一层，这是神经网络的输入层。中间四个神经元所产生的结果直接传入下一层的节点中，在训练过程中我们看不到他们产生的结果，所以将他们称为隐藏层。最后一个结点接受隐藏层四个节点的数据，产生最后的结果，这一层称为输出层，它负责产生预测值。通常我们将输入层称为第零层，所以隐藏层为第一层，输出层为第二层。这个神经网络是一个两层的神经网络。&lt;/p&gt;

&lt;p&gt;将左侧三个数据（$x_1, x_2, x_3 ​$）作为输入值，中间四个神经元堆叠在一起成为神经网络的第一层。使用上角标 $X^{[layers] (sample)}​$ 分别表示数据在神经网络的第几层/这是第几个样本，下角标 $X_{[features]}​$ 表示数据是该层第几个特征。&lt;/p&gt;

&lt;p&gt;例如，$x^{[1] (5)}_3$ 表示这个数据是第第一层的第五个样本中第三个特征。&lt;/p&gt;

&lt;p&gt;用$a^{[0]}​$来表示输入特征。$a​$表示激活的意思，它意味着网络中本层的值会传递到下一层。输入层将x传递给隐藏层，所以将输入层的激活值称为$a^{[0]}​$。下一层隐藏层同样会产生激活值，将其记为$a^{[1]}​$。按同样的规则，将每层线性方程的结果记为$z​$，参数记为$\theta​$，或$W​$和$b​$。&lt;/p&gt;

&lt;h2 id=&quot;神经网络计算&quot;&gt;&lt;strong&gt;神经网络计算&lt;/strong&gt;&lt;/h2&gt;

&lt;h3 id=&quot;数据向量化&quot;&gt;&lt;strong&gt;数据向量化&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;为方便计算同时提高运算速度，将所有数据合并为矩阵统一进行运算。对于输入层，将输入的三个特征合并成一个列向量，用$a^{[0]}$表示他们。即：&lt;/p&gt;

\[a^{[0]} = x = 
\left[
\begin{matrix}
x_1 \\
x_2 \\
x_3
\end{matrix}
\right]
=
\left[
\begin{matrix}
a^{[0]}_1 \\
a^{[0]}_2 \\
a^{[0]}_3
\end{matrix}
\right]\]

&lt;p&gt;$w$的维度取决于从上一层引入的特征数和本层需要产生的特征数。对于隐藏层每一个神经元，都从输入层引入了3个特征，所以每一个神经元都有3个参数 ($w_{n1}, w_{n2}, w_{n3}$)，可以合并成一个 3x1 的矩阵$w_n$。隐藏层共包含4个神经元，就有4个 3x1 的参数 ($w_1, w_2, w_3, w_4$)，将其合并为一个 3x4 大矩阵$W$：&lt;/p&gt;

\[W = 
\left[
\begin{matrix}
w_{11} &amp;amp; w_{21} &amp;amp; w_{31} &amp;amp; w_{41}\\
w_{12} &amp;amp; w_{22} &amp;amp; w_{32} &amp;amp; w_{42}\\
w_{13} &amp;amp; w_{23} &amp;amp; w_{33} &amp;amp; w_{43}
\end{matrix}
\right]\]

&lt;p&gt;每一个神经元对应一个参数$b$，同样将其合并为矩阵：&lt;/p&gt;

\[b =
\left[
\begin{matrix}
b_1 \\
b_2 \\
b_3 \\
b_4
\end{matrix}
\right]\]

&lt;h3 id=&quot;前向传播&quot;&gt;&lt;strong&gt;前向传播&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;对于每一个神经元，计算过程如下图所示：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning3_NeuralNetwork/Screenshot%20from%202019-03-26%2015-06-27.png&quot; alt=&quot;Screenshot from 2019-03-26 15-06-27&quot; /&gt;&lt;/p&gt;

&lt;p&gt;回到两层神经网络，逐个计算每个神经元。&lt;/p&gt;

&lt;p&gt;首先使用线性方程计算第一层即隐藏层各节点的$z^{[1]}​$，将结果代入激活函数计算$\alpha^{[1]}​$，并将$\alpha^{[1]}​$作为第二层的输入值传入神经网络的第二层。然后用另一组线性方程和激活函数计算$z^{[2]}​$和$\alpha^{[2]}​$，将$\alpha^{[2]}​$作为输出。这就是一个简单的神经网络前向传播的计算过程。&lt;/p&gt;

&lt;p&gt;公式如下：&lt;/p&gt;

\[(a^{[0]}, \theta^{[1]}) \Rightarrow z^{[1]} = W^{[1]T}x + b^{[1]}\Rightarrow \alpha^{[1]} = \sigma(z^{[1]})\]

\[(\sigma, \theta^{[2]}) \Rightarrow z^{[2]} = W^{[2]T}\sigma^{[2]} + b^{[2]}\Rightarrow \alpha^{[2]} = \sigma(z^{[2]})\]

&lt;p&gt;其中，各数据维度如下：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Data&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Dimension&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$a^{[0]}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;(3, 1)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$W^{[1]}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;(3, 4)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$b^{[1]}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;(4, 1)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$z^{[1]}, a^{[1]}, \sigma(z^{[1]})$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;(4, 1)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$W^{[2]}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;(4, 1)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$b^{[2]}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;(1, 1)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$z^{[2]}, a^{[2]}, \sigma(z^{[2]})$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;(1, 1)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;以上公式就完成了对一个样本的数据计算，最后给出的结果即为对该样本的预测结果。对于所有样本，同样可以通过向量化简化其运算。&lt;/p&gt;

&lt;h4 id=&quot;公式整理&quot;&gt;&lt;strong&gt;公式整理&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;设数据中共有$m​$个样本，每个样本有$n^{[0]}​$个特征。每一个样本的特征可以合并为一个 $(n^{[0]}, 1)​$ 的列向量。将所有样本对应的列向量转置然后堆叠在一起，即得到了一个维度为$ (m, n^{[0]})​$ 的矩阵，X。&lt;/p&gt;

&lt;p&gt;建立一个 $l$ 层的神经网络。每一层的特征数（神经元个数）用$n$表示，即为 $(n^{[1]}, n^{[2]}, …, n^{[l]})​$。&lt;/p&gt;

&lt;p&gt;相对应的，公式也应该变为如下形式以和其维度相匹配：&lt;/p&gt;

\[(X, \theta^{[1]}) \Rightarrow z^{[1]} = XW^{[1]} + b^{[1]T}\Rightarrow \alpha^{[1]} = \sigma(z^{[1]})\]

\[(\sigma(z^{[1]}, \theta^{[2]}) \Rightarrow z^{[2]} = \sigma^{[2]}W^{[2]} + b^{[2]T}\Rightarrow \alpha^{[2]} = \sigma(z^{[2]})\]

\[\vdots\]

\[(\sigma(z^{[l-1]}, \theta^{[l]}) \Rightarrow z^{[l]} = \sigma^{[l]}W^{[l]} + b^{[l]T}\Rightarrow \alpha^{[l]} = \sigma(z^{[l]})\]

&lt;p&gt;数据维度如下：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Data&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Dimension&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$X$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$(m, n^{[0]})$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$W^{[1]}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$(n^{[0]}, n^{[1]})$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$b^{[1]}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$(n^{[1]}, 1)$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$z^{[1]}, a^{[1]}, \sigma(z^{[1]})$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$(m, n^{[1]})$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$W^{[2]}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$(n^{[1]}, n^{[2]})$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$b^{[2]}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$(n^{[2]}, 1)$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$z^{[2]}, a^{[2]}, \sigma(z^{[2]})$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$(m, n^{[2]})$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$\vdots$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$\vdots$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$W^{[l]}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$(n^{[l-1]}, n^{[l]})$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$b^{[l]}$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$(n^{[l]}, 1)$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$z^{[l]}, a^{[l]}, \sigma(z^{[l]})$&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;$(m, n^{[l]})$&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;激活函数&quot;&gt;&lt;strong&gt;激活函数&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;在神经网络中，激活函数决定来自给定输入集的节点的输出，其中非线性激活函数允许网络模拟复杂的非线性行为。同时，激活函数需要是（几乎完全）可微分的，才可以通过梯度下降对神经网络进行优化。此外，复杂的激活函数可能会产生梯度消失或梯度爆炸等问题。&lt;/p&gt;

&lt;p&gt;使用一个神经网络时，需要决定使用哪种激活函数用在隐藏层上，哪种用在输出节点上。前面用到的sigmoid函数在某些情况下效果很好，但在某些情况下会出问题，这时可以使用其他激活函数。&lt;/p&gt;

&lt;p&gt;常见的激活函数包括Sigmoid、ReLU、Linear等，还有一些特殊的激活函数如Softmax。&lt;/p&gt;

&lt;h4 id=&quot;sigmoid&quot;&gt;&lt;strong&gt;Sigmoid&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;公式：&lt;/strong&gt;&lt;/p&gt;

\[\alpha = \sigma(z) = \frac{1}{1 + e^{-z}}\]

&lt;p&gt;&lt;strong&gt;图像：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning3_NeuralNetwork/sigmoid.png&quot; alt=&quot;sigmoid&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Sigmoid函数可以将实际的输出值“挤压“到0到1的范围内，适合输出为概率的情况。但由于其存在一些问题，现在已经很少有人在构建神经网络的过程中使用Sigmoid函数了。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;存在的问题：&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Sigmoid函数饱和使梯度消失。当Sigmoid值接近0和1时，其导数会接近0，那么求得的梯度也会接近0，这会导致算法在反向传播的过程中没有信号传回上一层，即梯度消失。&lt;/li&gt;
  &lt;li&gt;Sigmoid函数的输出不是零中心的。Sigmoid函数产生的结果总是正数且平均值为0.5而不是0，这会使神经网络收敛的速度更慢。因为下一层神经网络的输入值不是零中心，就更容易产生饱和，导致梯度消失。&lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;tanh&quot;&gt;&lt;strong&gt;Tanh&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;公式：&lt;/strong&gt;&lt;/p&gt;

\[\alpha = 2\sigma(2z) - 1 = \frac{1 - e^{-z}}{1 + e^{-z}}\]

&lt;p&gt;&lt;strong&gt;图像：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning3_NeuralNetwork/tanh.png&quot; alt=&quot;tanh&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Tanh是将Sigmoid函数的范围扩展到了 (-1, 1)，这就解决了SIgmoid函数输出不是零中心的问题。但仍然存在函数饱和的问题。结果表明，如果在隐藏层上使用tanh，结果总是优于sigmoid函数。但是在输出层上，如果需要处理二分类问题，就需要y的值处于0和1之间而不是-1和1之间。此时需要使用sigmoid函数。&lt;/p&gt;

&lt;p&gt;为了防止饱和，现在主流的做法会在激活函数前多做一步batch normalization，尽可能保证每一层网络的输入值具有均值较小，零中心的分布。&lt;/p&gt;

&lt;h4 id=&quot;relu&quot;&gt;&lt;strong&gt;ReLU&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;公式：&lt;/strong&gt;&lt;/p&gt;

\[\alpha = max(0, z)\]

&lt;p&gt;&lt;strong&gt;图像：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning3_NeuralNetwork/ReLU.png&quot; alt=&quot;ReLU&quot; /&gt;&lt;/p&gt;

&lt;p&gt;修正线性单元（Rectified linear unit，ReLU）函数是神经网络中最常用的激活函数。ReLU函数模仿了生物神经元的特性，即只有在输入超过阈值时才激活神经元。并且在输入为正时，导数不为0，从而允许基于梯度下降的优化。因为无论是其函数还是导数都不包含复杂的数学运算，所以使用ReLU函数也极大的减少了计算时间。&lt;/p&gt;

&lt;p&gt;然而，当输入值为负时，ReLU函数的梯度为0，从而其权重无法得到更新，并且在此后的训练过程中该神经元会一直保持沉默，即神经元死亡。但是，有足够的隐藏层使ReLU函数的输入值大于0，所以对于大多数训练数据来说学习过程仍然可以很快。&lt;/p&gt;

&lt;p&gt;如果在隐藏层上不确定使用哪个激活函数，那么通常会使用ReLU函数。&lt;/p&gt;

&lt;p&gt;还有另一个版本的ReLU函数被称为Leaky ReLU。当z是负值时，其函数值不是0，而是轻微的倾斜。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;图像：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning3_NeuralNetwork/LeakyReLU.png&quot; alt=&quot;LeakyReLU&quot; /&gt;&lt;/p&gt;

&lt;p&gt;由于其在负半区梯度不为0，所以不会产生ReLU函数在负半区神经元死亡的问题。这个函数通常比ReLU函数效果要好，但并不常用。&lt;/p&gt;

&lt;h4 id=&quot;softmax&quot;&gt;&lt;strong&gt;Softmax&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;公式：&lt;/strong&gt;&lt;/p&gt;

\[S_i = \frac{e^{z_i}}{\sum\limits_{j = 1}^n e^{z_j}}\]

&lt;p&gt;&lt;strong&gt;示意图如下：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning3_NeuralNetwork/softmax.jpg&quot; alt=&quot;softmax&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Softmax函数是Logistic回归模型在多分类问题上的推广，适用于多分类且不同类别之间互斥的问题。当类别数 k = 2 时，Softmax函数退化为Logistic回归。Softmax函数可以将多个神经元的输出映射到 (0, 1) 区间，可以看作当前输出是其属于各分类的概率，从而解决多分类的问题。&lt;/p&gt;

&lt;p&gt;对Softmax函数求导：&lt;/p&gt;

&lt;p&gt;当 $i = j$ 时：&lt;/p&gt;

\[\frac{\partial S_i}{\partial z_j} = \frac{\partial \frac{e^{z_i}}{\sum\limits_{k = 1}^{n} e^{z_k}}}{\partial z_j} = \frac{e^{z_i}\sum\limits_{k = 1}^{n} e^{z_k} - e^{z_i}e^{z_j}}{[\sum\limits_{k = 1}^{n} e^{z_k}]^2} = \frac{e^{z_i}}{\sum\limits_{k = 1}^{n} e^{z_k}}\frac{\sum\limits_{k = 1}^{n} e^{z_k} - e^{z_j}}{\sum\limits_{k = 1}^{n} e^{z_k}} = S_i(1 - S_j)\]

&lt;p&gt;当 $i \neq j​$ 时：&lt;/p&gt;

\[\frac{\partial S_i}{\partial z_j} = \frac{\partial \frac{e^{z_i}}{\sum\limits_{k = 1}^{n} e^{z_k}}}{\partial z_j} = \frac{0 - e^{z_i}e^{z_j}}{[\sum\limits_{k = 1}^{n} e^{z_k}]^2} = -\frac{e^{z_i}}{\sum\limits_{k = 1}^{n} e^{z_k}}\frac{e^{z_j}}{\sum\limits_{k = 1}^{n} e^{z_k}} = -S_iS_j\]

&lt;p&gt;即：&lt;/p&gt;

\[\frac{\partial S_i}{\partial z_j} = 
\begin{cases}
S_i(1 - S_j) &amp;amp; \text{if}\ i = j \\
-S_iS_j &amp;amp; \text{if}\ i \neq j 
\end{cases}\]

&lt;p&gt;结果可用雅可比矩阵表示：&lt;/p&gt;

\[\frac{\partial S_i}{\partial z_j} = 
 \left[
 \begin{matrix}
   \frac{\partial S_1}{\partial z_1} &amp;amp; \frac{\partial S_1}{\partial z_2} &amp;amp; \cdots &amp;amp;     	\frac{\partial S_1}{\partial z_n}\\
   \frac{\partial S_2}{\partial z_1} &amp;amp; \frac{\partial S_2}{\partial z_2} &amp;amp; \cdots &amp;amp;     	\frac{\partial S_2}{\partial z_n}\\
   \vdots &amp;amp; \vdots &amp;amp; \ddots &amp;amp; \vdots \\
   \frac{\partial S_n}{\partial z_1} &amp;amp; \frac{\partial S_n}{\partial z_2} &amp;amp; \cdots &amp;amp;     	\frac{\partial S_n}{\partial z_n}\\
  \end{matrix}
  \right]
  =
   \left[
 \begin{matrix}
   S_1(1 - S_1) &amp;amp; -S_1S_2 &amp;amp; \cdots &amp;amp; -S_1S_n\\
   -S_2S_1 &amp;amp; S_2(1 - S_2) &amp;amp; \cdots &amp;amp; -S_2S_n\\
   \vdots &amp;amp; \vdots &amp;amp; \ddots &amp;amp; \vdots \\
   -S_nS_1 &amp;amp; -S_nS_2 &amp;amp; \cdots &amp;amp;     	S_n(1 - S_n)\\
  \end{matrix}
  \right]\]

&lt;h3 id=&quot;代价函数&quot;&gt;&lt;strong&gt;代价函数&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;代价函数是神经网络模型优化时的目标，通过最小化代价函数来优化模型。对于不同的任务类型，神经网络模型需要使用不同的代价函数。&lt;/p&gt;

&lt;p&gt;常见的有代价函数有均方差代价函数、对数损失函数、交叉熵等。&lt;/p&gt;

&lt;h4 id=&quot;均方差代价函数&quot;&gt;&lt;strong&gt;均方差代价函数&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;公式：&lt;/p&gt;

\[J_{\theta} = \frac{1}{2m}\sum\limits_{i = 1}^{m}[h_{\theta}(x^{(i)}) - y^{(i)}]^2\]

&lt;p&gt;式中，$h_{\theta}(x^{(i)})$表示对第$i$个样本的预测值，$y^{(i)}$表示该样本对应的真实值。$\frac{1}{2}$的作用是简化后面的求导运算。&lt;/p&gt;

&lt;p&gt;均方差代价函数适用于回归模型。当预测值越接近真实值，代价函数会越接近0。&lt;/p&gt;

&lt;p&gt;计算梯度如下：&lt;/p&gt;

\[\frac{\partial}{\partial\theta_n}J_{\theta} = \sum\limits_{i = 1}^{m}[(h_{\theta}(x^{(i)}) - y^{(i)})x^{(i)}_n]\]

&lt;p&gt;式中，当 $n$ 取值为 (1, n) 时，$\theta_n$对应为$w_n$，$x^{(i)}_n$对应该样本的第 $n$ 个特征。当 $n$ 取值为0时，$\theta_n$即 $\theta_0$ 对应 $b$，取$x^{(i)}_0 = 1$。&lt;/p&gt;

&lt;h4 id=&quot;对数损失函数&quot;&gt;&lt;strong&gt;对数损失函数&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;公式：&lt;/p&gt;

\[Loss(y, P(y | x)) = -logP(y | x)\]

&lt;p&gt;其中：&lt;/p&gt;

\[P(y | x) = 
\begin{cases}
h_\theta(x) &amp;amp; \text{if}\ y = 1 \\
1 - h_\theta(x) &amp;amp; \text{if}\ y = 0 
\end{cases}\]

&lt;p&gt;所以，$Loss$ 函数可简化为：&lt;/p&gt;

\[Loss(y, P(y | x)) = -y * log(h_\theta(x)) - (1 - y) * log(1 - h_\theta(x))\]

&lt;p&gt;即取 y 和 (1 - y) 表示两种情况，y = 1 时，(1 - y) 为0，y = 0 时，(1 - y) 为1。&lt;/p&gt;

&lt;p&gt;对应的代价函数为：&lt;/p&gt;

\[J(\theta) = -\frac{1}{m}\sum\limits_{i = 1}^{m}Loss = -\frac{1}{m}\sum\limits_{i = 1}^{m}[y^{(i)} * log(h_\theta(x^{(i)})) + (1 - y^{(i)}) * log(1 - h_\theta(x^{(i)}))]\]

&lt;p&gt;当 y = 1且 h(x) = 1 时误差为零，且当 h(x) &amp;lt; 1 时Loss随h的减小而增大；当 y = 0 且 h(x) = 0 时误差为零，且当 h(x) &amp;gt; 1 时Loss随h增大而增大。&lt;/p&gt;

&lt;p&gt;对数损失函数适用于二分类问题，即逻辑回归。其本身是由统计学中最大似然估计得出。在其激活函数使用Sigmoid函数的情况下，计算梯度如下：&lt;/p&gt;

\[\frac{\partial}{\partial\theta_n}J_{\theta} = \frac{1}{m}\sum\limits_{i = 1}^{m}[(h_{\theta}(x^{(i)}) - y^{(i)})x^{(i)}_n]\]

&lt;p&gt;此式和均方差代价函数梯度表达式相同，但其中的$h_{\theta}(x^{(i)})$不同，所以两个代价函数不同。&lt;/p&gt;

&lt;h4 id=&quot;交叉熵&quot;&gt;&lt;strong&gt;交叉熵&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;概率:&lt;/p&gt;

\[P(x | \theta)\]

&lt;p&gt;其含义为在已知 $\theta$ 的条件下，最后的结果为 $x$ 的可能性。&lt;/p&gt;

&lt;p&gt;最大似然估计：&lt;/p&gt;

\[L(\theta | x)\]

&lt;p&gt;其含义为在已知结果为 $x$ 的条件下，取环境变量为 $\theta$ 时结果为 $x$ 的可能性。即：&lt;/p&gt;

\[L(\theta | x) = P(x | \theta)\]

&lt;p&gt;对于多个可能的结果，最大似然估计的表达式如下：&lt;/p&gt;

\[L(\theta) = P(x_1, x_2, ..., x_m | \theta) = p(x_1 | \theta)p(x_2 | \theta)...p(x_m | \theta) = \prod\limits_{i = 1}^{m}P(x_i | \theta)\]

&lt;p&gt;根据定义可知，概率和最大似然估计的区别为：$L$ 是关于 $\theta$ 的函数，$P$ 是关于 $x$ 的函数。在神经网络中，我们需要根据已知事件来找出产生这种结果的最有可能的条件，目的是根据这个最有可能的条件去推测未知事件。最大似然函数在现有数据的条件下，通过调整环境变量 $\theta$ 来获取最大可能性，最后产生的结果即可用于对未知数据的预测。&lt;/p&gt;

&lt;p&gt;为便于计算，用 $log​$ 函数将最大似然估计中的累乘转化为求和：&lt;/p&gt;

\[log(L(\theta)) = log(\prod\limits_{i = 1}^{m}P(x_i | \theta)) = \sum\limits_{i = 1}^{m}log(P(x_i | \theta))\]

&lt;p&gt;观察 $log$ 函数图像可知，当 $L(\theta) = 1$ 时，$log(L(\theta)) = 0$；当 $L(\theta) \rightarrow 0 $ 时，$log(L(\theta)) \rightarrow -\infty$。&lt;/p&gt;

&lt;p&gt;我们需要优化 $\theta$ 使得 $L(\theta)​$ 尽可能接近1。即最大化似然。&lt;/p&gt;

&lt;p&gt;另一种解释最大似然估计的观点是将它看作最小化经验分布（即真实情况下数据分布，$p_{data}$）和模型分布（即预测的分布，$p_{model}​$）之间的差异，两者之间的差异程度可以通过KL散度度量。定义如下：&lt;/p&gt;

\[D_{KL}(p_{data}||p_{model}) = \frac{1}{m}\sum\limits_{i = 1}^{m} \{ p_{data}(x^{(i)}) [log(p_{data}(x^{(i)})) - log(p_{model}(x^{(i)}))] \}\]

&lt;p&gt;$p_{data}$ 表示真实情况下的数据分布概论，与模型无关。这意味着当训练模型最小化KL散度时，我们只需要最小化：&lt;/p&gt;

\[-\frac{1}{m}\sum\limits_{i = 1}^{m} [p_{data}(x^{(i)}) log(p_{model}(x^{(i)}))]\]

&lt;p&gt;所以定义损失函数如下：&lt;/p&gt;

\[Loss(\theta) = -\frac{1}{m}\sum\limits_{i = 1}^{m}[y^{(i)}log(P(x^{(i)} | \theta))]\]

&lt;p&gt;最小化KL散度其实就是在最小化分布之间的交叉熵。&lt;/p&gt;

&lt;p&gt;实际计算时，为简化计算过程，取：&lt;/p&gt;

\[P(x^{(i)} | \theta) = 
\begin{cases}
h_\theta(x_j) &amp;amp; \text{if}\ y_k = 1 \\
1 - h_\theta(x_j) &amp;amp; \text{if}\ y_k = 0 
\end{cases}\]

&lt;p&gt;则：&lt;/p&gt;

\[Loss(\theta) = -\frac{1}{m}\sum\limits_{i = 1}^{m}[y^{(i)}log(P(x^{(i)} | \theta))] = -\frac{1}{m}\sum\limits_{i = 1}^{m}\sum\limits_{k = 1}^{n}[y^{(i)}_klog(h_{\theta}(x_k^{(i)})]\]

&lt;p&gt;可以看出，逻辑回归中的对数损失函数是交叉熵取 $n = 2$ 即只有两个分类时的特例。&lt;/p&gt;

&lt;h3 id=&quot;反向传播&quot;&gt;&lt;strong&gt;反向传播&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;反向传播过程也和逻辑回归过程很相似，需要通过代价函数和梯度下降来优化每一层的参数。不同的是，由于神经网络每一层相对独立，且层数较多，不能直接用代价函数对所有参数进行求导。所以需要使用高等数学中的链式法则，对每一层分别求导。&lt;/p&gt;

&lt;p&gt;使用在前向传播过程中构建的神经网络：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;设数据中共有$m​$个样本，每个样本有$n^{[0]}​$个特征。建立一个 $l​$ 层的神经网络。每一层的特征数（神经元个数）用$n​$表示，即为 $(n^{[1]}, n^{[2]}, …, n^{[l]})​$。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;前向传播过程如下：&lt;/p&gt;

\[(X, \theta^{[1]}) \Rightarrow z^{[1]} = XW^{[1]} + b^{[1]T}\Rightarrow \alpha^{[1]} = \sigma(z^{[1]})\]

\[(\sigma(z^{[1]}, \theta^{[2]}) \Rightarrow z^{[2]} = \sigma^{[2]}W^{[2]} + b^{[2]T}\Rightarrow \alpha^{[2]} = \sigma(z^{[2]})\]

\[\vdots\]

\[(\sigma(z^{[l-1]}, \theta^{[l]}) \Rightarrow z^{[l]} = \sigma^{[l]}W^{[l]} + b^{[l]T}\Rightarrow \alpha^{[l]} = \sigma(z^{[l]})\]

&lt;p&gt;以 $X$ 作为输入数据，$\sigma(z^{[l]})$即为最后的预测值。&lt;/p&gt;

&lt;p&gt;使用代价函数计算预测值和真实值之间的差距，代价函数表示为 $J(\theta)​$。&lt;/p&gt;

&lt;p&gt;则用链式法则计算反向传播过程如下：&lt;/p&gt;

&lt;p&gt;计算梯度：&lt;/p&gt;

\[\frac{\partial J(\theta)}{\partial z^{[l]}} = \frac{\partial J(\theta)}{\partial \sigma(z^{[l]})} \frac{\partial \sigma(z^{[l]})}{\partial z^{[l]}}\]

\[\frac{\partial J(\theta)}{\partial \theta^{[l]}} = \frac{\partial J(\theta)}{\partial z^{[l]}} \frac{\partial z^{[l]}}{\partial \theta^{[l]}}\]

\[\frac{\partial J(\theta)}{\partial z^{[l - 1]}} = \frac{\partial J(\theta)}{\partial z^{[l]}} \frac{\partial z^{[l]}}{\partial \sigma(z^{[l - 1]})} \frac{\partial \sigma(z^{[l - 1]})}{\partial z^{[l - 1]}}\]

\[\frac{\partial J(\theta)}{\partial \theta^{[l - 1]}} = \frac{\partial J(\theta)}{\partial z^{[l - 1]}} \frac{\partial z^{[l - 1]}}{\partial \theta^{[l]}}\]

\[\vdots\]

\[\frac{\partial J(\theta)}{\partial z^{[1]}} = \frac{\partial J(\theta)}{\partial z^{[2]}} \frac{\partial z^{[2]}}{\partial \sigma(z^{[1]})} \frac{\partial \sigma(z^{[1]})}{\partial z^{[1]}}\]

\[\frac{\partial J(\theta)}{\partial \theta^{[1]}} = \frac{\partial J(\theta)}{\partial z^{[1]}} \frac{\partial z^{[1]}}{\partial \theta^{[1]}}\]

&lt;p&gt;完成全部梯度的计算以后更新参数：&lt;/p&gt;

\[\theta^{[l]} := \theta^{[l]} - \alpha \frac{\partial J(\theta)}{\partial \theta^{[l]}}\]

\[\theta^{[l - 1]} := \theta^{[l - 1]} - \alpha \frac{\partial J(\theta)}{\partial \theta^{[l - 1]}}\]

\[\vdots\]

\[\theta^{[1]} := \theta^{[1]} - \alpha \frac{\partial J(\theta)}{\partial \theta^{[1]}}\]

&lt;p&gt;其中，所有数据的导数的维度和数据自身的维度相同。&lt;/p&gt;</content><author><name>Wyundi</name></author><category term="机器学习" /><category term="神经网络" /><summary type="html">全连接神经网络基础及相关数学计算。</summary></entry><entry><title type="html">Machine Learning: 逻辑回归</title><link href="https://wyundi.github.io/2019/03/15/Machine-Learning-%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92/" rel="alternate" type="text/html" title="Machine Learning: 逻辑回归" /><published>2019-03-15T11:34:37+00:00</published><updated>2019-03-15T11:34:37+00:00</updated><id>https://wyundi.github.io/2019/03/15/Machine-Learning-%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92</id><content type="html" xml:base="https://wyundi.github.io/2019/03/15/Machine-Learning-%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92/">&lt;p&gt;以乳腺癌分类预测为例介绍机器学习中的逻辑回归。&lt;/p&gt;

&lt;h1 id=&quot;逻辑回归logistic-regression&quot;&gt;&lt;strong&gt;逻辑回归（Logistic Regression）&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;参考：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.kongfy.com/2014/11/machine-learning%E5%B0%8F%E7%BB%931%EF%BC%9A%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E3%80%81%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92%E5%92%8C%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/&quot;&gt;Machine Learning小结(1)：线性回归、逻辑回归和神经网络&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/28408516&quot;&gt;逻辑回归（Logistic Regression）（一）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/25577671&quot;&gt;机器学习之对数几率回归(Logistic Regression)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://study.163.com/course/courseMain.htm?courseId=1004570029&quot;&gt;吴恩达机器学习&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mooc.study.163.com/learn/2001281002?tid=2001392029&amp;amp;_trace_c_p_k2_=db6bf4087fc04a09aa4143e4e92e172a&quot;&gt;深度学习工程师_01.神经网络和深度学习&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.ituring.com.cn/book/2661&quot;&gt;卷积神经网络的Python实现&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_breast_cancer.html&quot;&gt;sklearn.datasets.load_breast_cancer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;环境：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;操作系统：Ubuntu 18.04&lt;/p&gt;

&lt;p&gt;编程语言：Python 3.6.7&lt;/p&gt;

&lt;p&gt;第三方库：numpy\matplotlib\scikit-learn&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;imp&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;					&lt;span class=&quot;c1&quot;&gt;# 科学计算
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pylab&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;				&lt;span class=&quot;c1&quot;&gt;# 绘图
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sklearn&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datasets&lt;/span&gt;				&lt;span class=&quot;c1&quot;&gt;# 数据集
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sklearn.model_selection&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_test_split&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;# sklearn中的数据分割工具
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;定义&quot;&gt;&lt;strong&gt;定义&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;是一种用来解决分类问题的机器学习方法，用于估计某种事物的可能性。逻辑回归是一种广义线性回归，因此与线性回归分析有很多相同之处。他们的模型形式基本相同，都具有 wx + b ，其中w和b是待求参数。其区别在于他们的因变量不同，线性回归直接将 wx + b 作为因变量，即 h = wx + b ，而逻辑回归则通过逻辑函数将 wx + b 映射为一个非线性的特征p，表示某一分类的可能性，并将其作为因变量。逻辑回归的公式表示为：&lt;/p&gt;

\[h_\theta(x) = P(y = 1|x; \theta)\]

&lt;p&gt;即在给定自变量及其参数的情况下，y = 1的概率。&lt;/p&gt;

&lt;p&gt;如果采用常函数作为逻辑函数，则该模型即为线性回归模型。在机器学习中，将这个逻辑函数称为激活函数。常用的激活函数有sigmoid、Tanh、ReLU、softmax等。&lt;/p&gt;

&lt;h2 id=&quot;数据处理&quot;&gt;&lt;strong&gt;数据处理&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;采用sklearn乳腺癌数据集：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;cancer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datasets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load_breast_cancer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;data_X&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cancer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;data_y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cancer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;乳腺癌数据集一共包括30个特征，target是诊断结果，即是否患有乳腺癌。将数据分为训练及和测试集，选择第1个特征和第27个特征作为自变量，target作为因变量。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_test_split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;test_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;前向传播&quot;&gt;&lt;strong&gt;前向传播&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;调用matplotlib对所选数据进行绘图，所有 y = 0 的点将其表为绿色，y = 1 的点将其标为红色。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'green'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'red'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning2_LogisticRegression/x&amp;amp;y.png&quot; alt=&quot;x&amp;amp;y&quot; /&gt;&lt;/p&gt;

&lt;p&gt;如果要对这组数据进行分类，就需要有一个这样的函数：值在0和1之间，当该函数大于某一个值时，判断其为1，小于时判断其为0。逻辑回归模型的假设是：&lt;/p&gt;

\[h(x) = g(\theta^Tx)\]

&lt;p&gt;其中，$\theta^Tx$表示其决策边界，即两类数据的分隔边界。g代表逻辑函数。对于二分类问题，这里使用一个常见的逻辑函数：Sigmoid Function，公式为：&lt;/p&gt;

\[g(z) = \frac{1}{1 + e^{-z}}\]

&lt;p&gt;python代码实现：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sigmoid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;该函数的图像为:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning2_LogisticRegression/sigmoid.png&quot; alt=&quot;sigmoid&quot; /&gt;&lt;/p&gt;

&lt;p&gt;h(x) 的作用是，对于给定的输入变量，根据选择的参数计算 y = 1 的可能性。当 z &amp;gt; 0，g(z) &amp;gt; 0.5，预测 y = 1；当 z &amp;lt; 0，g(z) &amp;lt; 0.5，预测 y = 0。&lt;/p&gt;

&lt;p&gt;由于我们采用了两个自变量x1和x2，所以逻辑函数为：&lt;/p&gt;

\[h(x) = g(\theta^Tx) = g(w^Tx + b) = g(w_1x_1 + w_2x_2 + b)\]

&lt;p&gt;为简化计算量，可以将x1x2和w1w2分别组合成矩阵，然后通过numpy进行运算。&lt;/p&gt;

\[z(x) = w^Tx + b = 
 \left[
 \begin{matrix}
   x_{11} &amp;amp; x_{21}\\
   x_{12} &amp;amp; x_{22}\\
   \vdots &amp;amp; \vdots\\
   x_{1m} &amp;amp; x_{2m}
  \end{matrix}
  \right]
=
 \left[
 \begin{matrix}
   w_1\\
   w_2\\
 \end{matrix}
 \right]
+ b\]

\[h(x) = g(\theta^Tx) = sigmoid(z)\]

&lt;p&gt;训练集共包含540个样本，将x1和x2合并成一个 (540, 2) 的矩阵，并使用随机数初始化参数w和b，然后计算h，并取m作为样本个数：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;column_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sigmoid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;反向传播&quot;&gt;&lt;strong&gt;反向传播&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;反向传播过程需要通过损失函数和代价函数来优化参数。对于线性回归模型，我们使用最小二乘法定义了损失函数。理论上来说，我们也可以用它定义逻辑回归模型的损失函数，但将sigmoid函数带入代价函数时，对w和J作图，将得到一个非凸函数。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning2_LogisticRegression/Screenshot from 2019-03-14 18-57-40.png&quot; alt=&quot;Screenshot from 2019-03-14 18-57-40&quot; /&gt;&lt;/p&gt;

&lt;p&gt;这意味着我们的代价函数有许多局部最小值，这将影响梯度下降算法寻找全局最小值。&lt;/p&gt;

&lt;p&gt;线性回归的代价函数为：&lt;/p&gt;

\[J(\theta) = \frac{1}{2m}\sum\limits_{i = 1}^{m}[h_\theta(x^i), y^i]^2\]

&lt;p&gt;重新定义逻辑回归的代价函数：&lt;/p&gt;

\[J(\theta) = \frac{1}{m}\sum\limits_{i = 1}^{m}Loss(h_\theta(x^i), y^i)\]

&lt;p&gt;其中：&lt;/p&gt;

\[Loss(h_\theta(x^i), y^i) = 
\begin{cases}
log(h_\theta(x)) &amp;amp; \text{if}\ y = 1 \\
log(1 - h_\theta(x)) &amp;amp; \text{if}\ y = 0 
\end{cases}\]

&lt;p&gt;h与Loss( h(x), y ) 的关系如下图：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning2_LogisticRegression/Screenshot from 2019-03-14 19-27-03.png&quot; alt=&quot;Screenshot from 2019-03-14 19-27-03&quot; /&gt;&lt;/p&gt;

&lt;p&gt;这样构建的 Loss( h(x), y ) 函数的特点是：当 y = 1且 h(x) = 1 时误差为零，且当 h(x) &amp;lt; 1 时Loss随h的减小而增大；当 y = 0 且 h(x) = 0 时误差为零，且当 h(x) &amp;gt; 1 时Loss随h增大而增大。&lt;/p&gt;

&lt;p&gt;上式可以简化为：&lt;/p&gt;

\[Loss(h_\theta(x^i), y^i) = -y * log(h_\theta(x)) - (1 - y) * log(1 - h_\theta(x))\]

&lt;p&gt;即取 y 和 (1 - y) 表示两种情况，y = 1 时，(1 - y) 为0，y = 0 时，(1 - y) 为1。&lt;/p&gt;

&lt;p&gt;python代码实现：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;Loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;J&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;得到代价函数以后，使用梯度下降算法来优化参数。算法为：&lt;/p&gt;

&lt;p&gt;repeat until convergence {&lt;/p&gt;

\[\theta := \theta - \alpha\frac{\partial}{\partial \theta}{J(\theta)}\]

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;求导后得：&lt;/p&gt;

&lt;p&gt;repeat until convergence {&lt;/p&gt;

\[\theta := \theta - \alpha\frac{1}{m}\sum\limits_{i = 1}^{m}\{[h_\theta(x^i) - y^i]x^i\}\]

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;推导过程：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning2_LogisticRegression/Screenshot from 2019-03-15 10-16-02.png&quot; alt=&quot;Screenshot from 2019-03-15 10-16-02&quot; /&gt;&lt;/p&gt;

&lt;p&gt;这个结果看起来是和线性回归梯度下降的结果是一样的，但由于其中假设的定义（h）发生了变化，所以逻辑回归的梯度下降跟线性回归的梯度下降完全不同。&lt;/p&gt;

&lt;p&gt;逻辑回归的梯度下降过程为：&lt;/p&gt;

&lt;p&gt;repeat until convergence {&lt;/p&gt;

\[w := w - \alpha\frac{1}{m}\sum\limits_{i = 1}^{m}\{[h(x^i) - y^i]x^i\}\]

\[b := b - \alpha\frac{1}{m}\sum\limits_{i = 1}^{m}[h(x^i) - y^i]\]

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;注意：先完成计算然后同步更新所有参数。&lt;/p&gt;

&lt;p&gt;python代码为：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;J0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;J&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;J_dv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;J0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;J&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.03&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Hypothesis
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sigmoid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# CostFunction
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;J0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;J&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;J&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;J_dv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;J0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;J&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Derivative
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;dw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# ParamUpdate
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dw&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;J&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;J_dv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.00000001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用matplotlib绘图：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;x1_t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;21.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x2_t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x1_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x1_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x2_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'blue'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'green'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'red'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning2_LogisticRegression/result.png&quot; alt=&quot;result&quot; /&gt;&lt;/p&gt;

&lt;p&gt;结果基本符合训练集数据。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning2_LogisticRegression/testResult.png&quot; alt=&quot;testResult&quot; /&gt;&lt;/p&gt;

&lt;p&gt;也基本符合测试集数据分布。&lt;/p&gt;</content><author><name>Wyundi</name></author><category term="机器学习" /><summary type="html">以乳腺癌分类预测为例介绍机器学习中的逻辑回归。</summary></entry><entry><title type="html">Machine Learning: 线性回归</title><link href="https://wyundi.github.io/2019/03/11/Machine-Learning-%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92/" rel="alternate" type="text/html" title="Machine Learning: 线性回归" /><published>2019-03-11T14:51:00+00:00</published><updated>2019-03-11T14:51:00+00:00</updated><id>https://wyundi.github.io/2019/03/11/Machine-Learning-%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92</id><content type="html" xml:base="https://wyundi.github.io/2019/03/11/Machine-Learning-%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92/">&lt;p&gt;以波士顿房价预测为例介绍机器学习中的线性回归。&lt;/p&gt;

&lt;h1 id=&quot;线性回归linear-regression&quot;&gt;&lt;strong&gt;线性回归（Linear Regression）&lt;/strong&gt;&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;参考：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.kongfy.com/2014/11/machine-learning%E5%B0%8F%E7%BB%931%EF%BC%9A%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92%E3%80%81%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92%E5%92%8C%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/&quot;&gt;Machine Learning小结(1)：线性回归、逻辑回归和神经网络&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://zhuanlan.zhihu.com/p/25434586&quot;&gt;机器学习之线性回归(linear regression)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.cnblogs.com/pinard/p/5970503.html&quot;&gt;梯度下降（Gradient Descent）小结&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://study.163.com/course/courseMain.htm?courseId=1004570029&quot;&gt;吴恩达机器学习&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://mooc.study.163.com/learn/2001281002?tid=2001392029&amp;amp;_trace_c_p_k2_=db6bf4087fc04a09aa4143e4e92e172a&quot;&gt;深度学习工程师_01.神经网络和深度学习&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.ituring.com.cn/book/2661&quot;&gt;卷积神经网络的Python实现&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_boston.html&quot;&gt;sklearn.datasets.load_boston&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;环境：&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;操作系统：Ubuntu 18.04&lt;/p&gt;

&lt;p&gt;编程语言：Python 3.6.7&lt;/p&gt;

&lt;p&gt;第三方库：numpy\matplotlib\scikit-learn&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;imp&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;					&lt;span class=&quot;c1&quot;&gt;# 科学计算
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pylab&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;				&lt;span class=&quot;c1&quot;&gt;# 绘图
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sklearn&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datasets&lt;/span&gt;				&lt;span class=&quot;c1&quot;&gt;# 数据集
&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sklearn.model_selection&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_test_split&lt;/span&gt;	&lt;span class=&quot;c1&quot;&gt;# sklearn中的数据分割工具
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;定义&quot;&gt;&lt;strong&gt;定义&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;给定数据集D={(x1, y1), (x2, y2), … }，我们试图从此数据集中学习得到一个线性模型，这个模型尽可能准确地反应x(i)和y(i)的对应关系。这里的线性模型，就是属性(x)的线性组合的函数，可表示为：&lt;/p&gt;

\[f(x) = w_1x_1 + w_2x_2 + ... + w_nx_n + b\]

&lt;p&gt;向量表示为：&lt;/p&gt;

\[f(x) = W^TX + b\]

&lt;p&gt;其中，W是由(w1, w2, …, wn)组成的列向量，表示weight，即权重，b表示bias，即偏差。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning1_LinearnRegression/v2-51c619a7179c07e38b60ee8a7a0cd630_1200x500.jpg&quot; alt=&quot;v2-51c619a7179c07e38b60ee8a7a0cd630_1200x500&quot; /&gt;&lt;/p&gt;

&lt;p&gt;在机器学习中，我们希望可以建立一个函数，使其尽可能符合现有的数据分布，这样我们就可以通过这个函数对未知的数据进行预测。线性回归需要解决的问题就是如何求得参数W和b，使函数获得最优解，即使其预测结果更接近真实值。&lt;/p&gt;

&lt;h3 id=&quot;数据处理&quot;&gt;&lt;strong&gt;数据处理&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;首先我们需要获取数据集，然后根据数据集的分布确定我们的预测函数。&lt;/p&gt;

&lt;p&gt;采用sklearn的波士顿房价数据集：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;boston&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datasets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load_boston&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;data_X&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;boston&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;data_y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;boston&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;波士顿房价数据集中data一共包含13个特征，target是最后的房价。通过以下代码可以查看其各项属性：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;boston&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DESCR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;pre&gt;&lt;code class=&quot;language-reStructuredText&quot;&gt;.. _boston_dataset:

Boston house prices dataset
---------------------------

**Data Set Characteristics:**  

 :Number of Instances: 506 

 :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target.

 :Attribute Information (in order):
        - CRIM     per capita crime rate by town
        - ZN       proportion of residential land zoned for lots over 25,000 sq.ft.
        - INDUS    proportion of non-retail business acres per town
        - CHAS     Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
        - NOX      nitric oxides concentration (parts per 10 million)
        - RM       average number of rooms per dwelling
        - AGE      proportion of owner-occupied units built prior to 1940
        - DIS      weighted distances to five Boston employment centres
        - RAD      index of accessibility to radial highways
        - TAX      full-value property-tax rate per $10,000
        - PTRATIO  pupil-teacher ratio by town
        - B        1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town
        - LSTAT    % lower status of the population
        - MEDV     Median value of owner-occupied homes in $1000's

 :Missing Attribute Values: None

 :Creator: Harrison, D. and Rubinfeld, D.L.

This is a copy of UCI ML housing dataset.
https://archive.ics.uci.edu/ml/machine-learning-databases/housing/


This dataset was taken from the StatLib library which is maintained at Carnegie Mellon University.

The Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic
prices and the demand for clean air', J. Environ. Economics &amp;amp; Management,
vol.5, 81-102, 1978.   Used in Belsley, Kuh &amp;amp; Welsch, 'Regression diagnostics
...', Wiley, 1980.   N.B. Various transformations are used in the table on
pages 244-261 of the latter.

The Boston house-price data has been used in many machine learning papers that address regression
problems.   
  
.. topic:: References

   - Belsley, Kuh &amp;amp; Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261.
   - Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;为了便于测试模型的准确性，训练出准确性最好，泛化程度最高的模型，我们需要将模型分割成训练集、验证集和测试集。方便起见，在此我们仅将其分为训练集和测试集。使用sklearn的split工具分割数据：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_test&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_test_split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data_X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;test_size&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.05&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;train_test_split将数据集随机分为了训练集和测试集两部分，分别占原数据集的95%和5%。后面将使用这两个数据集训练模型并对其进行检测。&lt;/p&gt;

&lt;p&gt;选择数据集中第6个特征作为自变量，target作为因变量。根据上面的属性可以看出，该特征为average number of rooms per dwelling，即住所平均房间数。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x_train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y_train&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;前向传播&quot;&gt;&lt;strong&gt;前向传播&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;调用matplotlib对所选数据进行绘图：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning1_LinearnRegression/RomeNum.png&quot; alt=&quot;RomeNum&quot; /&gt;&lt;/p&gt;

&lt;p&gt;可以看出，数据近似为一个一次函数。因此我们使用以下函数预测数据：
\(h(x) = wx + b\)
如果数据分布近似二次函数或其他类型函数，就需要改变预测函数的形式，使其符合数据分布规律。此函数中，h(x)即为该函数预测的房价，x为房间数，w和b是该函数的参数。采用随机值为w和b赋值，可产生第一个预测的函数：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;（&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;，&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;）&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;x，w和b都是矩阵格式的数据。b和h的维度不一致但也可以进行运算，这是使用了numpy中的广播机制。h即为使用w和b作为参数预测的房价。对其进行绘图：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'red'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning1_LinearnRegression/prediction1.png&quot; alt=&quot;prediction1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;可以看出，此时预测的函数偏差还很大，不能作为最后的结果。&lt;/p&gt;

&lt;h3 id=&quot;反向传播&quot;&gt;&lt;strong&gt;反向传播&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;反向传播过程需要通过数据集中的target来优化现在的预测函数，使它的预测值尽可能接近真实值。&lt;/p&gt;

&lt;p&gt;定义每一个样本的预测结果与真实值之间的偏差的表达式为损失函数（Loss Function），所有损失函数的和为代价函数（Cost Function）。当Cost Function接近最小值，我们就可以认为函数预测的结果已经接近真实值。在线性回归中，使用最小二乘法来定义其损失函数：&lt;/p&gt;

\[Loss = [h(x^i) - y^i]^2\]

&lt;p&gt;Cost Function是所有Loss的和：&lt;/p&gt;

\[J = \frac{1}{2m}\sum\limits_{i = 1}^{m} Loss = \frac{1}{2m}\sum\limits_{i = 1}^{m} [h(x^i) - y^i]^2\]

&lt;p&gt;式中m是样本数量。当J接近0的时候，h(x) 近似等于 y，可以认为该函数的预测值接近真实值。1/2m的作用是简化后面的求导运算。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;J&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;power&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;完成以上定义以后，我们就可以通过优化w和b来使J接近0，即可获得最佳的预测函数。&lt;/p&gt;

&lt;h4 id=&quot;梯度下降gradient-descent&quot;&gt;&lt;strong&gt;梯度下降（Gradient Descent）&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;结合预测函数h和代价函数J，我们可以得到以下表达式：&lt;/p&gt;

\[J = \frac{1}{2m}\sum\limits_{i = 1}^{m} [h(x^i) - y^i]^2 = \frac{1}{2m}\sum\limits_{i = 1}^{m} [wx^i + b - y^i]^2\]

&lt;p&gt;先简化上式，假设b = 0，则该表达式就变成了：&lt;/p&gt;

\[J = \frac{1}{2m}\sum\limits_{i = 1}^{m} [wx^i - y^i]^2\]

&lt;p&gt;可以看出，现在J的大小只受w的影响。设w为自变量J为因变量，可画出以下图像：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;J&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;power&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;axis&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w_t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;J&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning1_LinearnRegression/w&amp;amp;J.png&quot; alt=&quot;w&amp;amp;J&quot; /&gt;&lt;/p&gt;

&lt;p&gt;任取一个w作为预测函数的初始参数，该参数对应着图像上的一个点。在该点用J对w求导，得到的结果就是梯度，从他的几何意义上来说，就是函数增长的方向。因此，要想使该点到达函数的局部/全局最优点，就需要让w减去这个梯度，使该点沿着函数值减小的方向前进。这就是梯度下降。公式为：&lt;/p&gt;

&lt;p&gt;&lt;em&gt;repeat until convergence {&lt;/em&gt;&lt;/p&gt;

\[\theta := \theta - \alpha\frac{\partial}{\partial \theta}{J(\theta)}\]

&lt;p&gt;&lt;em&gt;}&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;其中，θ是参数，对应预测函数中的w（和b），α是学习率。这个公式的含义就是让θ每次沿梯度下降的方向前进一点，直到函数收敛为止。学习率决定了每次前进的步长，如果学习率太小，训练过程会很慢；如果学习率过大，可能会使参数直接跨过最小值点，导致最后的结果无法收敛。另外，由于随着CostFunction趋近于最小值，函数的梯度逐渐减小，θ前进的步长也会逐渐减小，所以不需要随着训练过程改变α的值。&lt;/p&gt;

&lt;p&gt;由于线性回归的CostFunction没有局部最优解，所以梯度下降的结果一定是全局最优解。&lt;/p&gt;

&lt;p&gt;最小二乘法的代价函数的导数可以化简为如下形式：&lt;/p&gt;

\[\frac{\partial}{\partial w}{J(w)} = \frac{1}{m}\sum\limits_{i = 1}^{m}\{[h(x^i) - y^i]x^i\}\]

\[\frac{\partial}{\partial b}{J(b)} = \frac{1}{m}\sum\limits_{i = 1}^{m}[h(x^i) - y^i]\]

&lt;p&gt;所以梯度下降的过程为：&lt;/p&gt;

&lt;p&gt;&lt;em&gt;repeat until convergence {&lt;/em&gt;&lt;/p&gt;

\[w := w - \alpha\frac{1}{m}\sum\limits_{i = 1}^{m}\{[h(x^i) - y^i]x^i\}\]

\[b := b - \alpha\frac{1}{m}\sum\limits_{i = 1}^{m}[h(x^i) - y^i]\]

&lt;p&gt;}*&lt;/p&gt;

&lt;p&gt;由于更新参数会导致h发生变化，从而影响后面参数的更新过程，所以需要先完成计算然后同步更新所有参数。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;J0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;J&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;J_dv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;J0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;J&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.03&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# Hypothesis
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# CostFunction
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;J0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;J&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;J&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;power&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;J_dv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;J0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;J&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# Derivative
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;dw&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;# ParamUpdate
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dw&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;J_dv&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.00001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;使用参数更新前后CostFunction的差值作为判断函数收敛的标志，当差值小于0.00001以后，可以认为函数的梯度为近似为0，函数收敛。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning1_LinearnRegression/result.png&quot; alt=&quot;result&quot; /&gt;&lt;/p&gt;

&lt;p&gt;经过训练以后，预测函数已经有了比较好的效果，基本符合实际数据。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/MachineLearning1_LinearnRegression/testResult.png&quot; alt=&quot;testResult&quot; /&gt;&lt;/p&gt;

&lt;p&gt;同时，在测试集上，该预测函数同样也基本符合数据分布。&lt;/p&gt;</content><author><name>Wyundi</name></author><category term="机器学习" /><summary type="html">以波士顿房价预测为例介绍机器学习中的线性回归。</summary></entry><entry><title type="html">《梭罗和他的湖》</title><link href="https://wyundi.github.io/2019/02/27/%E7%93%A6%E5%B0%94%E7%99%BB%E6%B9%96%E4%B9%A6%E8%AF%84/" rel="alternate" type="text/html" title="《梭罗和他的湖》" /><published>2019-02-27T00:42:32+00:00</published><updated>2019-02-27T00:42:32+00:00</updated><id>https://wyundi.github.io/2019/02/27/%E7%93%A6%E5%B0%94%E7%99%BB%E6%B9%96%E4%B9%A6%E8%AF%84</id><content type="html" xml:base="https://wyundi.github.io/2019/02/27/%E7%93%A6%E5%B0%94%E7%99%BB%E6%B9%96%E4%B9%A6%E8%AF%84/">&lt;p&gt;瓦尔登湖书评&lt;/p&gt;

&lt;h2 id=&quot;梭罗和他的湖&quot;&gt;《梭罗和他的湖》&lt;/h2&gt;

&lt;p&gt;何怀宏&lt;/p&gt;

&lt;p&gt;十&lt;/p&gt;

&lt;p&gt;…&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;他明确地说他希望世界上的人，越不相同越好。但他愿意每一个人都能谨慎地找出并坚持他自己的合适的方式，而不要简单地因袭和模仿他父亲的、或母亲的、或邻居的生活方式。他是一个天生的倡异议者，对每一个建议本能的反应是说“不”。而现在有什么人愿意做人中的黄蜂呢？人们更喜欢在互相恭维的泥淖中打滚。&lt;/p&gt;

  &lt;p&gt;他的善意和同情并不表现为顺从别人，他的坚定和明智也不要求别人的顺从。他要自己绝对自主，也要每一个人都绝对自主。可是一个人仍然可以在这种意义上成为一个和他一样的人：即成为一个与任何其他人（当然也包括梭罗）不同的人，成为一个可以说这一句话的人 —&lt;/p&gt;

  &lt;p&gt;我是我自己。&lt;/p&gt;
&lt;/blockquote&gt;</content><author><name>Wyundi</name></author><category term="笔记" /><summary type="html">瓦尔登湖书评</summary></entry><entry><title type="html">Shadowsocks（二）：客户端配置</title><link href="https://wyundi.github.io/2019/02/23/Shadowsocks-%E4%BA%8C-%E5%AE%A2%E6%88%B7%E7%AB%AF%E9%85%8D%E7%BD%AE/" rel="alternate" type="text/html" title="Shadowsocks（二）：客户端配置" /><published>2019-02-23T00:48:22+00:00</published><updated>2019-02-23T00:48:22+00:00</updated><id>https://wyundi.github.io/2019/02/23/Shadowsocks(%E4%BA%8C):%E5%AE%A2%E6%88%B7%E7%AB%AF%E9%85%8D%E7%BD%AE</id><content type="html" xml:base="https://wyundi.github.io/2019/02/23/Shadowsocks-%E4%BA%8C-%E5%AE%A2%E6%88%B7%E7%AB%AF%E9%85%8D%E7%BD%AE/">&lt;p&gt;Ubuntu环境下使用Shadowsocks科学上网。使用Vultr服务器搭建SS服务端，并在本地配置网络代理。&lt;/p&gt;

&lt;p&gt;注：本机操作系统为 Ubuntu 18.04，服务器的搭建请参考上一篇文章。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://wyundi.github.io/2019/02/22/Shadowsocks%EF%BC%88%E4%B8%80%EF%BC%89%EF%BC%9A%E5%9F%BA%E4%BA%8EVultr%E6%90%AD%E5%BB%BASS%E6%9C%8D%E5%8A%A1%E5%99%A8/&quot;&gt;Shadowsocks（一）：基于Vultr搭建SS服务器&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;参考&quot;&gt;&lt;strong&gt;参考：&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.huihut.com/2017/08/25/LinuxInstallConfigShadowsocksClient/&quot;&gt;Linux安装配置Shadowsocks客户端及开机自动启动&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.flyzy2005.com/tech/switchyomega-proxy-server/&quot;&gt;用SwitchyOmega管理代理设置&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.codelast.com/%E5%8E%9F%E5%88%9B-ubuntu%E7%BB%88%E7%AB%AF%E4%BD%BF%E7%94%A8privoxy%E4%BB%A3%E7%90%86/&quot;&gt;Ubuntu终端使用Privoxy代理&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;目录&quot;&gt;&lt;strong&gt;目录&lt;/strong&gt;：&lt;/h4&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;
      &lt;p&gt;Ubuntu环境下网络代理配置&lt;/p&gt;

      &lt;ul&gt;
        &lt;li&gt;
          &lt;p&gt;Shadowsocks无GUI客户端配置&lt;/p&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;p&gt;SHadowsocks_Qt5客户端配置&lt;/p&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;p&gt;Chrome浏览器网络代理配置&lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;p&gt;终端网络代理配置&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;1-ubuntu环境下网络代理配置&quot;&gt;&lt;strong&gt;1. Ubuntu环境下网络代理配置&lt;/strong&gt;&lt;/h2&gt;

&lt;h3 id=&quot;shadowsocks无gui客户端配置&quot;&gt;Shadowsocks无GUI客户端配置&lt;/h3&gt;

&lt;h4 id=&quot;安装&quot;&gt;&lt;strong&gt;安装&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;安装Shadowsocks客户端需要python及其包管理工具pip，通过以下命令可以查看python和pip的版本：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;python &lt;span class=&quot;nt&quot;&gt;--version&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pip &lt;span class=&quot;nt&quot;&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;确定Python和pip都已经正确安装后，使用以下命令安装Shadowsocks客户端：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;shadowsocks
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;配置&quot;&gt;&lt;strong&gt;配置&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;创建Shadowsocks配置文件：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo touch&lt;/span&gt; /etc/shadowsocks/config.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后在该配置文件中添加服务器信息：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;s2&quot;&gt;&quot;server&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;my_server_ip&quot;&lt;/span&gt;,

    &lt;span class=&quot;s2&quot;&gt;&quot;server_port&quot;&lt;/span&gt;:my_server_port,

    &lt;span class=&quot;s2&quot;&gt;&quot;local_address&quot;&lt;/span&gt;: &lt;span class=&quot;s2&quot;&gt;&quot;127.0.0.1&quot;&lt;/span&gt;,

    &lt;span class=&quot;s2&quot;&gt;&quot;local_port&quot;&lt;/span&gt;:1080,

    &lt;span class=&quot;s2&quot;&gt;&quot;password&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;my_password&quot;&lt;/span&gt;,

    &lt;span class=&quot;s2&quot;&gt;&quot;timeout&quot;&lt;/span&gt;:300,

    &lt;span class=&quot;s2&quot;&gt;&quot;method&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;aes-256-cfb&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;详细配置说明：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Name&lt;/th&gt;
      &lt;th&gt;说明&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Server&lt;/td&gt;
      &lt;td&gt;服务器地址，填IP地址或域名&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;server_port&lt;/td&gt;
      &lt;td&gt;服务器开放端口&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;local_address&lt;/td&gt;
      &lt;td&gt;本地地址，127.0.0.1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;local_port&lt;/td&gt;
      &lt;td&gt;本地端口，一般为1080&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;password&lt;/td&gt;
      &lt;td&gt;服务器密码&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;port_password&lt;/td&gt;
      &lt;td&gt;服务器端口 + 密码&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;timeout&lt;/td&gt;
      &lt;td&gt;超时重连&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;method&lt;/td&gt;
      &lt;td&gt;加密方式，默认aes-256-cfb&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;fast_open&lt;/td&gt;
      &lt;td&gt;TCP_FASTOPEN&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 id=&quot;测试启动&quot;&gt;&lt;strong&gt;测试启动&lt;/strong&gt;&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;前端启动：sudo sslocal -c /etc/shadowsocks/config.json&lt;/li&gt;
  &lt;li&gt;后台启动：sudo sslocal -c /etc/shadowsocks/config.json -d start&lt;/li&gt;
  &lt;li&gt;后台停止：sudo sslocal -c /etc/shadowsocks/config.json -d stop&lt;/li&gt;
  &lt;li&gt;重启：sudo sslocal -c /etc/shadowsocks/config.json -d restart&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;开机启动&quot;&gt;&lt;strong&gt;开机启动&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;使用Systemd来实现shadowsocks开机自启。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;vim /etc/systemd/system/shadowsocks.service
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在里面填写如下内容：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;Unit]
&lt;span class=&quot;nv&quot;&gt;Description&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;Shadowsocks Client Service
&lt;span class=&quot;nv&quot;&gt;After&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;network.target

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;Service]
&lt;span class=&quot;nv&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;simple
&lt;span class=&quot;nv&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;root
&lt;span class=&quot;nv&quot;&gt;ExecStart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/bin/sslocal &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; /etc/shadowsocks/config.json 

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;Install]
&lt;span class=&quot;nv&quot;&gt;WantedBy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;multi-user.target
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;配置生效：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;systemctl &lt;span class=&quot;nb&quot;&gt;enable&lt;/span&gt; /etc/systemd/system/shadowsocks.service
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输入管理员密码后配置生效。&lt;/p&gt;

&lt;h3 id=&quot;shadowsocks_qt5客户端配置&quot;&gt;&lt;strong&gt;Shadowsocks_Qt5客户端配置&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Qt5客户端使用snap应用商店的ss-qt。&lt;/p&gt;

&lt;h4 id=&quot;安装-1&quot;&gt;&lt;strong&gt;安装&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;安装snap:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt update
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;snapd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;安装ss-qt&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;snap &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ss-qt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_2/Screenshot from 2019-02-23 03-49-17.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;代理配置&quot;&gt;&lt;strong&gt;代理配置&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;添加新的代理配置&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_2/Screenshot from 2019-02-23 03-51-56.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;保存后连接。&lt;/p&gt;

&lt;h4 id=&quot;开机启动-1&quot;&gt;&lt;strong&gt;开机启动&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;在Startup Application中添加ss-qt即可。&lt;/p&gt;

&lt;h2 id=&quot;2-chrome浏览器网络代理配置&quot;&gt;&lt;strong&gt;2. Chrome浏览器网络代理配置&lt;/strong&gt;&lt;/h2&gt;

&lt;h4 id=&quot;安装-2&quot;&gt;&lt;strong&gt;安装&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;使用SwitchyOmege配置Chrome浏览器的网络代理。首先需要在Chrome应用商店内安装SwitchyOmege&lt;/p&gt;

&lt;p&gt;在线安装直接打开Chrome应用商店，添加至Chrome即可。（需要科学上网）&lt;/p&gt;

&lt;p&gt;离线安装需要先下载SwithcyOmega的离线安装包，下载地址：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;1.GitHub：https://github.com/FelisCatus/SwitchyOmega/releases/latest&lt;/p&gt;

  &lt;p&gt;2.在线下载：https://www.switchyomega.com/download/&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;下载得到&lt;strong&gt;SwitchyOmega_Chromium.crx&lt;/strong&gt;这个离线安装文件后，在Chrome地址栏输入&lt;strong&gt;chrome://extensions&lt;/strong&gt;打开扩展程序，之后&lt;strong&gt;打开开发者模式&lt;/strong&gt;，将离线安装文件拖入到Chrome中即可进行安装。&lt;/p&gt;

&lt;h4 id=&quot;配置代理&quot;&gt;&lt;strong&gt;配置代理&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;安装成功后，先删除其自带的情景模式，然后点击New profile新建配置文件。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_2/Screenshot from 2019-02-23 10-44-32.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;名称为Vultr，类型选择Proxy Profile，点击创建。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_2/Screenshot from 2019-02-23 11-04-29.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;代理协议选择SOCKS5，服务器和端口填写本地IP地址和端口号（local_address和local_port）。&lt;/p&gt;

&lt;p&gt;配置完成后点击Apply changes保存配置。&lt;/p&gt;

&lt;p&gt;再新建一个配置文件，名称为AutoSwitch，类型选择Switch Profile。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_2/Screenshot from 2019-02-23 15-46-44.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;选择 Add a rule list 添加规则列表。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_2/Screenshot from 2019-02-23 15-07-36.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Rule List Format 选择Autoproxy。&lt;/p&gt;

&lt;p&gt;Rule List URL 填写：https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt&lt;/p&gt;

&lt;p&gt;这个地址是GFWList的地址，基本包含了常用的所有被墙网址，并且一直在更新。&lt;/p&gt;

&lt;p&gt;选择 Download Profile Now 下载Profile。&lt;/p&gt;

&lt;p&gt;然后将 Switch Rules 中对应的配置文件改为Vultr，保存配置即可。&lt;/p&gt;

&lt;h2 id=&quot;3-终端网络配置&quot;&gt;&lt;strong&gt;3. 终端网络配置&lt;/strong&gt;&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;在Chrome上，是SwitchyOmega插件把HTTP和HTTPS流量转换成了socks协议的流量，才能使用socks代理。而Ubuntu终端是没有这样的协议转换的，所以没法直接使用sock5代理。这时候就需要一个协议转换器，例如Privoxy。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;安装-3&quot;&gt;&lt;strong&gt;安装&lt;/strong&gt;&lt;/h4&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;privoxy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;配置-1&quot;&gt;&lt;strong&gt;配置&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;修改privoxy配置文件/etc/privoxy/config，在文件末尾添加如下内容：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;forward-socks5 / 127.0.0.1:1080 &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# SOCKS5代理地址&lt;/span&gt;
listen-address 127.0.0.1:8080     &lt;span class=&quot;c&quot;&gt;# HTTP代理地址&lt;/span&gt;
forward 10.&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;.&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;.&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;/ &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;               &lt;span class=&quot;c&quot;&gt;# 内网地址不走代理&lt;/span&gt;
forward .abc.com/ &lt;span class=&quot;nb&quot;&gt;.&lt;/span&gt;               &lt;span class=&quot;c&quot;&gt;# 指定域名不走代理&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中，第1行的 127.0.0.1:1080 是你在本地的SOCKS5代理地址，而第二行的 127.0.0.1:8080 则是SOCKS5转换成的 http 代理地址，最后两行指定了两个不走代理的地址。&lt;/p&gt;

&lt;p&gt;配置好以后配置好之后重启Privoxy服务：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo&lt;/span&gt; /etc/init.d/privoxy restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后打开 /etc/profile，在最后添加以下两行：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;http_proxy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;127.0.0.1:8080&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;https_proxy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;127.0.0.1:8080&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;即可在终端中科学上网。&lt;/p&gt;

&lt;h4 id=&quot;测试&quot;&gt;&lt;strong&gt;测试&lt;/strong&gt;&lt;/h4&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;curl google.come
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输入上述命令后显示Google首页的HTML代码即说明配置成功。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;curl ip.gs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输入上述命令后显示当前IP地址。&lt;/p&gt;

&lt;p&gt;由于执行ping指令使用ICMP传输协议，而SS代理是基于TCP或UDP协议，所以使用ping指令访问Google会超时。&lt;/p&gt;</content><author><name>Wyundi</name></author><category term="Shadowsocks" /><summary type="html">Ubuntu环境下使用Shadowsocks科学上网。使用Vultr服务器搭建SS服务端，并在本地配置网络代理。</summary></entry><entry><title type="html">Shadowsocks（一）：基于Vultr搭建SS服务器</title><link href="https://wyundi.github.io/2019/02/22/Shadowsocks-%E4%B8%80-%E5%9F%BA%E4%BA%8EVultr%E6%90%AD%E5%BB%BASS%E6%9C%8D%E5%8A%A1%E5%99%A8/" rel="alternate" type="text/html" title="Shadowsocks（一）：基于Vultr搭建SS服务器" /><published>2019-02-22T19:26:39+00:00</published><updated>2019-02-22T19:26:39+00:00</updated><id>https://wyundi.github.io/2019/02/22/Shadowsocks(%E4%B8%80):%E5%9F%BA%E4%BA%8EVultr%E6%90%AD%E5%BB%BASS%E6%9C%8D%E5%8A%A1%E5%99%A8</id><content type="html" xml:base="https://wyundi.github.io/2019/02/22/Shadowsocks-%E4%B8%80-%E5%9F%BA%E4%BA%8EVultr%E6%90%AD%E5%BB%BASS%E6%9C%8D%E5%8A%A1%E5%99%A8/">&lt;p&gt;Ubuntu环境下使用Shadowsocks科学上网。使用Vultr服务器搭建SS服务端，并在本地配置网络代理。&lt;/p&gt;

&lt;p&gt;注：本机操作系统为 Ubuntu 18.04， 图中所示服务器仅为测试用例，将在结束后销毁。&lt;/p&gt;

&lt;h2 id=&quot;vultr服务器&quot;&gt;&lt;strong&gt;Vultr服务器&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;Vultr是美国的一个VPS服务商，全球有15个数据中心，可以一键部署服务器。采用小时计费策略，可以在任何时间新建或者摧毁VPS服务器。价格低廉，最便宜的只要2.5一个月，支持支付宝及微信支付。&lt;/p&gt;

&lt;p&gt;Vultr官网连接：&lt;a href=&quot;https://www.vultr.com/&quot;&gt;Vultr The Infrastructure Cloud™&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;新用户注册&quot;&gt;&lt;strong&gt;新用户注册&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 14-19-26.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;打开Vultr官网，在相应的位置填写邮箱和密码，然后创建账户即可。注册完会受到一封验证邮件，点击连接确认注册。&lt;/p&gt;

&lt;h3 id=&quot;账户充值&quot;&gt;&lt;strong&gt;账户充值&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Vultr的服务器价格从一个月2.5美元到640美元不等，可根据个人需求选择相应的服务器方案。对服务器需求较小的个人用户可选择一个月5美元的计费方案，其中包含了25GB的SSD，1个CPU，1024MB内存以及1000GB的流量。Vultr的收费方式实际上是按小时计费的，比如5美元一个月的服务器对应的收费标准为0.07美元一小时。记时从开通服务器开始，直到服务器被销毁为止。费用会自动从账户中扣除。在Billing页面可以查看当前余额并进行充值，支持的支付方式包括信用卡、Paypal、支付宝以及微信等，还可以用Gift Code充值。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 14-51-54.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;创建服务器&quot;&gt;&lt;strong&gt;创建服务器&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;充值成功后，进入Servers页面，点击右上角的加号创建服务器。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 15-02-24.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;选择服务器配置，包括服务器位置，操作系统，机器配置以及附加服务等。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 15-06-05.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 15-04-08.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 15-07-37.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;配置完成后点击Deploy Now完成创建。&lt;/p&gt;

&lt;h3 id=&quot;配置服务器&quot;&gt;&lt;strong&gt;配置服务器&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;服务器创建成功后，你就可以从Servers页面看到你的服务器。待其安装完成后，点击服务器可以查看其详细信息。其中包含你的服务器的IP地址，用户名和密码。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 15-15-37.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;通过Linux终端连接VPS。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ssh &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; 远程服务器用户名 服务器ip地址
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;随后输入密码即可连接远程服务器。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 18-54-28.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;打开服务器终端以后开始配置和Shadowsocks， 这里采用&lt;a href=&quot;https://teddysun.com/342.html&quot;&gt;teddysun&lt;/a&gt;的一键安装脚本。
逐行输入以下命令：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;wget &lt;span class=&quot;nt&quot;&gt;--no-check-certificate&lt;/span&gt; https://raw.githubusercontent.com/teddysun/shadowsocks_install/master/shadowsocks.sh
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;chmod&lt;/span&gt; +x shadowsocks.sh
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./shadowsocks.sh 2&amp;gt;&amp;amp;1 | &lt;span class=&quot;nb&quot;&gt;tee &lt;/span&gt;shadowsocks.log
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;程序运行后将会出现如下所示界面：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 19-02-05.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;为你的Shadowsocks服务器设置密码。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 19-05-59.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;设置端口。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 19-06-32.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;选择加密方式。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 19-07-02.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;设置完成，按任意键开始配置Shadowsocks服务器。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/Shadowsocks_1/Screenshot from 2019-02-22 19-11-57.png&quot; alt=&quot;Alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;配置成功，终端将显示本机的Shadowsocks配置信息，将其保存后可用于配置客户端的Shadowsocks连接。&lt;/p&gt;</content><author><name>Wyundi</name></author><category term="Shadowsocks" /><summary type="html">Ubuntu环境下使用Shadowsocks科学上网。使用Vultr服务器搭建SS服务端，并在本地配置网络代理。</summary></entry></feed>