| <!DOCTYPE html> |
| |
| <html lang="en"> |
| <head> |
| <meta charset="utf-8"/> |
| <meta content="IE=edge" http-equiv="X-UA-Compatible"/> |
| <meta content="width=device-width, initial-scale=1" name="viewport"/> |
| <meta content="Handwritten Digits Classification Competition" property="og:title"> |
| <meta content="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/og-logo.png" property="og:image"> |
| <meta content="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/og-logo.png" property="og:image:secure_url"> |
| <meta content="Handwritten Digits Classification Competition" property="og:description"/> |
| <title>Handwritten Digits Classification Competition — mxnet documentation</title> |
| <link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" rel="stylesheet"/> |
| <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/> |
| <link href="../../_static/basic.css" rel="stylesheet" type="text/css"> |
| <link href="../../_static/pygments.css" rel="stylesheet" type="text/css"> |
| <link href="../../_static/mxnet.css" rel="stylesheet" type="text/css"/> |
| <script type="text/javascript"> |
| var DOCUMENTATION_OPTIONS = { |
| URL_ROOT: '../../', |
| VERSION: '', |
| COLLAPSE_INDEX: false, |
| FILE_SUFFIX: '.html', |
| HAS_SOURCE: true, |
| SOURCELINK_SUFFIX: '.txt' |
| }; |
| </script> |
| <script src="https://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script> |
| <script src="../../_static/underscore.js" type="text/javascript"></script> |
| <script src="../../_static/searchtools_custom.js" type="text/javascript"></script> |
| <script src="../../_static/doctools.js" type="text/javascript"></script> |
| <script src="../../_static/selectlang.js" type="text/javascript"></script> |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script> |
| <script type="text/javascript"> jQuery(function() { Search.loadIndex("/versions/1.2.1/searchindex.js"); Search.init();}); </script> |
| <script> |
| (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
| (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new |
| Date();a=s.createElement(o), |
| m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
| })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); |
| |
| ga('create', 'UA-96378503-1', 'auto'); |
| ga('send', 'pageview'); |
| |
| </script> |
| <!-- --> |
| <!-- <script type="text/javascript" src="../../_static/jquery.js"></script> --> |
| <!-- --> |
| <!-- <script type="text/javascript" src="../../_static/underscore.js"></script> --> |
| <!-- --> |
| <!-- <script type="text/javascript" src="../../_static/doctools.js"></script> --> |
| <!-- --> |
| <!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> --> |
| <!-- --> |
| <link href="../../genindex.html" rel="index" title="Index"> |
| <link href="../../search.html" rel="search" title="Search"/> |
| <link href="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/mxnet-icon.png" rel="icon" type="image/png"/> |
| </link></link></link></meta></meta></meta></head> |
| <body background="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/mxnet-background-compressed.jpeg" role="document"> |
| <div class="content-block"><div class="navbar navbar-fixed-top"> |
| <div class="container" id="navContainer"> |
| <div class="innder" id="header-inner"> |
| <h1 id="logo-wrap"> |
| <a href="../../" id="logo"><img src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/mxnet_logo.png"/></a> |
| </h1> |
| <nav class="nav-bar" id="main-nav"> |
| <a class="main-nav-link" href="/versions/1.2.1/install/index.html">Install</a> |
| <span id="dropdown-menu-position-anchor"> |
| <a aria-expanded="true" aria-haspopup="true" class="main-nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button">Gluon <span class="caret"></span></a> |
| <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu"> |
| <li><a class="main-nav-link" href="/versions/1.2.1/tutorials/gluon/gluon.html">About</a></li> |
| <li><a class="main-nav-link" href="https://www.d2l.ai/">Dive into Deep Learning</a></li> |
| <li><a class="main-nav-link" href="https://gluon-cv.mxnet.io">GluonCV Toolkit</a></li> |
| <li><a class="main-nav-link" href="https://gluon-nlp.mxnet.io/">GluonNLP Toolkit</a></li> |
| </ul> |
| </span> |
| <span id="dropdown-menu-position-anchor"> |
| <a aria-expanded="true" aria-haspopup="true" class="main-nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button">API <span class="caret"></span></a> |
| <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu"> |
| <li><a class="main-nav-link" href="/versions/1.2.1/api/python/index.html">Python</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/api/c++/index.html">C++</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/api/julia/index.html">Julia</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/api/perl/index.html">Perl</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/api/r/index.html">R</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/api/scala/index.html">Scala</a></li> |
| </ul> |
| </span> |
| <span id="dropdown-menu-position-anchor-docs"> |
| <a aria-expanded="true" aria-haspopup="true" class="main-nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button">Docs <span class="caret"></span></a> |
| <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu-docs"> |
| <li><a class="main-nav-link" href="/versions/1.2.1/faq/index.html">FAQ</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/tutorials/index.html">Tutorials</a> |
| <li><a class="main-nav-link" href="https://github.com/apache/incubator-mxnet/tree/1.2.1/example">Examples</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/architecture/index.html">Architecture</a></li> |
| <li><a class="main-nav-link" href="https://cwiki.apache.org/confluence/display/MXNET/Apache+MXNet+Home">Developer Wiki</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/model_zoo/index.html">Model Zoo</a></li> |
| <li><a class="main-nav-link" href="https://github.com/onnx/onnx-mxnet">ONNX</a></li> |
| </li></ul> |
| </span> |
| <span id="dropdown-menu-position-anchor-community"> |
| <a aria-expanded="true" aria-haspopup="true" class="main-nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button">Community <span class="caret"></span></a> |
| <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu-community"> |
| <li><a class="main-nav-link" href="http://discuss.mxnet.io">Forum</a></li> |
| <li><a class="main-nav-link" href="https://github.com/apache/incubator-mxnet/tree/1.2.1">Github</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/community/contribute.html">Contribute</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/community/powered_by.html">Powered By</a></li> |
| </ul> |
| </span> |
| <span id="dropdown-menu-position-anchor-version" style="position: relative"><a href="#" class="main-nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">1.2.1<span class="caret"></span></a><ul id="package-dropdown-menu" class="dropdown-menu"><li><a href="/">master</a></li><li><a href="/versions/1.7/">1.7</a></li><li><a href=/versions/1.6/>1.6</a></li><li><a href=/versions/1.5.0/>1.5.0</a></li><li><a href=/versions/1.4.1/>1.4.1</a></li><li><a href=/versions/1.3.1/>1.3.1</a></li><li><a href=/versions/1.2.1/>1.2.1</a></li><li><a href=/versions/1.1.0/>1.1.0</a></li><li><a href=/versions/1.0.0/>1.0.0</a></li><li><a href=/versions/0.12.1/>0.12.1</a></li><li><a href=/versions/0.11.0/>0.11.0</a></li></ul></span></nav> |
| <script> function getRootPath(){ return "../../" } </script> |
| <div class="burgerIcon dropdown"> |
| <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button">☰</a> |
| <ul class="dropdown-menu" id="burgerMenu"> |
| <li><a href="/versions/1.2.1/install/index.html">Install</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/tutorials/index.html">Tutorials</a></li> |
| <li class="dropdown-submenu dropdown"> |
| <a aria-expanded="true" aria-haspopup="true" class="dropdown-toggle burger-link" data-toggle="dropdown" href="#" tabindex="-1">Gluon</a> |
| <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu"> |
| <li><a class="main-nav-link" href="/versions/1.2.1/tutorials/gluon/gluon.html">About</a></li> |
| <li><a class="main-nav-link" href="http://gluon.mxnet.io">The Straight Dope (Tutorials)</a></li> |
| <li><a class="main-nav-link" href="https://gluon-cv.mxnet.io">GluonCV Toolkit</a></li> |
| <li><a class="main-nav-link" href="https://gluon-nlp.mxnet.io/">GluonNLP Toolkit</a></li> |
| </ul> |
| </li> |
| <li class="dropdown-submenu"> |
| <a aria-expanded="true" aria-haspopup="true" class="dropdown-toggle burger-link" data-toggle="dropdown" href="#" tabindex="-1">API</a> |
| <ul class="dropdown-menu"> |
| <li><a class="main-nav-link" href="/versions/1.2.1/api/python/index.html">Python</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/api/c++/index.html">C++</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/api/julia/index.html">Julia</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/api/perl/index.html">Perl</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/api/r/index.html">R</a></li> |
| <li><a class="main-nav-link" href="/versions/1.2.1/api/scala/index.html">Scala</a></li> |
| </ul> |
| </li> |
| <li class="dropdown-submenu"> |
| <a aria-expanded="true" aria-haspopup="true" class="dropdown-toggle burger-link" data-toggle="dropdown" href="#" tabindex="-1">Docs</a> |
| <ul class="dropdown-menu"> |
| <li><a href="/versions/1.2.1/faq/index.html" tabindex="-1">FAQ</a></li> |
| <li><a href="/versions/1.2.1/tutorials/index.html" tabindex="-1">Tutorials</a></li> |
| <li><a href="https://github.com/apache/incubator-mxnet/tree/1.2.1/example" tabindex="-1">Examples</a></li> |
| <li><a href="/versions/1.2.1/architecture/index.html" tabindex="-1">Architecture</a></li> |
| <li><a href="https://cwiki.apache.org/confluence/display/MXNET/Apache+MXNet+Home" tabindex="-1">Developer Wiki</a></li> |
| <li><a href="/versions/1.2.1/model_zoo/index.html" tabindex="-1">Gluon Model Zoo</a></li> |
| <li><a href="https://github.com/onnx/onnx-mxnet" tabindex="-1">ONNX</a></li> |
| </ul> |
| </li> |
| <li class="dropdown-submenu dropdown"> |
| <a aria-haspopup="true" class="dropdown-toggle burger-link" data-toggle="dropdown" href="#" role="button" tabindex="-1">Community</a> |
| <ul class="dropdown-menu"> |
| <li><a href="http://discuss.mxnet.io" tabindex="-1">Forum</a></li> |
| <li><a href="https://github.com/apache/incubator-mxnet/tree/1.2.1" tabindex="-1">Github</a></li> |
| <li><a href="/versions/1.2.1/community/contribute.html" tabindex="-1">Contribute</a></li> |
| <li><a href="/versions/1.2.1/community/powered_by.html" tabindex="-1">Powered By</a></li> |
| </ul> |
| </li> |
| <li id="dropdown-menu-position-anchor-version-mobile" class="dropdown-submenu" style="position: relative"><a href="#" tabindex="-1">1.2.1</a><ul class="dropdown-menu"><li><a tabindex="-1" href=/>master</a></li><li><a tabindex="-1" href=/versions/1.6/>1.6</a></li><li><a tabindex="-1" href=/versions/1.5.0/>1.5.0</a></li><li><a tabindex="-1" href=/versions/1.4.1/>1.4.1</a></li><li><a tabindex="-1" href=/versions/1.3.1/>1.3.1</a></li><li><a tabindex="-1" href=/versions/1.2.1/>1.2.1</a></li><li><a tabindex="-1" href=/versions/1.1.0/>1.1.0</a></li><li><a tabindex="-1" href=/versions/1.0.0/>1.0.0</a></li><li><a tabindex="-1" href=/versions/0.12.1/>0.12.1</a></li><li><a tabindex="-1" href=/versions/0.11.0/>0.11.0</a></li></ul></li></ul> |
| </div> |
| <div class="plusIcon dropdown"> |
| <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button"><span aria-hidden="true" class="glyphicon glyphicon-plus"></span></a> |
| <ul class="dropdown-menu dropdown-menu-right" id="plusMenu"></ul> |
| </div> |
| <div id="search-input-wrap"> |
| <form action="../../search.html" autocomplete="off" class="" method="get" role="search"> |
| <div class="form-group inner-addon left-addon"> |
| <i class="glyphicon glyphicon-search"></i> |
| <input class="form-control" name="q" placeholder="Search" type="text"/> |
| </div> |
| <input name="check_keywords" type="hidden" value="yes"> |
| <input name="area" type="hidden" value="default"/> |
| </input></form> |
| <div id="search-preview"></div> |
| </div> |
| <div id="searchIcon"> |
| <span aria-hidden="true" class="glyphicon glyphicon-search"></span> |
| </div> |
| <!-- <div id="lang-select-wrap"> --> |
| <!-- <label id="lang-select-label"> --> |
| <!-- <\!-- <i class="fa fa-globe"></i> -\-> --> |
| <!-- <span></span> --> |
| <!-- </label> --> |
| <!-- <select id="lang-select"> --> |
| <!-- <option value="en">Eng</option> --> |
| <!-- <option value="zh">中文</option> --> |
| <!-- </select> --> |
| <!-- </div> --> |
| <!-- <a id="mobile-nav-toggle"> |
| <span class="mobile-nav-toggle-bar"></span> |
| <span class="mobile-nav-toggle-bar"></span> |
| <span class="mobile-nav-toggle-bar"></span> |
| </a> --> |
| </div> |
| </div> |
| </div> |
| <script type="text/javascript"> |
| $('body').css('background', 'white'); |
| </script> |
| <div class="container"> |
| <div class="row"> |
| <div aria-label="main navigation" class="sphinxsidebar leftsidebar" role="navigation"> |
| <div class="sphinxsidebarwrapper"> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="../../api/python/index.html">Python Documents</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../../api/r/index.html">R Documents</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../../api/julia/index.html">Julia Documents</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../../api/c++/index.html">C++ Documents</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../../api/scala/index.html">Scala Documents</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../../api/perl/index.html">Perl Documents</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../../faq/index.html">HowTo Documents</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../../architecture/index.html">System Documents</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../index.html">Tutorials</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../../community/index.html">Community</a></li> |
| </ul> |
| </div> |
| </div> |
| <div class="content"> |
| <div class="page-tracker"></div> |
| <div class="section" id="handwritten-digits-classification-competition"> |
| <span id="handwritten-digits-classification-competition"></span><h1>Handwritten Digits Classification Competition<a class="headerlink" href="#handwritten-digits-classification-competition" title="Permalink to this headline">¶</a></h1> |
| <p><a class="reference external" href="http://yann.lecun.com/exdb/mnist/">MNIST</a> is a handwritten digits image data set created by Yann LeCun. Every digit is represented by a 28 x 28 pixel image. It’s become a standard data set for testing classifiers on simple image input. A neural network is a strong model for image classification tasks. There’s a <a class="reference external" href="https://www.kaggle.com/c/digit-recognizer">long-term hosted competition</a> on Kaggle using this data set. |
| This tutorial shows how to use <a class="reference external" href="https://github.com/dmlc/mxnet/tree/master/R-package">MXNet</a> to compete in this challenge.</p> |
| <div class="section" id="loading-the-data"> |
| <span id="loading-the-data"></span><h2>Loading the Data<a class="headerlink" href="#loading-the-data" title="Permalink to this headline">¶</a></h2> |
| <p>First, let’s download the data from <a class="reference external" href="https://www.kaggle.com/c/digit-recognizer/data">Kaggle</a> and put it in the <code class="docutils literal"><span class="pre">data/</span></code> folder in your working directory.</p> |
| <p>Now we can read it in R and convert it to matrices:</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="nf">require</span><span class="p">(</span><span class="n">mxnet</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## Loading required package: mxnet</span> |
| <span class="c1">## Loading required package: methods</span> |
| </pre></div> |
| </div> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">train</span> <span class="o"><-</span> <span class="nf">read.csv</span><span class="p">(</span><span class="s">'data/train.csv'</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">)</span> |
| <span class="n">test</span> <span class="o"><-</span> <span class="nf">read.csv</span><span class="p">(</span><span class="s">'data/test.csv'</span><span class="p">,</span> <span class="n">header</span><span class="o">=</span><span class="kc">TRUE</span><span class="p">)</span> |
| <span class="n">train</span> <span class="o"><-</span> <span class="nf">data.matrix</span><span class="p">(</span><span class="n">train</span><span class="p">)</span> |
| <span class="n">test</span> <span class="o"><-</span> <span class="nf">data.matrix</span><span class="p">(</span><span class="n">test</span><span class="p">)</span> |
| |
| <span class="n">train.x</span> <span class="o"><-</span> <span class="n">train[</span><span class="p">,</span><span class="m">-1</span><span class="n">]</span> |
| <span class="n">train.y</span> <span class="o"><-</span> <span class="n">train[</span><span class="p">,</span><span class="m">1</span><span class="n">]</span> |
| </pre></div> |
| </div> |
| <p>Every image is represented as a single row in train/test. The greyscale of each image falls in the range [0, 255]. Linearly transform it into [0,1] by using the following command:</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">train.x</span> <span class="o"><-</span> <span class="nf">t</span><span class="p">(</span><span class="n">train.x</span><span class="o">/</span><span class="m">255</span><span class="p">)</span> |
| <span class="n">test</span> <span class="o"><-</span> <span class="nf">t</span><span class="p">(</span><span class="n">test</span><span class="o">/</span><span class="m">255</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <p>Transpose the input matrix to npixel x nexamples, which is the major format for columns accepted by MXNet (and the convention of R).</p> |
| <p>In the label section, the number of each digit is fairly evenly distributed:</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="nf">table</span><span class="p">(</span><span class="n">train.y</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## train.y</span> |
| <span class="c1">## 0 1 2 3 4 5 6 7 8 9</span> |
| <span class="c1">## 4132 4684 4177 4351 4072 3795 4137 4401 4063 4188</span> |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="configuring-the-network"> |
| <span id="configuring-the-network"></span><h2>Configuring the Network<a class="headerlink" href="#configuring-the-network" title="Permalink to this headline">¶</a></h2> |
| <p>Now that we have the data, let’s configure the structure of our network:</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">data</span> <span class="o"><-</span> <span class="nf">mx.symbol.Variable</span><span class="p">(</span><span class="s">"data"</span><span class="p">)</span> |
| <span class="n">fc1</span> <span class="o"><-</span> <span class="nf">mx.symbol.FullyConnected</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">"fc1"</span><span class="p">,</span> <span class="n">num_hidden</span><span class="o">=</span><span class="m">128</span><span class="p">)</span> |
| <span class="n">act1</span> <span class="o"><-</span> <span class="nf">mx.symbol.Activation</span><span class="p">(</span><span class="n">fc1</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">"relu1"</span><span class="p">,</span> <span class="n">act_type</span><span class="o">=</span><span class="s">"relu"</span><span class="p">)</span> |
| <span class="n">fc2</span> <span class="o"><-</span> <span class="nf">mx.symbol.FullyConnected</span><span class="p">(</span><span class="n">act1</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">"fc2"</span><span class="p">,</span> <span class="n">num_hidden</span><span class="o">=</span><span class="m">64</span><span class="p">)</span> |
| <span class="n">act2</span> <span class="o"><-</span> <span class="nf">mx.symbol.Activation</span><span class="p">(</span><span class="n">fc2</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">"relu2"</span><span class="p">,</span> <span class="n">act_type</span><span class="o">=</span><span class="s">"relu"</span><span class="p">)</span> |
| <span class="n">fc3</span> <span class="o"><-</span> <span class="nf">mx.symbol.FullyConnected</span><span class="p">(</span><span class="n">act2</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">"fc3"</span><span class="p">,</span> <span class="n">num_hidden</span><span class="o">=</span><span class="m">10</span><span class="p">)</span> |
| <span class="n">softmax</span> <span class="o"><-</span> <span class="nf">mx.symbol.SoftmaxOutput</span><span class="p">(</span><span class="n">fc3</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">"sm"</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <ol class="simple"> |
| <li>In <code class="docutils literal"><span class="pre">mxnet</span></code>, we use the data type <code class="docutils literal"><span class="pre">symbol</span></code> to configure the network. <code class="docutils literal"><span class="pre">data</span> <span class="pre"><-</span> <span class="pre">mx.symbol.Variable("data")</span></code> uses <code class="docutils literal"><span class="pre">data</span></code> to represent the input data, i.e., the input layer.</li> |
| <li>We set the first hidden layer with <code class="docutils literal"><span class="pre">fc1</span> <span class="pre"><-</span> <span class="pre">mx.symbol.FullyConnected(data,</span> <span class="pre">name="fc1",</span> <span class="pre">num_hidden=128)</span></code>. This layer has <code class="docutils literal"><span class="pre">data</span></code> as the input, its name, and the number of hidden neurons.</li> |
| <li>Activation is set with <code class="docutils literal"><span class="pre">act1</span> <span class="pre"><-</span> <span class="pre">mx.symbol.Activation(fc1,</span> <span class="pre">name="relu1",</span> <span class="pre">act_type="relu")</span></code>. The activation function takes the output from the first hidden layer, <code class="docutils literal"><span class="pre">fc1</span></code>.</li> |
| <li>The second hidden layer takes the result from <code class="docutils literal"><span class="pre">act1</span></code> as input, with its name as “fc2” and the number of hidden neurons as 64.</li> |
| <li>The second activation is almost the same as <code class="docutils literal"><span class="pre">act1</span></code>, except we have a different input source and name.</li> |
| <li>This generates the output layer. Because there are only 10 digits, we set the number of neurons to 10.</li> |
| <li>Finally, we set the activation to softmax to get a probabilistic prediction.</li> |
| </ol> |
| </div> |
| <div class="section" id="training"> |
| <span id="training"></span><h2>Training<a class="headerlink" href="#training" title="Permalink to this headline">¶</a></h2> |
| <p>We are almost ready for the training process. Before we start the computation, let’s decide which device to use:</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">devices</span> <span class="o"><-</span> <span class="nf">mx.cpu</span><span class="p">()</span> |
| </pre></div> |
| </div> |
| <p>We assign CPU to <code class="docutils literal"><span class="pre">mxnet</span></code>. Now, you can run the following command to train the neural network! Note that <code class="docutils literal"><span class="pre">mx.set.seed</span></code> is the function that controls the random process in <code class="docutils literal"><span class="pre">mxnet</span></code>:</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="nf">mx.set.seed</span><span class="p">(</span><span class="m">0</span><span class="p">)</span> |
| <span class="n">model</span> <span class="o"><-</span> <span class="nf">mx.model.FeedForward.create</span><span class="p">(</span><span class="n">softmax</span><span class="p">,</span> <span class="n">X</span><span class="o">=</span><span class="n">train.x</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">train.y</span><span class="p">,</span> |
| <span class="n">ctx</span><span class="o">=</span><span class="n">devices</span><span class="p">,</span> <span class="n">num.round</span><span class="o">=</span><span class="m">10</span><span class="p">,</span> <span class="n">array.batch.size</span><span class="o">=</span><span class="m">100</span><span class="p">,</span> |
| <span class="n">learning.rate</span><span class="o">=</span><span class="m">0.07</span><span class="p">,</span> <span class="n">momentum</span><span class="o">=</span><span class="m">0.9</span><span class="p">,</span> <span class="n">eval.metric</span><span class="o">=</span><span class="n">mx.metric.accuracy</span><span class="p">,</span> |
| <span class="n">initializer</span><span class="o">=</span><span class="nf">mx.init.uniform</span><span class="p">(</span><span class="m">0.07</span><span class="p">),</span> |
| <span class="n">epoch.end.callback</span><span class="o">=</span><span class="nf">mx.callback.log.train.metric</span><span class="p">(</span><span class="m">100</span><span class="p">))</span> |
| </pre></div> |
| </div> |
| <div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## Start training with 1 devices</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.6563</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.777999999999999</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.827466666666665</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.855499999999999</span> |
| <span class="c1">## [1] Train-accuracy=0.859832935560859</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.9529</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.953049999999999</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.955866666666666</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.957525000000001</span> |
| <span class="c1">## [2] Train-accuracy=0.958309523809525</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.968</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.9677</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.9696</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.970650000000002</span> |
| <span class="c1">## [3] Train-accuracy=0.970809523809526</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.973</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.974249999999999</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.976</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.977100000000003</span> |
| <span class="c1">## [4] Train-accuracy=0.977452380952384</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.9834</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.981949999999999</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.981900000000001</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.982600000000003</span> |
| <span class="c1">## [5] Train-accuracy=0.983000000000003</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.983399999999999</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.98405</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.985000000000001</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.985725000000003</span> |
| <span class="c1">## [6] Train-accuracy=0.985952380952384</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.988999999999999</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.9876</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.988100000000001</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.988750000000003</span> |
| <span class="c1">## [7] Train-accuracy=0.988880952380955</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.991999999999999</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.9912</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.990066666666668</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.990275000000003</span> |
| <span class="c1">## [8] Train-accuracy=0.990452380952384</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.9937</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.99235</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.991966666666668</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.991425000000003</span> |
| <span class="c1">## [9] Train-accuracy=0.991500000000003</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.9942</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.99245</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.992433333333334</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.992275000000002</span> |
| <span class="c1">## [10] Train-accuracy=0.992380952380955</span> |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="making-a-prediction-and-submitting-to-the-competition"> |
| <span id="making-a-prediction-and-submitting-to-the-competition"></span><h2>Making a Prediction and Submitting to the Competition<a class="headerlink" href="#making-a-prediction-and-submitting-to-the-competition" title="Permalink to this headline">¶</a></h2> |
| <p>To make a prediction, type:</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">preds</span> <span class="o"><-</span> <span class="nf">predict</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">test</span><span class="p">)</span> |
| <span class="nf">dim</span><span class="p">(</span><span class="n">preds</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## [1] 10 28000</span> |
| </pre></div> |
| </div> |
| <p>It is a matrix with 28000 rows and 10 cols, containing the desired classification probabilities from the output layer. To extract the maximum label for each row, use <code class="docutils literal"><span class="pre">max.col</span></code>:</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">pred.label</span> <span class="o"><-</span> <span class="nf">max.col</span><span class="p">(</span><span class="nf">t</span><span class="p">(</span><span class="n">preds</span><span class="p">))</span> <span class="o">-</span> <span class="m">1</span> |
| <span class="nf">table</span><span class="p">(</span><span class="n">pred.label</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## pred.label</span> |
| <span class="c1">## 0 1 2 3 4 5 6 7 8 9</span> |
| <span class="c1">## 2818 3195 2744 2767 2683 2596 2798 2790 2784 2825</span> |
| </pre></div> |
| </div> |
| <p>With a little extra effort to modify the .csv format, our submission is ready for the competition!</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">submission</span> <span class="o"><-</span> <span class="nf">data.frame</span><span class="p">(</span><span class="n">ImageId</span><span class="o">=</span><span class="m">1</span><span class="o">:</span><span class="nf">ncol</span><span class="p">(</span><span class="n">test</span><span class="p">),</span> <span class="n">Label</span><span class="o">=</span><span class="n">pred.label</span><span class="p">)</span> |
| <span class="nf">write.csv</span><span class="p">(</span><span class="n">submission</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="s">'submission.csv'</span><span class="p">,</span> <span class="n">row.names</span><span class="o">=</span><span class="kc">FALSE</span><span class="p">,</span> <span class="n">quote</span><span class="o">=</span><span class="kc">FALSE</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| </div> |
| <div class="section" id="lenet"> |
| <span id="lenet"></span><h2>LeNet<a class="headerlink" href="#lenet" title="Permalink to this headline">¶</a></h2> |
| <p>Now let’s use a new network structure: <a class="reference external" href="http://yann.lecun.com/exdb/lenet/">LeNet</a>. It has been proposed by Yann LeCun for recognizing handwritten digits. We’ll demonstrate how to construct and train a LeNet in <code class="docutils literal"><span class="pre">mxnet</span></code>.</p> |
| <p>First, we construct the network:</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span><span class="c1"># input</span> |
| <span class="n">data</span> <span class="o"><-</span> <span class="nf">mx.symbol.Variable</span><span class="p">(</span><span class="s">'data'</span><span class="p">)</span> |
| <span class="c1"># first conv</span> |
| <span class="n">conv1</span> <span class="o"><-</span> <span class="nf">mx.symbol.Convolution</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">,</span> <span class="n">kernel</span><span class="o">=</span><span class="nf">c</span><span class="p">(</span><span class="m">5</span><span class="p">,</span><span class="m">5</span><span class="p">),</span> <span class="n">num_filter</span><span class="o">=</span><span class="m">20</span><span class="p">)</span> |
| <span class="n">tanh1</span> <span class="o"><-</span> <span class="nf">mx.symbol.Activation</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">conv1</span><span class="p">,</span> <span class="n">act_type</span><span class="o">=</span><span class="s">"tanh"</span><span class="p">)</span> |
| <span class="n">pool1</span> <span class="o"><-</span> <span class="nf">mx.symbol.Pooling</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">tanh1</span><span class="p">,</span> <span class="n">pool_type</span><span class="o">=</span><span class="s">"max"</span><span class="p">,</span> |
| <span class="n">kernel</span><span class="o">=</span><span class="nf">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="m">2</span><span class="p">),</span> <span class="n">stride</span><span class="o">=</span><span class="nf">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="m">2</span><span class="p">))</span> |
| <span class="c1"># second conv</span> |
| <span class="n">conv2</span> <span class="o"><-</span> <span class="nf">mx.symbol.Convolution</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">pool1</span><span class="p">,</span> <span class="n">kernel</span><span class="o">=</span><span class="nf">c</span><span class="p">(</span><span class="m">5</span><span class="p">,</span><span class="m">5</span><span class="p">),</span> <span class="n">num_filter</span><span class="o">=</span><span class="m">50</span><span class="p">)</span> |
| <span class="n">tanh2</span> <span class="o"><-</span> <span class="nf">mx.symbol.Activation</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">conv2</span><span class="p">,</span> <span class="n">act_type</span><span class="o">=</span><span class="s">"tanh"</span><span class="p">)</span> |
| <span class="n">pool2</span> <span class="o"><-</span> <span class="nf">mx.symbol.Pooling</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">tanh2</span><span class="p">,</span> <span class="n">pool_type</span><span class="o">=</span><span class="s">"max"</span><span class="p">,</span> |
| <span class="n">kernel</span><span class="o">=</span><span class="nf">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="m">2</span><span class="p">),</span> <span class="n">stride</span><span class="o">=</span><span class="nf">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="m">2</span><span class="p">))</span> |
| <span class="c1"># first fullc</span> |
| <span class="n">flatten</span> <span class="o"><-</span> <span class="nf">mx.symbol.Flatten</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">pool2</span><span class="p">)</span> |
| <span class="n">fc1</span> <span class="o"><-</span> <span class="nf">mx.symbol.FullyConnected</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">flatten</span><span class="p">,</span> <span class="n">num_hidden</span><span class="o">=</span><span class="m">500</span><span class="p">)</span> |
| <span class="n">tanh3</span> <span class="o"><-</span> <span class="nf">mx.symbol.Activation</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">fc1</span><span class="p">,</span> <span class="n">act_type</span><span class="o">=</span><span class="s">"tanh"</span><span class="p">)</span> |
| <span class="c1"># second fullc</span> |
| <span class="n">fc2</span> <span class="o"><-</span> <span class="nf">mx.symbol.FullyConnected</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">tanh3</span><span class="p">,</span> <span class="n">num_hidden</span><span class="o">=</span><span class="m">10</span><span class="p">)</span> |
| <span class="c1"># loss</span> |
| <span class="n">lenet</span> <span class="o"><-</span> <span class="nf">mx.symbol.SoftmaxOutput</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">fc2</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <p>Then let’s reshape the matrices into arrays:</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span><span class="n">train.array</span> <span class="o"><-</span> <span class="n">train.x</span> |
| <span class="nf">dim</span><span class="p">(</span><span class="n">train.array</span><span class="p">)</span> <span class="o"><-</span> <span class="nf">c</span><span class="p">(</span><span class="m">28</span><span class="p">,</span> <span class="m">28</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="nf">ncol</span><span class="p">(</span><span class="n">train.x</span><span class="p">))</span> |
| <span class="n">test.array</span> <span class="o"><-</span> <span class="n">test</span> |
| <span class="nf">dim</span><span class="p">(</span><span class="n">test.array</span><span class="p">)</span> <span class="o"><-</span> <span class="nf">c</span><span class="p">(</span><span class="m">28</span><span class="p">,</span> <span class="m">28</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="nf">ncol</span><span class="p">(</span><span class="n">test</span><span class="p">))</span> |
| </pre></div> |
| </div> |
| <p>We want to compare training speed on different devices, so define the devices:</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span><span class="n">n.gpu</span> <span class="o"><-</span> <span class="m">1</span> |
| <span class="n">device.cpu</span> <span class="o"><-</span> <span class="nf">mx.cpu</span><span class="p">()</span> |
| <span class="n">device.gpu</span> <span class="o"><-</span> <span class="nf">lapply</span><span class="p">(</span><span class="m">0</span><span class="o">:</span><span class="p">(</span><span class="n">n.gpu</span><span class="m">-1</span><span class="p">),</span> <span class="nf">function</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span> |
| <span class="nf">mx.gpu</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> |
| <span class="p">})</span> |
| </pre></div> |
| </div> |
| <p>We can pass a list of devices to ask MXNet to train on multiple GPUs (you can do this for CPUs, |
| but because internal computation of CPUs is already multi-threaded, there is less gain than with using GPUs).</p> |
| <p>Start by training on the CPU first. Because this takes a bit time, we run it for just one iteration.</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="nf">mx.set.seed</span><span class="p">(</span><span class="m">0</span><span class="p">)</span> |
| <span class="n">tic</span> <span class="o"><-</span> <span class="nf">proc.time</span><span class="p">()</span> |
| <span class="n">model</span> <span class="o"><-</span> <span class="nf">mx.model.FeedForward.create</span><span class="p">(</span><span class="n">lenet</span><span class="p">,</span> <span class="n">X</span><span class="o">=</span><span class="n">train.array</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">train.y</span><span class="p">,</span> |
| <span class="n">ctx</span><span class="o">=</span><span class="n">device.cpu</span><span class="p">,</span> <span class="n">num.round</span><span class="o">=</span><span class="m">1</span><span class="p">,</span> <span class="n">array.batch.size</span><span class="o">=</span><span class="m">100</span><span class="p">,</span> |
| <span class="n">learning.rate</span><span class="o">=</span><span class="m">0.05</span><span class="p">,</span> <span class="n">momentum</span><span class="o">=</span><span class="m">0.9</span><span class="p">,</span> <span class="n">wd</span><span class="o">=</span><span class="m">0.00001</span><span class="p">,</span> |
| <span class="n">eval.metric</span><span class="o">=</span><span class="n">mx.metric.accuracy</span><span class="p">,</span> |
| <span class="n">epoch.end.callback</span><span class="o">=</span><span class="nf">mx.callback.log.train.metric</span><span class="p">(</span><span class="m">100</span><span class="p">))</span> |
| </pre></div> |
| </div> |
| <div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## Start training with 1 devices</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.1066</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.16495</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.401766666666667</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.537675</span> |
| <span class="c1">## [1] Train-accuracy=0.557136038186157</span> |
| </pre></div> |
| </div> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="nf">print</span><span class="p">(</span><span class="nf">proc.time</span><span class="p">()</span> <span class="o">-</span> <span class="n">tic</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## user system elapsed</span> |
| <span class="c1">## 130.030 204.976 83.821</span> |
| </pre></div> |
| </div> |
| <p>Train on a GPU:</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="nf">mx.set.seed</span><span class="p">(</span><span class="m">0</span><span class="p">)</span> |
| <span class="n">tic</span> <span class="o"><-</span> <span class="nf">proc.time</span><span class="p">()</span> |
| <span class="n">model</span> <span class="o"><-</span> <span class="nf">mx.model.FeedForward.create</span><span class="p">(</span><span class="n">lenet</span><span class="p">,</span> <span class="n">X</span><span class="o">=</span><span class="n">train.array</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">train.y</span><span class="p">,</span> |
| <span class="n">ctx</span><span class="o">=</span><span class="n">device.gpu</span><span class="p">,</span> <span class="n">num.round</span><span class="o">=</span><span class="m">5</span><span class="p">,</span> <span class="n">array.batch.size</span><span class="o">=</span><span class="m">100</span><span class="p">,</span> |
| <span class="n">learning.rate</span><span class="o">=</span><span class="m">0.05</span><span class="p">,</span> <span class="n">momentum</span><span class="o">=</span><span class="m">0.9</span><span class="p">,</span> <span class="n">wd</span><span class="o">=</span><span class="m">0.00001</span><span class="p">,</span> |
| <span class="n">eval.metric</span><span class="o">=</span><span class="n">mx.metric.accuracy</span><span class="p">,</span> |
| <span class="n">epoch.end.callback</span><span class="o">=</span><span class="nf">mx.callback.log.train.metric</span><span class="p">(</span><span class="m">100</span><span class="p">))</span> |
| </pre></div> |
| </div> |
| <div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## Start training with 1 devices</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.1066</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.1596</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.3983</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.533975</span> |
| <span class="c1">## [1] Train-accuracy=0.553532219570405</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.958</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.96155</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.966100000000001</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.968550000000003</span> |
| <span class="c1">## [2] Train-accuracy=0.969071428571432</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.977</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.97715</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.979566666666668</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.980900000000003</span> |
| <span class="c1">## [3] Train-accuracy=0.981309523809527</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.9853</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.985899999999999</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.986966666666668</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.988150000000002</span> |
| <span class="c1">## [4] Train-accuracy=0.988452380952384</span> |
| <span class="c1">## Batch [100] Train-accuracy=0.990199999999999</span> |
| <span class="c1">## Batch [200] Train-accuracy=0.98995</span> |
| <span class="c1">## Batch [300] Train-accuracy=0.990600000000001</span> |
| <span class="c1">## Batch [400] Train-accuracy=0.991325000000002</span> |
| <span class="c1">## [5] Train-accuracy=0.991523809523812</span> |
| </pre></div> |
| </div> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="nf">print</span><span class="p">(</span><span class="nf">proc.time</span><span class="p">()</span> <span class="o">-</span> <span class="n">tic</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## user system elapsed</span> |
| <span class="c1">## 9.288 1.680 6.889</span> |
| </pre></div> |
| </div> |
| <p>By using a GPU processor, we significantly speed up training! |
| Now, we can submit the result to Kaggle to see the improvement of our ranking!</p> |
| <div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">preds</span> <span class="o"><-</span> <span class="nf">predict</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">test.array</span><span class="p">)</span> |
| <span class="n">pred.label</span> <span class="o"><-</span> <span class="nf">max.col</span><span class="p">(</span><span class="nf">t</span><span class="p">(</span><span class="n">preds</span><span class="p">))</span> <span class="o">-</span> <span class="m">1</span> |
| <span class="n">submission</span> <span class="o"><-</span> <span class="nf">data.frame</span><span class="p">(</span><span class="n">ImageId</span><span class="o">=</span><span class="m">1</span><span class="o">:</span><span class="nf">ncol</span><span class="p">(</span><span class="n">test</span><span class="p">),</span> <span class="n">Label</span><span class="o">=</span><span class="n">pred.label</span><span class="p">)</span> |
| <span class="nf">write.csv</span><span class="p">(</span><span class="n">submission</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="s">'submission.csv'</span><span class="p">,</span> <span class="n">row.names</span><span class="o">=</span><span class="kc">FALSE</span><span class="p">,</span> <span class="n">quote</span><span class="o">=</span><span class="kc">FALSE</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <p><img alt="" src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/knitr/mnistCompetition-kaggle-submission.png"/></p> |
| </div> |
| <div class="section" id="next-steps"> |
| <span id="next-steps"></span><h2>Next Steps<a class="headerlink" href="#next-steps" title="Permalink to this headline">¶</a></h2> |
| <div class="toctree-wrapper compound"> |
| <ul> |
| <li class="toctree-l1"><a class="reference external" href="/versions/1.2.1/tutorials/r/charRnnModel.html">Character Language Model using RNN</a></li> |
| </ul> |
| </div> |
| </div> |
| </div> |
| </div> |
| </div> |
| <div aria-label="main navigation" class="sphinxsidebar rightsidebar" role="navigation"> |
| <div class="sphinxsidebarwrapper"> |
| <h3><a href="../../index.html">Table Of Contents</a></h3> |
| <ul> |
| <li><a class="reference internal" href="#">Handwritten Digits Classification Competition</a><ul> |
| <li><a class="reference internal" href="#loading-the-data">Loading the Data</a></li> |
| <li><a class="reference internal" href="#configuring-the-network">Configuring the Network</a></li> |
| <li><a class="reference internal" href="#training">Training</a></li> |
| <li><a class="reference internal" href="#making-a-prediction-and-submitting-to-the-competition">Making a Prediction and Submitting to the Competition</a></li> |
| <li><a class="reference internal" href="#lenet">LeNet</a></li> |
| <li><a class="reference internal" href="#next-steps">Next Steps</a></li> |
| </ul> |
| </li> |
| </ul> |
| </div> |
| </div> |
| </div><div class="footer"> |
| <div class="section-disclaimer"> |
| <div class="container"> |
| <div> |
| <img height="60" src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/apache_incubator_logo.png"/> |
| <p> |
| Apache MXNet is an effort undergoing incubation at The Apache Software Foundation (ASF), <strong>sponsored by the <i>Apache Incubator</i></strong>. Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects. While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF. |
| </p> |
| <p> |
| "Copyright © 2017-2018, The Apache Software Foundation |
| Apache MXNet, MXNet, Apache, the Apache feather, and the Apache MXNet project logo are either registered trademarks or trademarks of the Apache Software Foundation." |
| </p> |
| </div> |
| </div> |
| </div> |
| </div> <!-- pagename != index --> |
| </div> |
| <script crossorigin="anonymous" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> |
| <script src="../../_static/js/sidebar.js" type="text/javascript"></script> |
| <script src="../../_static/js/search.js" type="text/javascript"></script> |
| <script src="../../_static/js/navbar.js" type="text/javascript"></script> |
| <script src="../../_static/js/clipboard.min.js" type="text/javascript"></script> |
| <script src="../../_static/js/copycode.js" type="text/javascript"></script> |
| <script src="../../_static/js/page.js" type="text/javascript"></script> |
| <script src="../../_static/js/docversion.js" type="text/javascript"></script> |
| <script type="text/javascript"> |
| $('body').ready(function () { |
| $('body').css('visibility', 'visible'); |
| }); |
| </script> |
| </body> |
| </html> |