<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/zalandos-images-classification-using-h2o-with-r-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://appsilondatascience.com/blog/rstats/2017/09/12/first-diving-into-h2o.html"> Appsilon Data Science Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Fashion-MNIST</h2>
<p>About three weeks ago the Fashion-MNIST dataset of Zalando’s article images, which is a great replacement of classical MNIST dataset, was released. In the following article we will try to build a strong classifier using H2O and R.</p>
<p>Each example is a 28×28 grayscale image, associated with a label from 10 classes:</p>
<ol>
<li>T-shirt/top</li>
<li>Trouser</li>
<li>Pullover</li>
<li>Dress</li>
<li>Coat</li>
<li>Sandal</li>
<li>Shirt</li>
<li>Sneaker</li>
<li>Bag</li>
<li>Ankle boot</li>
</ol>
<p>You can download it here <a rel="nofollow" href="https://www.kaggle.com/zalando-research/fashionmnist" target="_blank">https://www.kaggle.com/zalando-research/fashionmnist</a></p>
<p>The first column is an image label and the other 784 pixel columns are associated with the darkness of that pixel.</p>
<h2>Quick reminder: what is H2O?</h2>
<p>H2O is an open-source, fast, scalable platform for machine learning written in Java. It allows access to all of its capabilities from Python, Scala and most importantly from R via REST API.</p>
<p>Overview of available algorithms:</p>
<ol>
<li>Supervised:
<ul>
<li>Deep Learning (Neural Networks)</li>
<li>Distributed Random Forest (DRF)</li>
<li>Generalized Linear Model (GLM)</li>
<li>Gradient Boosting Machine (GBM)</li>
<li>Naive Bayes Classifier</li>
<li>Stacked Ensembles</li>
<li>XGBoost</li>
</ul>
</li>
<li>Unsupervised
<ul>
<li>Generalized Low Rank Models (GLRM)</li>
<li>K-Means Clustering</li>
<li>Principal Component Analysis (PCA)</li>
</ul>
</li>
</ol>
<p>Instalation is easy:</p>
<figure>
<pre><code data-lang="r"><span>install.packages</span><span>(</span><span>&quot;h2o&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>h</span><span>2</span><span>o</span><span>)</span></code></pre>
</figure>
<h2>Building a neural network for image classification</h2>
<p>Let’s start by running an H2O cluster:</p>
<figure>
<pre><code data-lang="r"><span>h</span><span>2</span><span>o.init</span><span>(</span><span>ip</span><span> </span><span>=</span><span> </span><span>&quot;localhost&quot;</span><span>,</span><span>
</span><span>port</span><span> </span><span>=</span><span> </span><span>54321</span><span>,</span><span>
</span><span>nthreads</span><span> </span><span>=</span><span> </span><span>-1</span><span>,</span><span>
</span><span>min_mem_size</span><span> </span><span>=</span><span> </span><span>&quot;20g&quot;</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>H</span><span>2</span><span>O</span><span> </span><span>is</span><span> </span><span>not</span><span> </span><span>running</span><span> </span><span>yet</span><span>,</span><span> </span><span>starting</span><span> </span><span>it</span><span> </span><span>now...</span><span>
</span><span>Note</span><span>:</span><span> </span><span>In</span><span> </span><span>case</span><span> </span><span>of</span><span> </span><span>errors</span><span> </span><span>look</span><span> </span><span>at</span><span> </span><span>the</span><span> </span><span>following</span><span> </span><span>log</span><span> </span><span>files</span><span>:</span><span>
</span><span>/</span><span>tmp</span><span>/</span><span>RtmpQEf3RX</span><span>/</span><span>h</span><span>2</span><span>o_maju116_started_from_r.out</span><span>
</span><span>/</span><span>tmp</span><span>/</span><span>RtmpQEf3RX</span><span>/</span><span>h</span><span>2</span><span>o_maju116_started_from_r.err</span><span>
</span><span>openjdk</span><span> </span><span>version</span><span> </span><span>&quot;1.8.0_131&quot;</span><span>
</span><span>OpenJDK</span><span> </span><span>Runtime</span><span> </span><span>Environment</span><span> </span><span>(</span><span>build</span><span> </span><span>1.8.0</span><span>_</span><span>131-8</span><span>u</span><span>131</span><span>-</span><span>b</span><span>11-2</span><span>ubuntu1.16.04.3</span><span>-</span><span>b</span><span>11</span><span>)</span><span>
</span><span>OpenJDK</span><span> </span><span>64</span><span>-</span><span>Bit</span><span> </span><span>Server</span><span> </span><span>VM</span><span> </span><span>(</span><span>build</span><span> </span><span>25.131</span><span>-</span><span>b</span><span>11</span><span>,</span><span> </span><span>mixed</span><span> </span><span>mode</span><span>)</span><span>
</span><span>Starting</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>JVM</span><span> </span><span>and</span><span> </span><span>connecting</span><span>:</span><span> </span><span>..</span><span> </span><span>Connection</span><span> </span><span>successful</span><span>!</span><span>
</span><span>R</span><span> </span><span>is</span><span> </span><span>connected</span><span> </span><span>to</span><span> </span><span>the</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>cluster</span><span>:</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>cluster</span><span> </span><span>uptime</span><span>:</span><span> </span><span>1</span><span> </span><span>seconds</span><span> </span><span>906</span><span> </span><span>milliseconds</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>cluster</span><span> </span><span>version</span><span>:</span><span> </span><span>3.13.0.3973</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>cluster</span><span> </span><span>version</span><span> </span><span>age</span><span>:</span><span> </span><span>1</span><span> </span><span>month</span><span> </span><span>and</span><span> </span><span>5</span><span> </span><span>days</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>cluster</span><span> </span><span>name</span><span>:</span><span> </span><span>H</span><span>2</span><span>O_started_from_R_maju116_cuf927</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>cluster</span><span> </span><span>total</span><span> </span><span>nodes</span><span>:</span><span> </span><span>1</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>cluster</span><span> </span><span>total</span><span> </span><span>memory</span><span>:</span><span> </span><span>19.17</span><span> </span><span>GB</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>cluster</span><span> </span><span>total</span><span> </span><span>cores</span><span>:</span><span> </span><span>8</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>cluster</span><span> </span><span>allowed</span><span> </span><span>cores</span><span>:</span><span> </span><span>8</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>cluster</span><span> </span><span>healthy</span><span>:</span><span> </span><span>TRUE</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>Connection</span><span> </span><span>ip</span><span>:</span><span> </span><span>localhost</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>Connection</span><span> </span><span>port</span><span>:</span><span> </span><span>54321</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>Connection</span><span> </span><span>proxy</span><span>:</span><span> </span><span>NA</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>Internal</span><span> </span><span>Security</span><span>:</span><span> </span><span>FALSE</span><span> </span><span>H</span><span>2</span><span>O</span><span> </span><span>API</span><span> </span><span>Extensions</span><span>:</span><span> </span><span>XGBoost</span><span>,</span><span> </span><span>Algos</span><span>,</span><span> </span><span>AutoML</span><span>,</span><span> </span><span>Core</span><span> </span><span>V</span><span>3</span><span>,</span><span> </span><span>Core</span><span> </span><span>V</span><span>4</span><span> </span><span>R</span><span> </span><span>Version</span><span>:</span><span> </span><span>R</span><span> </span><span>version</span><span> </span><span>3.4.1</span><span> </span><span>(</span><span>2017-06-30</span><span>)</span><span> </span></code></pre>
</figure>
<p>Next we will import data into H2O using <code>h2o.importFile()</code> function, in which we can specify column types and column names if needed. If you want to send data into H2O directly from R, you can use <code>as.h2o()</code> function</p>
<figure>
<pre><code data-lang="r"><span>fmnist_train</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.importFile</span><span>(</span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;data/fashion-mnist_train.csv&quot;</span><span>,</span><span> </span><span>destination_frame</span><span> </span><span>=</span><span> </span><span>&quot;fmnist_train&quot;</span><span>,</span><span>
</span><span>col.types</span><span>=</span><span>c</span><span>(</span><span>&quot;factor&quot;</span><span>,</span><span> </span><span>rep</span><span>(</span><span>&quot;int&quot;</span><span>,</span><span> </span><span>784</span><span>)))</span><span>
</span><span>fmnist_test</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.importFile</span><span>(</span><span>path</span><span> </span><span>=</span><span> </span><span>&quot;data/fashion-mnist_test.csv&quot;</span><span>,</span><span>
</span><span>destination_frame</span><span> </span><span>=</span><span> </span><span>&quot;fmnist_test&quot;</span><span>,</span><span>
</span><span>col.types</span><span>=</span><span>c</span><span>(</span><span>&quot;factor&quot;</span><span>,</span><span> </span><span>rep</span><span>(</span><span>&quot;int&quot;</span><span>,</span><span> </span><span>784</span><span>)))</span></code></pre>
</figure>
<p>If everything went fine, we can check if our datasets are in H2O:</p>
<figure>
<pre><code data-lang="r"><span>h</span><span>2</span><span>o.ls</span><span>()</span></code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span> </span><span>key</span><span>
</span><span>1</span><span> </span><span>fmnist_test</span><span>
</span><span>2</span><span> </span><span>fmnist_train</span></code></pre>
</figure>
<p>Before we begin modeling, let’s take a quick look at the data:</p>
<figure>
<pre><code data-lang="r"><span>xy_axis</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>expand.grid</span><span>(</span><span>1</span><span>:</span><span>28</span><span>,</span><span>28</span><span>:</span><span>1</span><span>)[,</span><span>1</span><span>],</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>expand.grid</span><span>(</span><span>1</span><span>:</span><span>28</span><span>,</span><span>28</span><span>:</span><span>1</span><span>)[,</span><span>2</span><span>])</span><span>
</span><span>plot_theme</span><span> </span><span>&lt;-</span><span> </span><span>list</span><span>(</span><span>
</span><span>raster</span><span> </span><span>=</span><span> </span><span>geom_raster</span><span>(</span><span>hjust</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>vjust</span><span> </span><span>=</span><span> </span><span>0</span><span>),</span><span>
</span><span>gradient_fill</span><span> </span><span>=</span><span> </span><span>scale_fill_gradient</span><span>(</span><span>low</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>high</span><span> </span><span>=</span><span> </span><span>&quot;black&quot;</span><span>,</span><span> </span><span>guide</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>),</span><span>
</span><span>theme</span><span> </span><span>=</span><span> </span><span>theme</span><span>(</span><span>axis.line</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.ticks</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.title</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.grid.major</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.grid.minor</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>plot.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>())</span><span>
</span><span>)</span><span>
</span><span>sample_plots</span><span> </span><span>&lt;-</span><span> </span><span>sample</span><span>(</span><span>1</span><span>:</span><span>nrow</span><span>(</span><span>fmnist_train</span><span>),</span><span>100</span><span>)</span><span> </span><span>%>%</span><span> </span><span>map</span><span>(</span><span>~</span><span> </span><span>{</span><span>
</span><span>plot_data</span><span> </span><span>&lt;-</span><span> </span><span>cbind</span><span>(</span><span>xy_axis</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>as.data.frame</span><span>(</span><span>t</span><span>(</span><span>fmnist_train</span><span>[</span><span>.x</span><span>,</span><span> </span><span>-1</span><span>]))[,</span><span>1</span><span>])</span><span> </span><span>ggplot</span><span>(</span><span>plot_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>fill</span><span>))</span><span> </span><span>+</span><span> </span><span>plot_theme</span><span>
</span><span>})</span><span>
</span><span>do.call</span><span>(</span><span>&quot;grid.arrange&quot;</span><span>,</span><span> </span><span>c</span><span>(</span><span>sample_plots</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>nrow</span><span> </span><span>=</span><span> </span><span>10</span><span>))</span></code></pre>
</figure>
<p><img alt="100 Random items from Fashion-MNIST dataset" src="https://i1.wp.com/appsilondatascience.com/blog/assets/article_images/2017-09-04-into-h2o/fmnist.png?w=456" data-recalc-dims="1"></p>
<p>Now we will build a simple neural network, with one hidden layer of ten neurons:</p>
<figure>
<pre><code data-lang="r"><span>fmnist_nn_1</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.deeplearning</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>2</span><span>:</span><span>785</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;label&quot;</span><span>,</span><span> </span><span>training_frame</span><span> </span><span>=</span><span> </span><span>fmnist_train</span><span>,</span><span>
</span><span>distribution</span><span> </span><span>=</span><span> </span><span>&quot;multinomial&quot;</span><span>,</span><span>
</span><span>model_id</span><span> </span><span>=</span><span> </span><span>&quot;fmnist_nn_1&quot;</span><span>,</span><span>
</span><span>l</span><span>2</span><span> </span><span>=</span><span> </span><span>0.4</span><span>,</span><span>
</span><span>ignore_const_cols</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>hidden</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>export_weights_and_biases</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre>
</figure>
<p>If we set <code>export_weights_and_biases</code> parameter to <code>TRUE</code> networks weights and biases will be saved and we can retrieve them using <code>h2o.weights()</code> and <code>h2o.biases()</code> functions. Thanks to this we can try to visualize neurons from the hidden layer (Note that we set ignore_const_cols to <code>FALSE</code> to get weights for every pixel).</p>
<figure>
<pre><code data-lang="r"><span>weights_nn_1</span><span> </span><span>&lt;-</span><span> </span><span>as.data.frame</span><span>(</span><span>h</span><span>2</span><span>o.weights</span><span>(</span><span>fmnist_nn_1</span><span>,</span><span> </span><span>1</span><span>))</span><span>
</span><span>biases_nn_1</span><span> </span><span>&lt;-</span><span> </span><span>as.vector</span><span>(</span><span>h</span><span>2</span><span>o.biases</span><span>(</span><span>fmnist_nn_1</span><span>,</span><span> </span><span>1</span><span>))</span><span>
</span><span>neurons_plots</span><span> </span><span>&lt;-</span><span> </span><span>1</span><span>:</span><span>10</span><span> </span><span>%>%</span><span> </span><span>map</span><span>(</span><span>~</span><span> </span><span>{</span><span>
</span><span>plot_data</span><span> </span><span>&lt;-</span><span> </span><span>cbind</span><span>(</span><span>xy_axis</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>t</span><span>(</span><span>weights_nn_1</span><span>[</span><span>.x</span><span>,])</span><span> </span><span>+</span><span> </span><span>biases_nn_1</span><span>[</span><span>.x</span><span>])</span><span>
</span><span>colnames</span><span>(</span><span>plot_data</span><span>)[</span><span>3</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;fill&quot;</span><span>
</span><span>ggplot</span><span>(</span><span>plot_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span>,</span><span> </span><span>y</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>fill</span><span>))</span><span> </span><span>+</span><span> </span><span>plot_theme</span><span>
</span><span>})</span><span>
</span><span>do.call</span><span>(</span><span>&quot;grid.arrange&quot;</span><span>,</span><span> </span><span>c</span><span>(</span><span>neurons_plots</span><span>,</span><span> </span><span>ncol</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>nrow</span><span> </span><span>=</span><span> </span><span>4</span><span>))</span></code></pre>
</figure>
<p><img alt="Hidden layer" src="https://i2.wp.com/appsilondatascience.com/blog/assets/article_images/2017-09-04-into-h2o/hidden_1.png?w=456" data-recalc-dims="1"></p>
<p>We can definitely see some resemblance to shirts and sneakers. Let’s test our model:</p>
<figure>
<pre><code data-lang="r"><span>h</span><span>2</span><span>o.confusionMatrix</span><span>(</span><span>fmnist_nn_1</span><span>,</span><span> </span><span>fmnist_test</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>Confusion</span><span> </span><span>Matrix</span><span>:</span><span> </span><span>Row</span><span> </span><span>labels</span><span>:</span><span> </span><span>Actual</span><span> </span><span>class</span><span>;</span><span> </span><span>Column</span><span> </span><span>labels</span><span>:</span><span> </span><span>Predicted</span><span> </span><span>class</span><span>
</span><span>0</span><span> </span><span>1</span><span> </span><span>2</span><span> </span><span>3</span><span> </span><span>4</span><span> </span><span>5</span><span> </span><span>6</span><span> </span><span>7</span><span> </span><span>8</span><span> </span><span>9</span><span> </span><span>Error</span><span> </span><span>Rate</span><span>
</span><span>0</span><span> </span><span>801</span><span> </span><span>12</span><span> </span><span>14</span><span> </span><span>87</span><span> </span><span>2</span><span> </span><span>36</span><span> </span><span>25</span><span> </span><span>1</span><span> </span><span>22</span><span> </span><span>0</span><span> </span><span>0.1990</span><span> </span><span>=</span><span> </span><span>199</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>1</span><span> </span><span>6</span><span> </span><span>938</span><span> </span><span>23</span><span> </span><span>25</span><span> </span><span>1</span><span> </span><span>3</span><span> </span><span>4</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0.0620</span><span> </span><span>=</span><span> </span><span>62</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>2</span><span> </span><span>24</span><span> </span><span>4</span><span> </span><span>695</span><span> </span><span>7</span><span> </span><span>188</span><span> </span><span>18</span><span> </span><span>49</span><span> </span><span>0</span><span> </span><span>15</span><span> </span><span>0</span><span> </span><span>0.3050</span><span> </span><span>=</span><span> </span><span>305</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>3</span><span> </span><span>43</span><span> </span><span>23</span><span> </span><span>12</span><span> </span><span>865</span><span> </span><span>21</span><span> </span><span>13</span><span> </span><span>22</span><span> </span><span>0</span><span> </span><span>1</span><span> </span><span>0</span><span> </span><span>0.1350</span><span> </span><span>=</span><span> </span><span>135</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>4</span><span> </span><span>1</span><span> </span><span>6</span><span> </span><span>138</span><span> </span><span>44</span><span> </span><span>770</span><span> </span><span>14</span><span> </span><span>25</span><span> </span><span>0</span><span> </span><span>2</span><span> </span><span>0</span><span> </span><span>0.2300</span><span> </span><span>=</span><span> </span><span>230</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>5</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>1</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>865</span><span> </span><span>0</span><span> </span><span>90</span><span> </span><span>7</span><span> </span><span>37</span><span> </span><span>0.1350</span><span> </span><span>=</span><span> </span><span>135</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>6</span><span> </span><span>273</span><span> </span><span>6</span><span> </span><span>224</span><span> </span><span>53</span><span> </span><span>262</span><span> </span><span>46</span><span> </span><span>107</span><span> </span><span>0</span><span> </span><span>28</span><span> </span><span>1</span><span> </span><span>0.8930</span><span> </span><span>=</span><span> </span><span>893</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>7</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>107</span><span> </span><span>0</span><span> </span><span>838</span><span> </span><span>0</span><span> </span><span>55</span><span> </span><span>0.1620</span><span> </span><span>=</span><span> </span><span>162</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>8</span><span> </span><span>4</span><span> </span><span>1</span><span> </span><span>13</span><span> </span><span>22</span><span> </span><span>5</span><span> </span><span>36</span><span> </span><span>10</span><span> </span><span>8</span><span> </span><span>897</span><span> </span><span>4</span><span> </span><span>0.1030</span><span> </span><span>=</span><span> </span><span>103</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>9</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>40</span><span> </span><span>0</span><span> </span><span>104</span><span> </span><span>0</span><span> </span><span>856</span><span> </span><span>0.1440</span><span> </span><span>=</span><span> </span><span>144</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>Totals</span><span> </span><span>1152</span><span> </span><span>990</span><span> </span><span>1120</span><span> </span><span>1103</span><span> </span><span>1249</span><span> </span><span>1178</span><span> </span><span>242</span><span> </span><span>1041</span><span> </span><span>972</span><span> </span><span>953</span><span> </span><span>0.2368</span><span> </span><span>=</span><span> </span><span>2</span><span> </span><span>368</span><span> </span><span>/</span><span> </span><span>10</span><span> </span><span>000</span></code></pre>
</figure>
<p>Accuracy 0.7632 isn’t a great result, but we didn’t use full capabilities of H2O yet. We should do something more advanced!</p>
<p>In <code>h2o.deeplearning()</code> function there’s over 70 parameters responsible for structure and optimization of our model. Changing thme should give as much better results.</p>
<figure>
<pre><code data-lang="r"><span>fmnist_nn_final</span><span> </span><span>&lt;-</span><span> </span><span>h</span><span>2</span><span>o.deeplearning</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>2</span><span>:</span><span>785</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;label&quot;</span><span>,</span><span>
</span><span>training_frame</span><span> </span><span>=</span><span> </span><span>fmnist_train</span><span>,</span><span>
</span><span>distribution</span><span> </span><span>=</span><span> </span><span>&quot;multinomial&quot;</span><span>,</span><span>
</span><span>model_id</span><span> </span><span>=</span><span> </span><span>&quot;fmnist_nn_final&quot;</span><span>,</span><span>
</span><span>activation</span><span> </span><span>=</span><span> </span><span>&quot;RectifierWithDropout&quot;</span><span>,</span><span>
</span><span>hidden</span><span>=</span><span>c</span><span>(</span><span>1000</span><span>,</span><span> </span><span>1000</span><span>,</span><span> </span><span>2000</span><span>),</span><span>
</span><span>epochs</span><span> </span><span>=</span><span> </span><span>180</span><span>,</span><span>
</span><span>adaptive_rate</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>rate</span><span>=</span><span>0.01</span><span>,</span><span>
</span><span>rate_annealing</span><span> </span><span>=</span><span> </span><span>1.0e-6</span><span>,</span><span>
</span><span>rate_decay</span><span> </span><span>=</span><span> </span><span>1.0</span><span>,</span><span>
</span><span>momentum_start</span><span> </span><span>=</span><span> </span><span>0.4</span><span>,</span><span>
</span><span>momentum_ramp</span><span> </span><span>=</span><span> </span><span>384000</span><span>,</span><span>
</span><span>momentum_stable</span><span> </span><span>=</span><span> </span><span>0.98</span><span>,</span><span> </span><span>input_dropout_ratio</span><span> </span><span>=</span><span> </span><span>0.22</span><span>,</span><span>
</span><span>l</span><span>1</span><span> </span><span>=</span><span> </span><span>1.0e-5</span><span>,</span><span>
</span><span>max_w2</span><span> </span><span>=</span><span> </span><span>15.0</span><span>,</span><span> </span><span>initial_weight_distribution</span><span> </span><span>=</span><span> </span><span>&quot;Normal&quot;</span><span>,</span><span>
</span><span>initial_weight_scale</span><span> </span><span>=</span><span> </span><span>0.01</span><span>,</span><span>
</span><span>nesterov_accelerated_gradient</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>loss</span><span> </span><span>=</span><span> </span><span>&quot;CrossEntropy&quot;</span><span>,</span><span>
</span><span>fast_mode</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>diagnostics</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>ignore_const_cols</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>force_load_balance</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>seed</span><span> </span><span>=</span><span> </span><span>3.656455e+18</span><span>)</span><span>
</span><span>h</span><span>2</span><span>o.confusionMatrix</span><span>(</span><span>fmnist_nn_final</span><span>,</span><span> </span><span>fmnist_test</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>Confusion</span><span> </span><span>Matrix</span><span>:</span><span> </span><span>Row</span><span> </span><span>labels</span><span>:</span><span> </span><span>Actual</span><span> </span><span>class</span><span>;</span><span> </span><span>Column</span><span> </span><span>labels</span><span>:</span><span> </span><span>Predicted</span><span> </span><span>class</span><span>
</span><span>0</span><span> </span><span>1</span><span> </span><span>2</span><span> </span><span>3</span><span> </span><span>4</span><span> </span><span>5</span><span> </span><span>6</span><span> </span><span>7</span><span> </span><span>8</span><span> </span><span>9</span><span> </span><span>Error</span><span> </span><span>Rate</span><span>
</span><span>0</span><span> </span><span>898</span><span> </span><span>0</span><span> </span><span>14</span><span> </span><span>15</span><span> </span><span>1</span><span> </span><span>1</span><span> </span><span>66</span><span> </span><span>0</span><span> </span><span>5</span><span> </span><span>0</span><span> </span><span>0.1020</span><span> </span><span>=</span><span> </span><span>102</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>1</span><span> </span><span>2</span><span> </span><span>990</span><span> </span><span>2</span><span> </span><span>6</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0.0100</span><span> </span><span>=</span><span> </span><span>10</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>2</span><span> </span><span>12</span><span> </span><span>1</span><span> </span><span>875</span><span> </span><span>13</span><span> </span><span>60</span><span> </span><span>1</span><span> </span><span>35</span><span> </span><span>0</span><span> </span><span>3</span><span> </span><span>0</span><span> </span><span>0.1250</span><span> </span><span>=</span><span> </span><span>125</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>3</span><span> </span><span>16</span><span> </span><span>11</span><span> </span><span>8</span><span> </span><span>925</span><span> </span><span>23</span><span> </span><span>1</span><span> </span><span>14</span><span> </span><span>0</span><span> </span><span>2</span><span> </span><span>0</span><span> </span><span>0.0750</span><span> </span><span>=</span><span> </span><span>75</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>4</span><span> </span><span>1</span><span> </span><span>0</span><span> </span><span>61</span><span> </span><span>21</span><span> </span><span>885</span><span> </span><span>0</span><span> </span><span>30</span><span> </span><span>0</span><span> </span><span>2</span><span> </span><span>0</span><span> </span><span>0.1150</span><span> </span><span>=</span><span> </span><span>115</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>5</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>1</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>964</span><span> </span><span>0</span><span> </span><span>24</span><span> </span><span>1</span><span> </span><span>10</span><span> </span><span>0.0360</span><span> </span><span>=</span><span> </span><span>36</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>6</span><span> </span><span>131</span><span> </span><span>2</span><span> </span><span>66</span><span> </span><span>22</span><span> </span><span>50</span><span> </span><span>0</span><span> </span><span>722</span><span> </span><span>0</span><span> </span><span>7</span><span> </span><span>0</span><span> </span><span>0.2780</span><span> </span><span>=</span><span> </span><span>278</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>7</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>10</span><span> </span><span>0</span><span> </span><span>963</span><span> </span><span>0</span><span> </span><span>27</span><span> </span><span>0.0370</span><span> </span><span>=</span><span> </span><span>37</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>8</span><span> </span><span>4</span><span> </span><span>1</span><span> </span><span>4</span><span> </span><span>1</span><span> </span><span>1</span><span> </span><span>2</span><span> </span><span>3</span><span> </span><span>2</span><span> </span><span>981</span><span> </span><span>1</span><span> </span><span>0.0190</span><span> </span><span>=</span><span> </span><span>19</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>9</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>0</span><span> </span><span>6</span><span> </span><span>0</span><span> </span><span>37</span><span> </span><span>0</span><span> </span><span>957</span><span> </span><span>0.0430</span><span> </span><span>=</span><span> </span><span>43</span><span> </span><span>/</span><span> </span><span>1</span><span> </span><span>000</span><span>
</span><span>Totals</span><span> </span><span>1064</span><span> </span><span>1005</span><span> </span><span>1031</span><span> </span><span>1003</span><span> </span><span>1020</span><span> </span><span>985</span><span> </span><span>870</span><span> </span><span>1026</span><span> </span><span>1001</span><span> </span><span>995</span><span> </span><span>0.0840</span><span> </span><span>=</span><span> </span><span>840</span><span> </span><span>/</span><span> </span><span>10</span><span> </span><span>000</span></code></pre>
</figure>
<p>Accuracy 0.916 is a lot better result, but there’s sitll a lot of thing we can do to improve our model. In the future we can consider using a grid or random search to find best hyperparameters or use same ensemble methods to get better results.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://appsilondatascience.com/blog/rstats/2017/09/12/first-diving-into-h2o.html"> Appsilon Data Science Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/mT_uFrYtzQM" height="1"></a></div>