Home School Support
 
STUDENT
 
FACULTY
 
SCHOOL
 
SUPPORT
 
PUBLIC
 
SIGNUP
DAILY QUIZ
 
     
  B U L L E T I N    B O A R D

Use TensorFlow Package for Deep Learning in R

(Subject: Data Analytics/Authored by: Liping Liu on 5/4/2023 4:00:00 AM)/Views: 2481
Blog    News    Post   

Introduction

TensorFlow is a python library for numerical computation using data flow graphs, where nodes in the graph represent mathematical operations, and edges represent the multidimensional data arrays (tensors) communicated between the nodes. Its architecture allows us to deploy computation to one or more CPUs or GPUs in a desktop, server, or cloud services with a single API. TensorFlow was originally developed by researchers and engineers working on the Google Brain Team within Google's Machine Intelligence research organization for the purposes of conducting machine learning and deep neural networks research, but the system is general enough to be applicable in a wide variety of other domains as well.

Installation

The default build of TensorFlow will use an NVIDIA® GPU for MacOS and Linux if it is available and the appropriate drivers are installed, and otherwise fallback to using the CPU only.

The prerequisites for the GPU version of TensorFlow on each platform are covered below. Note that on all platforms (except macOS) you must be running an NVIDIA® GPU with CUDA® Compute Capability 3.5 or higher. See the list of CUDA-enabled GPU cards.

To enable TensorFlow to use a local NVIDIA® GPU, you can install the following:

Note that the following instructions require the development version of the tensorflow R package installed:

remotes::install_github("rstudio/tensorflow")

Prior to the installation of the tensorflow package in R, we need to install Python (version 3.9, 3.10, 0r 3.11) and Anaconda Python because the R package is ported from the tensorflow Python package. 

        install.packages("remotes")

        library(remotes)

        remotes::install_github("rstudio/tensorflow")

  1. Load tensorflow library and run the install_tensorflow() function. This function is a wrapper of many steps required to install TensorFlow.

        library(tensorflow)

        install_tensorflow(version="2.10", gpu=TRUE)

Finally, we can confirm that the installation succeeded with:

library(tensorflow)
tf$constant("Hellow Tensorflow")

We can verify the GPU support by the following command that will list your GPUs available:

library(tensorflow)
tf$config$list_physical_devices("GPU")

By default, install_tensorflow() install the latest release version of TensorFlow. You can override this behavior by specifying the version parameter. For example:

install_tensorflow(version = "2.10.0")

Note that the version should be a full major.minor.patch version specification (rather than just major and minor versions).

You can install the nightly build of TensorFlow (CPU or GPU version) with:

install_tensorflow(version = "nightly") # cpu version

install_tensorflow(version = "nightly-gpu") # gpu version

It’s recommended that we run deep-learning code on a modern NVIDIA GPU like Tesla. Some applications – in particular, image processing with convolutional networks and sequence processing with recurrent neural networks – will be excruciatingly slow on CPU, even a fast multicore CPU. For applications that can realistically be run on CPU, you’ll generally see speed increase by a factor or 5 or 10 by using a modern GPU.

Prerequisites for the GPU version of TensorFlow in Windows:

The following article describes how to detect whether your graphics card uses an NVIDIA® GPU:

http://nvidia.custhelp.com/app/answers/detail/a_id/2040/~/identifying-the-graphics-card-model-and-device-id-in-a-pc

Once you’ve confirmed that you have an NVIDIA® GPU, the following article describes how to install required software components including the CUDA Toolkit, NVIDIA® drivers, and cuDNN. See 

https://www.tensorflow.org/install/gpu#hardware_requirements for details.

Note that the documentation on installation of the last component (cuDNN v7.4.1) is a bit sparse. Once you join the NVIDIA® developer program and download the zip file containing cuDNN you need to extract the zip file and add the location where you extracted it to your system PATH

Once you’ve met the prerequisites installing the GPU version in a single-user / desktop environment is as simple as:

library(tensorflow)

install_tensorflow(version = "gpu")

If you are using Keras you can install both Keras and the GPU version of TensorFlow with:

library(keras)
install_keras(tensorflow = "gpu")

 

To be verified installation using devtools:

Installing GPU version of TensorFlow™ for use in R on Windows

Intro 

