<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/fitting-a-tensorflow-linear-classifier-with-tfestimators/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2018/01/12/linear-model-in-tensorflow/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In a <a rel="nofollow" href="https://rviews.rstudio.com/2017/12/11/r-and-tensorflow/" target="_blank">recent post</a>, I mentioned three avenues for working with TensorFlow from R:<br>
* The <a rel="nofollow" href="https://cran.r-project.org/web/packages/keras/index.html" target="_blank"><code>keras</code> package</a>, which uses the <a rel="nofollow" href="https://keras.io/" target="_blank">Keras API</a> for building scaleable, deep learning models * The <a rel="nofollow" href="https://cran.r-project.org/package=tfestimators" target="_blank"><code>tfestimators</code> package</a>, which wraps Google’s <a rel="nofollow" href="https://www.tensorflow.org/programmers_guide/estimators" target="_blank">Estimators API</a> for fitting models with pre-built estimators<br>
* The <a rel="nofollow" href="https://cran.r-project.org/package=tensorflow" target="_blank"><code>tensorflow</code> package</a>, which provides an interface to Google’s low-level <a rel="nofollow" href="https://www.tensorflow.org/api_docs/python/" target="_blank">TensorFlow API</a></p>
<p>In this post, Edgar and I use the <code>linear_classifier()</code> function, one of six pre-built models currently in the <code>tfestimators</code> package, to train a linear classifier using data from the <a rel="nofollow" href="https://cran.r-project.org/web/packages/titanic/index.html" target="_blank"><code>titanic</code></a> package.</p>
<div>
<img src="https://i1.wp.com/rviews.rstudio.com/post/2018-01-08-tfestimators_files/estimators.png?w=456&ssl=1" data-recalc-dims="1">
</div>
<pre><code>library(tfestimators)
library(tensorflow)
library(tidyverse)
library(titanic)</code></pre>
<p>The <code>titanic_train</code> data set contains 12 fields of information on 891 passengers from the Titanic. First, we load the data, split it into training and test sets, and have a look at it.</p>
<pre><code>titanic_set &lt;- titanic_train %>% filter(!is.na(Age))

# Split the data into training and test data sets
indices &lt;- sample(1:nrow(titanic_set), size = 0.80 * nrow(titanic_set))
train &lt;- titanic_set[indices, ]
test  &lt;- titanic_set[-indices, ]

glimpse(titanic_set)</code></pre>
<pre><code>## Observations: 714
## Variables: 12
## $ PassengerId </code></pre>