0% found this document useful (0 votes)
4 views17 pages

Algoritma Handbook Guide - Keras in R

This document provides a comprehensive guide for installing Keras in R, including the necessary steps for installing Python via Anaconda and verifying the installations. It details the process of setting up the Keras package in R, creating a conda environment, and troubleshooting common installation errors. The guide is aimed at ensuring users can successfully run Keras in their R environment.

Uploaded by

Lycia Pricia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views17 pages

Algoritma Handbook Guide - Keras in R

This document provides a comprehensive guide for installing Keras in R, including the necessary steps for installing Python via Anaconda and verifying the installations. It details the process of setting up the Keras package in R, creating a conda environment, and troubleshooting common installation errors. The guide is aimed at ensuring users can successfully run Keras in their R environment.

Uploaded by

Lycia Pricia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Installing Keras in R

Handbook Guide
September 2022

[Link] © Copyright - Algoritma 2022


Contents
Python Installation 2

● Installing Python using Anaconda


● Verify Python Installation

`keras` Installation 8

● Installing `keras` in R
● Verify `keras` Installation
● Common error in `keras` installation

1
Python Installation
Installing Python using Anaconda

For our Python installation, we will be using and installing a package manager named
Anaconda. With Anaconda, users will not only have Python installed but also will have
the necessary packages (i.e. numpy, pandas) utilized in our workshops. Also, Anaconda
would have included the installation of Jupyter - an open-source web application that
allows you to create and share Python code. Once opening the link below, please
choose Python version 3.8 for installation.

Use this link: [Link]

- Choose your appropriate Operating System, make sure you choose the version
that is compatible with your pc's bit-rate

- Install Anaconda

2
- For windows users make sure you check Add Anaconda to my PATH to the
environment and then wait until the installation finished.

3
More info on Anaconda:
[Link]

Warning: For Windows operating systems, if you can’t find the conda command from your Command
Prompt please add the C:\User\Anaconda3\ and the C:\User\Anaconda3\Scripts\ to the environment
variable as shown here:
[Link]

● For Mac OS X and Linux-based OS: Open “Terminal”


● For Windows: Open “Command Prompt”

Verify Python Installation:

1. Type the command python


2. If the installation was completed successfully, there should be a response which
includes information on which Python version was installed as shown below. In
this case, it appears the user installed Python version 2.7.13. But make sure yours
is 3.8
3. To exit, enter the command quit() or use Ctrl-D

Figure 1: python Response on Mac OS X Terminal

4
Figure 2: python Response on Windows Command Prompt

Verify Anaconda Installation

1. Type the command conda list in your “Terminal” or “Command Prompt”.


2. not give any response, please check the Warning in the installation section, if the
problem still persists please contact our teaching assistants for help.

5
Figure 3: conda list Response on Mac OS X Terminal

6
Figure 4: conda list Response on Windows Command Prompt

7
`keras` Installation
Installing `keras` in R

- Open your R or Rstudio and you can run:

1. Install `keras` package you can use your UI or type `[Link]("keras")`

Figure 5 : Installing keras in rstudio console

2. Go to the anaconda prompt if you are windows user or terminal if you are
linux/mac user.

3. Type `conda create -n r-tensorflow python=3.7`

Figure 6 : creating new conda environment

4. Type `y`

5. Type `Conda activate r-tensorflow`

8
6. Installing tensorflow 2.0. Type `pip install tensorflow==2.0`

Figure 7 : Installing Tensorflow library version 2.0

7. Install protobuff 3.20. Type `pip install protobuf==3.20` .

Figure 8 : Installing protobuff version 3.20

8. Back to your Rstudio and Restart your R.

Figure 9: Restarting your R

9
9. Setting up your conda environment to rstudio
- Go to tools choose global option > python > conda environment

Figure 10: Going to global option

10
- Choose python then click select

Figure 11: Going to python option

11
- Choose the conda environment, then choose the ‘r-tensorflow`
environment you installed earlier. Then press select.

Figure 12: Choosing “r-tensorflow” environment

- Press apply then restart your R again.

Verify `keras` Installation:

1. In your console type `library(keras)`

12
2. To check if keras is ready, load keras library in R then try:

`model <- keras_model_sequential()`

Figure 13 : Verifying your `keras` installation

3. If there is no error, then it is ready to use

13
Common Installation Error Solution
Note: Usually, the installation process ends here. But some users might find problems
when they run keras_model_sequential( ). It’s because R use python version 3.8 as
default python to run reticulate (package to use python and its environment in R). If you
have problem as the picture below, please continue this additional step.

4. Make sure the tensorflow 2.0 is successfully installed in r-tensorflow environment


by using this step
a. Open command prompt (windows) or terminal (Mac Os)
b. Run “conda activate r-tensorflow” then “conda list”
c. The terminal will print default python packages that are installed in the
environment. If you found tensorflow version 2.0, then its successfully
installed and you can continue to the next step

Windows:
5. Close the command prompt and re-open then run “conda env list”
6. Copy the directory of r-tensorflow like the picture below

14
7. Open R studio then run this code sequentially. You can run in the console or
create new chunk
reticulate::use_python(“directory r-tensorflow2”,required=TRUE) *note:
change \ to/*
library(keras)
Model <- keras_model_sequential

Example:

8. If there’s no error when running keras_model_sequential(), then you good to go

MacOs
1. After you make sure the tensorflow 2.0 is installed in r-tensorflow environment, run this
following code sequentially. You can run in the console or create new chunk

library(tensorflow)
use_condaenv(“r-tensorflow2”)
library(keras)
Model <- keras_model_sequental()

2. If there’s no error when running keras_model_sequential(), then you are good to go

15
16

You might also like