Zeus
Zeus

Reputation: 1590

How to install package keras in R

I'm trying to install deep learning package keras on RStudio using this website. I installed keras using

install.packages("keras") 
library(keras)
install_keras()

but when I tried to open the MNIST dataset

mnist <- dataset_mnist()

I keep getting the error

Error: ModuleNotFoundError: No module named 'absl'

I thought keras installed tensorflow but do I need to install tensorflow separately?

Upvotes: 4

Views: 7581

Answers (6)

dmetivie
dmetivie

Reputation: 1

For me it was really like @pari said in the accepted answer but in addition I had to force the default Python version to downgrade (3.12 -> 3.9).

install.packages("keras")
install_keras(python_version = "3.9")

It is confusing that this keyword argument does not appear in the official doc but only in the tensorflow::install_tensorflow doc

Upvotes: 0

mariko
mariko

Reputation: 216

I had the same problem, but mine was solved by enclosing keras in double quotes.

    install.packages("keras")    ## worked for me, 

    install.packages(keras)      ## never worked.

Upvotes: 2

Spike
Spike

Reputation: 23

try:

    install.packages("devtools")
    devtools::install_github("rstudio/keras")
    library(keras)
    mnist<-dataset_mnist()

Upvotes: 1

Nilrey Jim Cornites
Nilrey Jim Cornites

Reputation: 41

If you follow the TUT and still got error, try running py_config() and check the python and libpython if it is pointing to an r-tensorflow environment. If not, best to try manually install keras in your manually set up conda environment.

Step 1: Install keras in your R just like in the link above.

#Open rstudio and run the following command
devtools::install_github("rstudio/keras") 
#Don't close rstudio after running this, okay?

Step 2: Manually install keras (and tensorflow) in your machine ##. When i say “manual” it means using python specifically through conda. Here’s the link I followed: https://medium.com/i-want-to-be-the-very-best/installing-keras-tensorflow-using-anaconda-for-machine-learning-44ab28ff39cb .

In summary, the link will teach you to install anaconda, create an environment and install necessary libraries. Just follow it. I named my environment as “r-tensorflow” because that is the name of the environment that the install_keras() in R will do :)

Step 3: Point rstudio to use the python in your newly created environment using use_python() function

Open your rstudio (if you close it after following step 1) and type the following code

library(keras)
library(reticulate)
# in case you run into error run this : reticulate::py_discover_config("keras") 
use_python("<yourpath>/Anaconda3/envs/r-tensorflow/Scripts/python.exe")
# change <yourpath> approriately
# write all the codes for building model in keras (or tensorflow) e.g. mnist<-dataset_mnist()

Important note on Step 3: If you still got the "not found module" after following step 3, you must start a new fresh R session and ensure to delete the workspace (.RData) because more likely your current script will still use the old python configuration though you used use_python

Upvotes: 4

pari
pari

Reputation: 808

I had the same problem and it is solved by installing the package in two steps:

install keras: install.packages("keras")
keras::install_keras()

There you go!

Upvotes: 5

Lakshmi Bhavani - Intel
Lakshmi Bhavani - Intel

Reputation: 1333

Please install "reticulate" library using command install.packages("reticulate") and then load using library(reticulate) then install absl using command conda_install('r-tensorflow','absl-py')

Upvotes: 0

Related Questions