<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/connecting-r-to-keras-and-tensorflow/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2017/12/11/r-and-tensorflow/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>It has always been the mission of R developers to connect R to the “good stuff”. As John Chambers puts it in his book <em><a rel="nofollow" href="http://amzn.to/2A2U1RG" target="_blank">Extending R</a></em>:</p>
<blockquote>
<p><em>One of the attractions of R has always been the ability to compute an interesting result quickly. A key motivation for the original S remains as important now: to give easy access to the best computations for understanding data.</em></p>
</blockquote>
<p>From the day it was announced a little over two years ago, it was clear that Google’s <a rel="nofollow" href="https://www.tensorflow.org/" target="_blank">TensorFlow</a> platform for <a rel="nofollow" href="https://en.wikipedia.org/wiki/Deep_learning#cite_note-dechter1986-22" target="_blank">Deep Learning</a> is good stuff. This September (see <a rel="nofollow" href="https://blog.rstudio.com/2017/09/05/keras-for-r/" target="_blank">announcment</a>), J.J. Allaire, François Chollet, and the other authors of the <a rel="nofollow" href="https://cran.r-project.org/package=keras" target="_blank">keras package</a> delivered on R’s “easy access to the best” mission in a big way. Data scientists can now build very sophisticated Deep Learning models from an R session while maintaining the <em>flow</em> that R users expect. The strategy that made this happen seems to have been straightforward. But, the smooth experience of using the <code>Keras</code> API indicates inspired programming all the way along the chain from TensorFlow to R.</p>
<div>
<h3>The Keras Strategy</h3>
<p>TensorFlow itself is implemented as a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Dataflow_programming" target="_blank">Data Flow Language</a> on a directed graph. Operations are implemented as nodes on the graph and the data, multi-dimensional arrays called “tensors”, flow over the graph as directed by control signals. An overview and some of the details of how this all happens is lucidly described in a <a rel="nofollow" href="http://delivery.acm.org/10.1145/3090000/3088527/pldiws17mapl-maplmainid2.pdf?ip=73.71.144.79&id=3088527&acc=OA&key=4D4702B0C3E38B35.4D4702B0C3E38B35.4D4702B0C3E38B35.5945DC2EABF3343C&CFID=831811081&CFTOKEN=34450892&__acm__=1512687001_5cc6d6628bb281a58e545884cba347f9" target="_blank">paper by Abadi, Isard and Murry</a> of the Google Brain Team,</p>
<div>
<img src="https://i2.wp.com/rviews.rstudio.com/post/2017-12-7-Rickert-TensorFlow_files/TF_graph.png?w=456&ssl=1" data-recalc-dims="1">
</div>
<p>and even more details and some fascinating history are contained in Peter Goldsborough’s paper, <a rel="nofollow" href="https://arxiv.org/pdf/1610.01178v1.pdf" target="_blank">A Tour of TensorFlow</a>.</p>
<p>This kind of programming will probably strike most R users as being exotic and obscure, but my guess is that because of the <a rel="nofollow" href="https://pdfs.semanticscholar.org/6869/4d0a776b55459392a1fdead1bad5266f4b38.pdf" target="_blank">long history</a> of dataflow programming and parallel computing, it was an obvious choice for the Google computer scientists who were tasked to develop a platform flexible enough to implement arbitrary algorithms, work with extremely large data sets, and be easily implementable on any kind of distributed hardware including GPUs, CPUs, and mobile devices.</p>
<p>The TensorFlow operations are written in C++, <a rel="nofollow" href="https://developer.nvidia.com/cuda-downloads" target="_blank">CUDA</a>, <a rel="nofollow" href="http://eigen.tuxfamily.org/index.php?title=Main_Page" target="_blank">Eigen</a>, and other low-level languages optimized for different operation. Users don’t directly program TensorFlow at this level. Instead, they assemble flow graphs or algorithms using a higher-level language, most commonly Python, that accesses the elementary building blocks through an <a rel="nofollow" href="https://www.tensorflow.org/api_docs/" target="_blank">API</a>.</p>
<p>The <code>keras</code> R package wraps the <a rel="nofollow" href="https://www.tensorflow.org/api_docs/" target="_blank">Keras Python Library</a> that was expressly built for developing Deep Learning Models. It supports convolutional networks (for computer vision), recurrent networks (for sequence processing), and any combination of both, as well as arbitrary network architectures: multi-input or multi-output models, layer sharing, model sharing, etc. (It should be pretty clear that the Python code that makes this all happen counts as good stuff too.)</p>
</div>
<div>
<h3>Getting Started with Keras and TensorFlow</h3>
<p>Setting up the whole shebang on your local machine couldn’t be simpler, just three lines of code:</p>
<pre><code>install.packages(&quot;keras&quot;)
library(keras)
install_keras()</code></pre>
<p>Just install and load the <code>keras</code> R package and then run the <code>keras::install_keras()</code> function, which installs TensorFlow, Python and everything else you need including a <a rel="nofollow" href="https://virtualenv.pypa.io/en/stable/" target="_blank">Virtualenv</a> or <a rel="nofollow" href="https://conda.io/docs/" target="_blank">Conda</a> environment. It just works! For instructions on installing Keras and TensorFLow on GPUs, look <a rel="nofollow" href="https://tensorflow.rstudio.com/installation_gpu.html" target="_blank">here</a>.</p>
<p>That’s it; just a few minutes and you are ready to start a hands-on exploration of the extensive documentation on the RStudio’s TensorFlow webpage <a rel="nofollow" href="https://tensorflow.rstudio.com/" target="_blank">tensorflow.rstudio.com</a>, or jump right in and build a <a rel="nofollow" href="https://tensorflow.rstudio.com/keras/" target="_blank">Deep Learning model</a> to classify the hand-written numerals using</p>
<div>
<img src="https://i2.wp.com/rviews.rstudio.com/post/2017-12-7-Rickert-TensorFlow_files/MNIST.png?w=456&ssl=1" data-recalc-dims="1">
</div>
<p>MNIST data set which comes with the <code>keras</code> package, or any one of the other twenty-five pre-built examples.</p>
</div>
<div>
<h3>Beyond Deep Learning</h3>
<p>Being able to build production-level Deep Learning applications from R is important, but Deep Learning is not the answer to everything, and TensorFlow is bigger than Deep Learning. The really big ideas around TensorFlow are: (1) TensorFlow is a general-purpose platform for building large, distributed applications on a wide range of cluster architectures, and (2) while data flow programming takes some getting used to, TensorFlow was designed for algorithm development with big data.</p>
<p>Two additional R packages make general modeling and algorithm development in TensorFlow accessible to R users.</p>
<p>The <a rel="nofollow" href="https://github.com/rstudio/tfestimators" target="_blank"><code>tfestimators</code></a> package, currently on GitHub, provides an interface to Google’s <a rel="nofollow" href="https://www.tensorflow.org/programmers_guide/estimators" target="_blank">Estimators</a> API, which provides access to pre-built TensorFlow models including SVM’s, Random Forests and KMeans. The architecture of the API looks something like this:</p>
<div>
<img src="https://i2.wp.com/rviews.rstudio.com/post/2017-12-7-Rickert-TensorFlow_files/tfestimators.png?w=456&ssl=1" data-recalc-dims="1">
</div>
<p>There are several layers in the stack, but execution on the small models I am running locally goes quickly. Look <a rel="nofollow" href="https://tensorflow.rstudio.com/tfestimators/" target="_blank">here</a> for documentation and sample models that you can run yourself.</p>
<p>At the deepest level, the <a rel="nofollow" href="https://cran.r-project.org/package=tensorflow" target="_blank"><code>tensorflow</code></a> package provides an interface to the core <a rel="nofollow" href="https://www.tensorflow.org/api_docs/python/" target="_blank">TensorFlow API</a>, which comprises a set of Python modules that enable constructing and executing TensorFlow graphs. The documentation on the package’s <a rel="nofollow" href="https://tensorflow.rstudio.com/tensorflow/articles/tutorial_mnist_pros.html" target="_blank">webpage</a> is impressive, containing tutorials for different levels of expertise, several examples, and references for further reading. The <a rel="nofollow" href="https://tensorflow.rstudio.com/tensorflow/articles/tutorial_mnist_beginners.html" target="_blank">MNIST for ML Beginners</a> tutorial works through the classification problem described above in terms of the Keras interface at a low level that works through the details of a softmax regression.</p>
<div>
<img src="https://i0.wp.com/rviews.rstudio.com/post/2017-12-7-Rickert-TensorFlow_files/softmax.png?w=456&ssl=1" data-recalc-dims="1">
</div>
<p>While Deep Learning is sure to capture most of the R to TensorFlow attention in the near term, I think having easy access to a big league computational platform will turn out to be the most important benefit to R users in the long run.</p>
<p>As a final thought, I am very much enjoying reading the <a rel="nofollow" href="https://www.manning.com/books/deep-learning-with-r" target="_blank">MEAP</a> from the forthcoming Manning Book, <em>Deep Learning with R</em> by François Chollet, the creator of Keras, and J.J. Allaire. It is a really good read, masterfully balancing theory and hands-on practice, that ought to be helpful to anyone interested in Deep Learning and TensorFlow.</p>
</div>
<p> </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://rviews.rstudio.com/2017/12/11/r-and-tensorflow/"> R Views</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/up9bPYfrimc" height="1"></a></div>