专栏首页思考是一种快乐卷积神经网络的python实现

卷积神经网络的python实现

这篇文章介绍如何使用Michael Nielsen 用python写的卷积神经网络代码,以及比较卷积神经网络和普通神经网络预测的效果。

这个例子是经典的识别MNIST手写体的AI程序。如下面这些手写数字,分别代表504192。这个程序会对这样的样本进行训练,并在测试集上验证正确率。

至于卷积神经网络的原理,我以后会单独写一篇文章介绍。

准备:

  • 安装 virtualenv
pip install virtualenv
  • 创建env
virtualenv neural
cd neural
source bin/activate
  • 安装 Theano库
pip install Theano
  • 下载 代码
git clone https://github.com/mnielsen/neural-networks-and-deep-learning.git

卷积神经网络在src/network3.py里。因为在作者写完代码后,Theano库又有更新, 且downsample被废弃,所以network3.py需要做如2处修改:'#'后面的为原来的代码,不带'#'的是修改后的代码。

#from theano.tensor.signal import downsample
from theano.tensor.signal.pool import pool_2d
...

#pooled_out = downsample.max_pool_2d(input=conv_out, ds=self.poolsize, ignore_border=True)
pooled_out = pool_2d(input=conv_out, ws=self.poolsize, ignore_border=True)
  • 进入python
cd neural-networks-and-deep-learning/src
phtyon

普通神经网络

  • 使用普通的full-connected layer模型训练 各种参数如下。每个参数的含义,我以后会专门写文章介绍,也可参考作者的书。
single hidden layer
100  hidden neurons
60 epochs
learning rate : η=0.1
mini-batch size : 10
no regularization
  • 先用普通神经网络训练,执行命令:
>>> import network3
>>> from network3 import Network
>>> from network3 import ConvPoolLayer, FullyConnectedLayer, SoftmaxLayer
>>> training_data, validation_data, test_data = network3.load_data_shared()
>>> mini_batch_size = 10
>>> net = Network([
        FullyConnectedLayer(n_in=784, n_out=100),
        SoftmaxLayer(n_in=100, n_out=10)], mini_batch_size)
>>> net.SGD(training_data, 60, mini_batch_size, 0.1, 
            validation_data, test_data)
  • 执行结果
Training mini-batch number 0
Training mini-batch number 1000
Training mini-batch number 2000
Training mini-batch number 3000
Training mini-batch number 4000
Epoch 0: validation accuracy 92.62%
This is the best validation accuracy to date.
The corresponding test accuracy is 92.00%
Training mini-batch number 5000
Training mini-batch number 6000
Training mini-batch number 7000
Training mini-batch number 8000
Training mini-batch number 9000
Epoch 1: validation accuracy 94.64%
This is the best validation accuracy to date.
The corresponding test accuracy is 94.10%
...
Training mini-batch number 295000
Training mini-batch number 296000
Training mini-batch number 297000
Training mini-batch number 298000
Training mini-batch number 299000
Epoch 59: validation accuracy 97.76%
This is the best validation accuracy to date.
The corresponding test accuracy is 97.79%
Finished training network.
Best validation accuracy of 97.76% obtained at iteration 299999
Corresponding test accuracy of 97.79%

准确率为97.79%,或者说错误率2.21%

卷积神经网络

  • 使用卷积模型训练 各种参数如下:
local receptive fields: 5x5
stride length : 1
feature maps : 20
max-pooling layer
pooling windows: 2x2
  • 执行命令
>>> net = Network([
        ConvPoolLayer(image_shape=(mini_batch_size, 1, 28, 28), 
                      filter_shape=(20, 1, 5, 5), 
                      poolsize=(2, 2)),
        FullyConnectedLayer(n_in=20*12*12, n_out=100),
        SoftmaxLayer(n_in=100, n_out=10)], mini_batch_size)
>>> net.SGD(training_data, 60, mini_batch_size, 0.1, 
            validation_data, test_data)
  • 输出
Training mini-batch number 0
Training mini-batch number 1000
Training mini-batch number 2000
Training mini-batch number 3000
Training mini-batch number 4000
Epoch 0: validation accuracy 94.18%
This is the best validation accuracy to date.
The corresponding test accuracy is 93.43%
Training mini-batch number 5000
Training mini-batch number 6000
Training mini-batch number 7000
Training mini-batch number 8000
Training mini-batch number 9000
Epoch 1: validation accuracy 96.12%
This is the best validation accuracy to date.
The corresponding test accuracy is 95.85%
...
Training mini-batch number 295000
Training mini-batch number 296000
Training mini-batch number 297000
Training mini-batch number 298000
Training mini-batch number 299000
Epoch 59: validation accuracy 98.74%
Finished training network.
Best validation accuracy of 98.74% obtained at iteration 214999
Corresponding test accuracy of 98.84%

准确率为98.84%,或者说错误率1.16%. 错误率几乎降低了一半!

相关文章

卷积神经网络的原理

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Symfony 3 框架+Elasticsearch

    目录src/AppBundle/Controller/SearchController.php

    Stanley Sun
  • 搜索中的权重度量利器: TF-IDF和BM25

    我们在网上搜东西时,搜索引擎总是会把相关性高的内容显示在前面,相关性低的内容显示在后面。那么,搜索引擎是如何计算关键字和内容的相关性呢?这里介绍2种重要的权重度...

    Stanley Sun
  • Scrapy爬虫读取中文出现乱码

    Stanley Sun
  • UML类图使用

    画UML图的工具大致可以分为两类,一类是专业的绘图工具,带了画UML的功能,如Visio、Dia;另一类是专门用来制作UML图的,如ArgoUML和Rose,通...

    用户5807183
  • maven基础--IDEA集成

    provided:已提供依赖范围。编译和测试有效,运行无效。如servlet-api,在项目运行时,tomcat等容器已经提供

    eadela
  • UML类图简单介绍

    一个项目初期,我们往往对业务一无所知,我们最急迫需要解决的问题就是理清楚这些业务概念以及它们的关系,如果能用好类图,你将能深入地剖析系统业务。

    终身幼稚园
  • 一周播报| 实体零售正迈向大数据和O2O的DT时代:阿里224亿拿下大润发、欧尚超市!

    求教各位老铁们,用户中心项目某一功能在压测下到达4000的TPS,这算是什么水准?薪资可以拿到20K+吗?

    养码场
  • 从基础知识到实际应用,一文了解机器学习非凸优化技术

    选自arXiv 优化技术在科技领域应用广泛,小到航班表,大到医疗、物理、人工智能的发展,皆可看到其身影,机器学习当然也不例外,且在实践中经历了一个从凸优化到非凸...

    企鹅号小编
  • 【简历指南1】程序员通用简历模板

    其次,写简历和写议论文不同,过分的论证会显得自夸,反而容易引起反感,所以要点到为止。这里的技巧是,提供论据,把论点留给阅读简历的人自己去得出。放论据要具体,最基...

    ConardLi
  • 从基础知识到实际应用,一文了解「机器学习非凸优化技术」

    机器之心

扫码关注云+社区

领取腾讯云代金券