How to Set Up TensorFlow 2 in R in 5 Minutes (BONUS Image Recognition Tutorial)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The 2 most popular data science languages – Python and R – are often pitted as rivals. This couldn’t be further from the truth. Data scientists that learn to use the strengths of both languages are valuable because they have NO LIMITS.
- Machine Learning and Deep Learning: They can switch to Python to leverage
scikit learn
andtensorflow
. - Data Wrangling, Visualization, Apps & Reporting: They can quickly change to R to use
tidyverse
,shiny
andrmarkdown
.
The bottom line is that knowing both R
and Python
makes you SUPER PRODUCTIVE.
Article Updates:
-
This article has been updated. View the updated TensorFlow article at Business Science.
-
Check out this article to set up Scikit Learn in a Python 3.8 Environment.
Have 5 minutes?
Then let’s set up TensorFlow 2 for Deep Learning
We’re going to go through the essential setup tips of the PRO’s – those that use Python from R via reticulate
.
-
Install
TensorFlow
in R -
Do a Image Recognition Analysis to verify
TensorFlow
is working.
Here’s the BONUS Image Reconition Tutorial. You’ll classify Fashion Images.
TensorFlow Ankle Boot Classification – Tutorial to Test if TF is Working
Using TensorFlow & R
How do you use them together in Business Projects?
Setting up TensorFlow
in R
is an insane productivity booster. You can leverage the best of Python + R. But you still need to learn how to use Python and R together for real business projects. And, it’s impossible to teach you all the in’s and out’s in 1 short article. But, I have great news!
I just launched a NEW 5-PART PYTHON + R LEARNING LAB SERIES (Register Here) that will show you how to use Python
and R
together on Real Business Projects:
- Lab 34: Human Resources Employee Clustering
- Lab 35: Advanced Customer Segmentation and Market Basket Analysis
- Lab 36: TensorFlow for Finance
- Lab 37: TensorFlow for Energy Demand Forecasting
- Lab 38: Social Media Text Analytics!
And it’s FREE to attend live.
Register here to attend Python + R Learning Labs live for free. I’ll notify you in advance of the accelerated 1-hour courses that you can attend via webinar.
Installing TensorFlow in R
This process should take under 5-minutes. First we have some requirments to get TensorFlow 2
installed.
TensorFlow 2.0.0 Requirements
These may (will) change in the future, but currently the requirements are:
- Python 3.5-3.7
- Windows 7 or Later
- MacOS 10.12.6 or Later
- Ubunto 16.04 or Later
If you’ve followed the Scikit Learn in R tutorial, we used Python 3.8 (latest stable version). We can’t use Python 3.8 for TensorFlow, so we need to create a new environment. We’ll use Python 3.6 in this tutorial.
Conda Requirements
If you don’t have Conda installed, please install here: Anaconda Installation.
Installing TensorFlow in R with reticulate
Do this in R. Install and load tidyverse
, reticulate
, and tensorflow
.
Next, run install_tensorflow()
in your R environment. This will take about 3-5 minutes to install TensorFlow
in a new Conda Environment named “py3.6”.
Side note: You can actually specify which TensorFlow version to install with the version
arg. This can be helpful to switch from the CPU vesion to GPU version (greater power, greater responsibility) or to access older versions of TF.
We can check to see that py3.6
conda environment has been created.
Next, we tell reticulate
to use the py3.6
conda environment.
Congrats on the Installation is now complete! – Now Let’s Use TensorFlow
to classify images.
Image Recognition Analysis
To Verify TensorFlow is Working
Let’s run through a short image recognition tutorial. This tutorial comes from Google’s Basic classification: Classify images of clothing
Step 1 – Make a Python Code Chunk
Use Pro-Tip #1 Below to make a “Python Code Chunk”.
Python Code Chunk
Step 2 – Import Libraries
Import the libraries needed:
- Deep Learning:
tensorflow
andkeras
- Math:
numpy
- Visualization:
matplotlib
Check the version of tensorflow
to make sure we’re using 2.0.0+.
Step 3 – Load the Fashion Images
Load the fashion_mnist
dataset from keras
.
We have 60,000 training images that have been labeled.
We can check the unique labels to see what classifications the images belong to. Note that these are numeric values ranging from 0 to 9.
The corresponding labels are:
We can see what the first image looks like using matplotlib
.
And we can also check out the first 25 images.
Step 4 – Modeling with Keras
Make a keras
model using the Sequential()
with 3 steps: Flatten, Dense, and Dense.
Next, compile
the model with the “adam” optimizer.
Inspect the model summary.
Step 5 – Fit the Keras Model
CRITICAL STEP – Fit the model. Make sure this step works!
TensorFlow Model Training
Step 6 – Training History
I’ll plot this using R. Note – This is an R Code Chunk (not a Python Code Chunk).
Step 7 – Test Accuracy
Evaluate accuracy on the out-of-sample images.
Step 8 – Make Predictions
The model produces linear outputs cakked “logits”. The softmax layer to converts the logits to probabilities.
We can then classify all of the test images (held out)
We can make a prediction for the first image.
Use np.argmax()
to determine which index has the highest probability.
The index value can be retrieved with np.max()
.
Get the class name.
And visualize the image.
Nice work – If you made it through this tutorial unscathed, then you are doing well! And your ready for the TensorFlow Learning Labs.
Pro Tips (Python in R)
Now that you have python
running in R
, use these pro-tips to make your experience way more enjoyable.
Pro-Tip #1 – Python Chunk Keyboard Shortcut
I can’t stress this one enough – Set up a Keyboard shortcut for Python Code Chunks. This is a massive productivity booster for Rmarkdown documents.
- My preference:
Ctrl + Alt + P
When you hit Ctrl + Alt + P
, a {python}
code chunk will appear in your R Markdown document.
Pro-Tip #2 – Use Python Interactively
For debugging Python Code Chunks in R Markdown, it can help to use the repl_python()
to convert your Console to a Python Code Console. To do so:
- In R Console, you can run python interactively using
repl_python()
. You will see>>>
indicating you are in Python Mode. - Make sure the correct Python / Conda Environment is selected.
- To escape Python in the console, just hit
escape
.
Pro-Tip #3 – My Top 4 Conda Terminal Commands
At some point you will need to create, modify, add more packages to your Conda Environment(s). Here are 4 useful commands:
- Run
conda env list
to list the available conda environments - Run
conda activate <env_name>
to activate a conda environment - Run
conda update --all
to update allpython
packages in a conda environment. - Run
conda install <package_name>
to install a new package
Use Python inside Shiny Apps
Up until now we haven’t talked about Shiny
– the web application framework that is used to take your python
and R
machine learning models into Production.
Business Science Application Library
A Meta-Application that houses Shiny Apps
R Shiny needs to be in your toolbox if you want to productionize Data Science. You simply cannot put machine learning applications into production with other “BI” Tools like Tableau, PowerBI, and QlikView.
CRITICAL POINT: You can USE SHINY to productionize Scikit Learn
and TensorFlow
models.
If you need to learn R Shiny as fast as possible, I have the perfect program for you. It will accelerate your career. The 4-Course R-Track Bundle through Business Science.
Have questions on using Python + R?
Make a comment in the chat below. ????
And, if you plan on using Python
+ R
at work, it’s a no-brainer – attend my Learning Labs (they are FREE to attend live).
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.