The other night I got TensorFlow™ (TF) and Keras-based text classifier in R to successfully run on my gaming PC that has Windows 10 and an NVIDIA GeForce GTX 980 graphics card, so I figured I’d write up a full walkthrough, since I had to make minor detours and the official instructions assume – in my opinion – a certain level of knowledge that might make the process inaccessible to some folks.

Why would you want to install and use the GPU version of TF? “TensorFlow programs typically run significantly faster on a GPU than on a CPU.” Graphics processing units (GPUs) are typically used to render 3D graphics for video games. As a result of the race for real-time rendering of more and more realistic-looking scenes, they have gotten really good at performing vector/matrix operations and linear algebra. While CPUs are still better for general purpose computing and there is some overhead in transferring data to/from the GPU’s memory, GPUs are a more powerful resource for performing those particular calculations.

Notes: For installing on Ubuntu, you can follow RStudio’s instructions. If you’re interested in a Python-only (sans R) installation on Linux, follow these instructions.

Prerequisites 

  • An NVIDIA GPU with CUDA Compute Capability 3.0 or higher. Check your GPU’s compute capability here. For more details, refer to Requirements to run TensorFlow with GPU support.
  • A recent version of R – latest version is 3.4.0 at the time of writing.
    • For example, I like using Microsoft R Open (MRO) on my gaming PC with a multi-core CPU because MRO includes and links to the multi-threaded Intel Math Kernel Library (MKL), which parallelizes vector/matrix operations.
    • I also recommend installing and using the RStudio IDE.
    • You will need devtoolsinstall.packages("devtools", repos = c(CRAN = "https://cran.rstudio.com"))
  • Python 3.5 (required for TF at the time of writing) via Anaconda (recommended):
    1. Install Anaconda3 (in my case it was Anaconda3 4.4.0), which will install Python 3.6 (at the time of writing) but we’ll take care of that.
    2. Add Anaconda3 and Anaconda3/Scripts to your PATH environment variable so that python.exe and pip.exe could be found, in case you did not check that option during the installation process. (See [these instructions][19] for how to do that.)
    3. Install Python 3.5 by opening up the Anaconda Prompt (look for it in the Anaconda folder in the Start menu) and running conda install python=3.5
    4. Verify by running python --version

Setting Up 

CUDA & cuDNN 

Step 0: Presumably you’ve got the latest NVIDIA drivers.

  1. Install CUDA Toolkit 8.0 (or later).
  2. Download and extract CUDA Deep Neural Network library (cuDNN) v5.1 (specifically), which requires signing up for a free NVIDIA Developer account.
  3. Add the path to the bin directory (where the DLL is) to the PATH system environment variable. (See these instructions for how to do that.) For example, mine is C:\cudnn-8.0\bin

TF & Keras in R 

Once you’ve got R, Python 3.5, CUDA, and cuDNN installed and configured:

You may need to install the dev version of the processx packagedevtools::install_github("r-lib/processx") because everything installed OK for me originally but when I ran devtools::update_packages() it gave me an error about processx missing, so I’m including this optional step.

  1. Install reticulate package for interfacing with Python in R: devtools::install_github("rstudio/reticulate")
  2. Install tensorflow packagedevtools::install_github("rstudio/tensorflow")
  3. Install GPU version of TF (see this page for more details):
    library(tensorflow)
    install_tensorflow(gpu = TRUE)
    
  4. Verify by running:
    use_condaenv("r-tensorflow")
    sess = tf$Session()
    hello 
  5. Install keras packagedevtools::install_github("rstudio/keras")

You should be able to run RStudio’s examples now.

 

Using Tensor Flow

First, we need to install the keras package, which is the high-level API of TensorFlow 2. This short introduction uses Keras to:

  1. Build a neural network that classifies images.
  2. Train this neural network.
  3. And, finally, evaluate the accuracy of the model.
  4. Save and restore the created model.

 

Let’s start by loading and preparing the MNIST dataset. The values of thee pixels are integers between 0 and 255 and we will convert them to floats between 0 and 1.

library(keras)

mnist = dataset_mnist()

mnist$train$x = mnist$train$x/255

mnist$test$x = mnist$test$x/255

Now, let’s define the a Keras model using the sequential API.

model = keras_model_sequential() 

model = layer_flatten(model, input_shape = c(28, 28))

model = layer_dense (model, units = 128, activation = "relu")

