<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/deep-learning-dude-pt-1/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://thinkinator.com/2017/06/03/deep-learning-dude-pt-1/"> Rblog – Thinkinator</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>You’ve probably noticed that Deep Learning is all the rage right now. AlphaGo has beaten the world champion at Go, you can google cat photos and be sure you won’t accidentally get photos of canines, and many other near-miraculous feats: all enabled by Deep Learning with neural nets. (I am thinking of coining the phrase “laminar learning” to add some panache to old-school non-deep learning.)</p>
<p>I do a lot of my work in R, and it turns out that not one but two R packages have recently been released that enable R users to use the famous Python-based deep learning package, <code>Keras</code>: <code>keras</code> and <code>kerasR</code>.</p>
<p><img sizes="(max-width: 440px) 100vw, 440px" data-image-description="" data-orig-file="https://thinkinator.files.wordpress.com/2017/06/model2.png?w=456" data-orig-size="800,530" data-image-title="model2" data-comments-opened="1" class="wp-image-1576" data-recalc-dims="1" data-attachment-id="1576" data-permalink="https://thinkinator.com/2017/06/03/deep-learning-dude-pt-1/model2/" alt="model2" data-medium-file="https://thinkinator.files.wordpress.com/2017/06/model2.png?w=456?w=300" data-large-file="https://thinkinator.files.wordpress.com/2017/06/model2.png?w=456?w=440" data-image-meta="{" src="https://thinkinator.files.wordpress.com/2017/06/model2.png?w=456"></p>
<p><span></span></p>
<p>The first thing you’ll need to do is to make sure you have Python 3 on your system. Windows generally doesn’t have Python at all, while Macs and some Linux systems will include Python 2.7. The way I’d recommend to get Python 3 is to download and install <a target="_blank" rel="nofollow" href="https://www.continuum.io/downloads">Anaconda</a>. (Few people do data science with a base-installed Python: they mostly use Anaconda, which pre-packages a lot of data science tools along with Python. Trying to install all of those tools yourself is … difficult.)</p>
<p>The usual place to install Anaconda is in your home directory. Then, on a Mac or Linux system add <code>~/anaconda/bin:</code> to the beginning of your <code>PATH</code> environment variable. For example, on my system, I edited the <code>.profile</code> file in my home directory and the beginning of the PATH line looks like <code>export PATH=~/anaconda/bin:/usr/local/bin:</code>, so that when I type <code>python</code> the system will use the one provided by Anaconda.</p>
<p>One of the nice things about Anaconda is that it provides an enhanced package loading system, <code>conda</code>. This is similar to R’s <code>cran</code> in some sense. But to install Keras, we’ll use Python’s <code>pip</code>, which is similar to R’s <code>devtools</code> and is run from a command line (not from within Python):</p>
<p><code>pip install keras tensorflow pydot graphviz<br>
</code><br>
should do the trick. The last two packages allow you to plot the model, though the plot is fairly boring.</p>
<p>Of the two packages (<code>keras</code> and <code>kerasR</code>), I’ve started using <code>kerasR</code> because it has some nice tutorials and it’s in CRAN, so that’s what I’ll use here. (<code>keras</code> is installed via <code>devtools</code>.)</p>
<p>In R, install packages <code>kerasR</code> and <code>reticulate</code> from CRAN. To load <code>kerasR</code> requires an extra step beyond the usual <code>library</code>:</p>
<p><code>reticulate::use_python (&quot;~/anaconda/bin/python&quot;)<br>
library (kerasR)<br>
</code></p>
<p>The <code>use_python</code> tells R where to find the Python 3 that you’re using, the one where you also <code>pip</code>-loaded <code>keras</code>. If the <code>library</code> doesn’t think for a moment and then return “successfully loaded keras”, something is wrong, and you’ll get a bunch of Python error messages which will give you a clue, if you examine them carefully. If you forget the <code>use_python</code>, R will be looking at the base-installed Python and won’t be able to <code>import keras</code> in Python. The things that can go wrong are myriad, and I can’t really be more specific, unfortunately.</p>
<p>If you got this far, you can now follow the <code>kerasR</code> tutorial. At the moment (03 June), there is an error in the first model of the tutorial (Boston Housing) that causes it to perform poorly. I’ve submitted a trouble ticket, and the code I’d recommend is:</p>
<p><code>mod &lt;- Sequential ()
<p>mod$add (Dense (units=50, input_shape=13))<br>
mod$add (Activation (&quot;relu&quot;))</p>
<p>mod$add (Dense (units=1))<br>
# mod$add (Activation (&quot;relu&quot;))</p>
<p>keras_compile (mod, loss=&quot;mse&quot;, optimizer=RMSprop())</p>
<p>boston &lt;-load_boston_housing ()<br>
X_train &lt;-scale (boston$X_train)<br>
Y_train &lt;-boston$Y_train<br>
X_test &lt;-scale (boston$X_test, center=attr (X_train, &quot;scaled:center&quot;), scale=attr (X_train, &quot;scaled:scale&quot;))<br>
Y_test &lt;-boston$Y_test</p>
<p>keras_fit (mod, X_train, Y_train, batch_size=32, epochs=200, verbose=1, validation_split=0.1)</p>
<p>pred &lt;-keras_predict (mod, X_test)<br>
sd (as.numeric (pred) - Y_test) / sd (Y_test)</p>
<p>plot (Y_test, pred)<br>
abline (0, 1)<br>
</p>
<p>Keras works a bit differently than the way R usually works in that <code>mod$add</code> modifies the model <code>mod</code> directly, in-place. The <code>mod$add</code>‘s first create an empty model (<code>Sequential ()</code>), and then add a layer with 50 hidden units and a “Relu” activation function, and then add the 1-unit output layer.</p>
<p>This is pretty much a simple Hello World model with 13 input variables and one hidden layer with 50 units. You could have made the same model in older R neural net packages, but Keras has many advantages.</p>
<p>In the tutorial (as of 03 June), the R <code>scale</code>‘s of the X training and testing data were independent and not linked. In this case, I scale the training data and then use the same <code>center</code> and <code>scale</code> for the testing data, just as you would when you deploy a model: training represents the data we already have, while testing represents new data arriving in the future. (This is a pet peeve on my part, and not generally important.)</p>
<p>More importantly, the tutorial also accidentally applied a second normalization to the data in the prediction step, which would drive it in a different direction from the training data. The version, above, has results that look pretty reasonable:<br>
<img sizes="(max-width: 440px) 100vw, 440px" data-image-description="" data-orig-file="https://thinkinator.files.wordpress.com/2017/06/bostonresults.png?w=456" data-orig-size="851,528" data-image-title="BostonResults" data-comments-opened="1" class="wp-image-1651" data-recalc-dims="1" data-attachment-id="1651" data-permalink="https://thinkinator.com/2017/06/03/deep-learning-dude-pt-1/bostonresults/" alt="BostonResults" data-medium-file="https://thinkinator.files.wordpress.com/2017/06/bostonresults.png?w=456?w=300" data-large-file="https://thinkinator.files.wordpress.com/2017/06/bostonresults.png?w=456?w=440" data-image-meta="{" src="https://thinkinator.files.wordpress.com/2017/06/bostonresults.png?w=456"></p>
<p>This example isn’t Deep Learning(tm) and you could’ve done this with older R neural net packages, but it’s just the start of Keras exploration. Follow the <code>kerasR</code> tutorials and the links they recommend. For more details on what the various <code>kerasR</code> functions do, check out the <a target="_blank" rel="nofollow" href="https://keras.io/">Keras</a> pages. (Remembering that <code>kerasR</code> doesn’t implement everything in Keras itself.)</p>
<p>For a very readable explanation of Deep Learning architectures, first read <a target="_blank" rel="nofollow" href="http://www.asimovinstitute.org/neural-network-zoo-prequel-cells-layers/">Neural Network Zoo Prequel</a>, and then <a target="_blank" rel="nofollow" href="http://www.asimovinstitute.org/neural-network-zoo/">Neural Network Zoo</a>, by the Asimov Institute.</p>
<p>One of the advantages of Keras is that it’s built on <a target="_blank" rel="nofollow" href="https://www.tensorflow.org/">Tensor Flow</a>, which takes full advantage of clusters of machines, GPUs, and all those other things that makes Google Google. Unfortunately, by “GPU” we mean Nvidia GPUs (i.e. GPUs that support CUDA). My Mac laptop has an AMD graphics chip, so I can’t use GPUs, though I can still develop things on my laptop and then someday spin up things on Amazon Web Services and use GPU-based instances.</p>
<p>Filed under: <a target="_blank" rel="nofollow" href="https://thinkinator.com/category/data-science/">Data Science</a>, <a target="_blank" rel="nofollow" href="https://thinkinator.com/category/r/">R</a>, <a target="_blank" rel="nofollow" href="https://thinkinator.com/category/rblog/">Rblog</a>, <a target="_blank" rel="nofollow" href="https://thinkinator.com/category/uncategorized/">Uncategorized</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thinkinator.wordpress.com/1553/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/thinkinator.wordpress.com/1553/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></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="https://thinkinator.com/2017/06/03/deep-learning-dude-pt-1/"> Rblog – Thinkinator</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...

	<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/SDwtQN6KnxE"></a></div></code>