简体   繁体   中英

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?

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!

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

请使用命令install.packages("reticulate")安装“reticulate”库,然后使用library(reticulate)加载,然后使用命令conda_install('r-tensorflow','absl-py')安装 absl

try:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM