Deep Learning: Installing TensorFlow
Purpose: We are about to discuss neural networks and deep learning. These are advanced structures which are built on specialized libraries. While {tidymodels}
supports some of the most basic neural network architecture, we’ll be much better off if we work in something which has been built for the purpose of constructing and utilizing deep learning networks. In this notebook, you’ll install {keras}
and {tensorflow}
. The functionality from {tensorflow}
was built in python
, so we’ll need python installed as well.
Setup
Follow the instructions to install Keras and Tensorflow here. Note, the instructions seemed to be somewhat finicky. Here is what has worked for me.
- Install TensorFlow with
install.packages("tensorflow")
- Run
library(reticulate)
– install the{reticulate}
package if you don’t have it already. This will allow you to run Python and R together in a single notebook and pass objects between environments. - Open your terminal using the terminal tab in the bottom-left pane of RStudio and type
python
and run the command by hittingEnter
/Return
.
If you get a Python interpreter – indicated by
>>>
– then you have python installed on your computer.- In this case, run
import sys
next to your python prompt. - Next run
sys.executable
to discover the path to your python executable. - Now, back in your R Console (click on the Console tab), define
path_to_python
as the path you’ve discovered. Don’t forget to use quotes to surround the file path.
- In this case, run
If you don’t get the python prompt, then you’ll need to install python.
- Define
path_to_python <- install_python()
in your console and run it.
- Define
- In your Console, run
virtualenv_create("r-reticulate", python = path_to_python)
. This will create a python virtual environment and package container in your working directory. - Install the
{keras}
package and then load the{keras}
library. - Install TensorFlow, SciPy, and several TensorFlow data sets using
install_keras(method = "virtualenv", envname = "r-reticulate")
. - Completely shut down and reopen RStudio. You might try clicking on Session and Restart R, but this only sometimes worked for me.
- Reopen RStudio and your Quarto notebook.
- In the notebook, load the
{tensorflow}
and{reticulate}
libraries. - Type and run
use_virtualenv("r-reticulate")
. - Run
tf$constant("Hello TensorFlow!")
- In addition to a notification, you should see
tf.Tensor(b'Hello TensorFlow!', shape = (), dtype = string)
.
Summary
You now have TensorFlow installed. This is a state of the art software for training and utilizing deep learning networks which was developed at Google.