model = layer_dropout(model, 0.2

model = layer_dense(model, 10, activation = "softmax")

When using the Sequential API, the first layer must specify the input_shape argument which represents the dimensions of the input. In the above case, images 28x28.

After defining the model, you can see information about layers, number of parameters, etc. with the summary function:

summary(model)

## Model: "sequential"
## ___________________________________________________________________________
## Layer (type)                     Output Shape                  Param #     
## ===========================================================================
## flatten (Flatten)                (None, 784)                   0           
## ___________________________________________________________________________
## dense (Dense)                    (None, 128)                   100480      
## ___________________________________________________________________________
## dropout (Dropout)                (None, 128)                   0           
## ___________________________________________________________________________
## dense_1 (Dense)                  (None, 10)                    1290        
## ===========================================================================
## Total params: 101,770
## Trainable params: 101,770
## Non-trainable params: 0
## ___________________________________________________________________________

The next step after building the model is to compile it. It’s at compile time that we define what loss will be optimized and what optimizer will be used. You can also specify metrics, callbacks and etc that are meant to be run during the model fitting.

Compiling is done with the compile function: 

compile(model, loss = "sparse_categorical_crossentropy", optimizer = "adam", metrics = "accuracy")

Note that compile and fit (which we are going to see next) modify the model object in place, unlike most R functions.

Now let’s fit our model:

history = fit(model, x = mnist$train$x, y = mnist$train$y, epochs = 5,validation_split = 0.3, verbose = 2)

## Train on 42000 samples, validate on 18000 samples
## Epoch 1/5
## 42000/42000 - 3s - loss: 0.3442 - accuracy: 0.9008 - val_loss: 0.1780 - val_accuracy: 0.9484
## Epoch 2/5
## 42000/42000 - 3s - loss: 0.1682 - accuracy: 0.9498 - val_loss: 0.1356 - val_accuracy: 0.9599
## Epoch 3/5
## 42000/42000 - 3s - loss: 0.1242 - accuracy: 0.9626 - val_loss: 0.1233 - val_accuracy: 0.9622
## Epoch 4/5
## 42000/42000 - 3s - loss: 0.0999 - accuracy: 0.9697 - val_loss: 0.1072 - val_accuracy: 0.9685
## Epoch 5/5
## 42000/42000 - 3s - loss: 0.0834 - accuracy: 0.9739 - val_loss: 0.0966 - val_accuracy: 0.9731

Plot the training history. We will see loss, val_loss, accuracy, and val_accuracy over the training epochs. The values val_loss and val_accuracy are computed using evaluation training set. 

plot(history)

We can also evaluate the model using test dataset:

evaluate(model, mnist$test$x, mnist$test$y)

We can now make predictions with our model using the predict function:

predictions = predict(model, mnist$test$x)
head(predictions, 2)
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 1.079081e-07 1.105458e-08 4.597065e-05 2.821549e-04 5.768893e-11
## [2,] 2.735454e-06 6.786310e-04 9.992226e-01 8.388522e-05 3.788405e-13
##              [,6]         [,7]         [,8]         [,9]        [,10]
## [1,] 5.044960e-07 3.673492e-14 9.996552e-01 4.329958e-07 1.558235e-05
## [2,] 4.735405e-08 1.990466e-07 3.531684e-11 1.182519e-05 3.717427e-13

By default predict will return the output of the last Keras layer. In our case this is the probability for each class. You can also use predict_classes and predict_proba to generate class and probability - these functions are slighly different then predict since they will be run in batches.

You can access the model performance on a different dataset using the evaluate function, for example:

evaluate(model, mnist$test$x, mnist$test$y, verbose = 0)
## $loss
## [1] 0.0833252
## 
## $accuracy
## [1] 0.9741

Our model achieved ~90% accuracy on the test set.

Unlike models built with the lm function, to save Keras models for later prediction, you need to use specialized functions, like save_model_tf:

 save_model_tf(object = model, filepath = "model")

The above model will be saved into the folder called "model" inside the working directory. You can then reload the model and make predictions with:

reloaded_model = load_model_tf("model")
all.equal(predict(model, minist$test$x), predict(reloaded_model, mnist$test$x)


           Register

Blog    News    Post
 
     
 
Blog Posts    News Digest    Contact Us    About Developer    Privacy Policy

©1997-2025 ecourse.org. All rights reserved.