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

Install TensorFlow for R

(Subject: Data Analytics/Authored by: Liping Liu on 5/13/2024 5:00:00 AM)/Views: 5124
Blog    News    Post   

To install Tensor Flow for R, we will have to install Python first. If you don't have Python, follow this guideline to install and configure it: https://www.ecourse.org/news.asp?which=6501. In addition, we might install Jupyter Notebook and Jupyter Lab as well in case we want to run Python code. See my other blog on how to install Tensor Flow with GPU support here if just want to run machine learning algorithms in Python. Another option for installing Python with Jupyter is to install Anaconda, which includes Python and options to install Jupyter and other packages later. I will have another blog to detail the steps. 

Install Tensor Flow in R for Windows:

  1. Check Python installation: Open Windows Command Prompt (type cmd in Windows search Box to find Windows Command Prompt) and type "python --version" to make sure you have Python 3.9 installed. If you don't have Python, follow this guideline to install and configure it: https://www.ecourse.org/news.asp?which=6501
  2. Open R to install keras package and run install_keras() command:

    install.pakages("keras")

    library(keras)

    install_keras()

  3.  Test your installation, 

    model = keras_model_sequential()

    layer_dense(model, units = 64, input_shape = 3, activation="sigmoid")

    summary(model)

In case of errors, follow a more complex procedure in the second part of this file for remedies.

 

Install Tensor Flow for R in MacOS

First make sure you have the most recent version of Xcode is installed: Go to terminal windows, and issue the following commands:

sudo rm -rf /Library/Developer/CommandLineTools

xcode-select --install

Then, you can go to R and issue the following commands one by one:

install.packages("remotes")

remotes::install_github("rstudio/tensorflow")

reticulate::install_python()

install.packages("keras")

library(keras)

install_keras()

 

Install Tensor Flow for R in Windows 11 (in case of errors when following prior instructions):

  1. Open R
  2. Install Tensor Flow Package by command: 

    install.packages("keras")

    library(keras)

    #assume your installed python into C:\Program Files\Python39\

    reticulate::use_python("C:\\Program Files\\Python39\\python.exe")

    reticulate::py_install("tensorflow==2.10")

    reticulate::py_install("keras")

  3.   Restart R

  4.  Type commands:

    library(keras)

    install_keras()

   5. Test the installation with commands: type the following commands in R

    model = keras_model_sequential()

    layer_dense(model, units = 64, input_shape = 3, activation="sigmoid")

    summary(model)

 

Errors and Fixes:

  1. If you get error messages saying "multiple virtualenv exist or default vitualenv not compatible" or something like that, go to your R working directory (you can find it out by the command getwd() in R), go the directory, delete .virtualenv folder. And then perform the installation as normal. 

Use Conda Tensor Flow Environment with GPU Acceleration in R

If you followed https://www.ecourse.org/news.asp?which=6241 and installed either anaconda or miniconda and created an environment with Tensor Flow with GPU support in it, you can then share the same environment with R. 

  1. If you have not done so, add the conda binary source folder to the system Path: assume the binary folder is c:\ProgramData\miniconda3\condabin, download AddCondaPath.zip, extract AddCondaPath.bat file, right click on the batch file to run as administrator. 
  2. Open RStudio and go to Tools --> Global Options ... --> Python --> Select --> Conda Environments to select the one with tensor flow package as the default tensor flow environment to run.
  3. Load the following libraries: reticulate and keras, and you are ready to use tensor flow with GPU support in R.

           Register

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

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