简体   繁体   中英

Error loading the keras package in R studio

I am using R4.0.1 and Rstudio1.3.959 on windows 10. I have installed tensor flow:

install.packages("tensorflow")
library(tensorflow)
install_tensorflow(method = "conda", conda_python_version = 3.6)

I checked the installation success by:

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

Output: Tensor("Const:0", shape=(), dtype=string)

tf$constant(1.5)

Output: Tensor("Const_1:0", shape=(), dtype=float32)

I further checked by:

tf_config()

Output: TensorFlow v1.13.2 () Python v3.6 (C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/python.exe)

From here it seems the tensor flow is installed properly and is working fine. However, I faced problems in loading Keras library. I did the following:

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

As I load the library, it gives the following error

Error: package or namespace load failed for ‘keras’:
 .onLoad failed in loadNamespace() for 'keras', details:
  call: py_module_import(module, convert = convert)
  error: ImportError: cannot import name 'swish'

Detailed traceback: 
  File "C:\Users\user\AppData\Local\r-miniconda\envs\r-reticulate\lib\site-packages\tensorflow\keras\__init__.py", line 14, in <module>
    from . import activations
  File "C:\Users\user\AppData\Local\r-miniconda\envs\r-reticulate\lib\site-packages\tensorflow\keras\activations\__init__.py", line 23, in <module>
    from tensorflow.python.keras.activations import swish

Since the keras package could not be loaded I cannot run the following code

install_keras(method = "conda")

I could get additional information as below:

library(reticulate)
> py_discover_config("keras")
python:         C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/python.exe
libpython:      C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/python36.dll
pythonhome:     C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate
version:        3.6.10 |Anaconda, Inc.| (default, May  7 2020, 19:46:08) [MSC v.1916 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/Lib/site-packages/numpy
numpy_version:  1.18.1

I could also see that default python version in use as:

Sys.which("python")
                                                                 python 
"C:\\Users\\user\\AppData\\Local\\R-MINI~1\\envs\\R-RETI~1\\python.exe"

I would be grateful if anyone can solve this installation issue. Thanks

I am a Windows 10 User and I encountered the same issue today. After checking multiple posts, the below steps worked for me.

  1. Update R to the latest 4.1.2 version. R Studio might say that R is up to date (which happened to me today), but it may not be. install.packages("installr") library(installr) updateR()

  2. Update Rcpp, jsonlite, and curl packages even if they claim to be up to date. install.packages("Rcpp") install.packages("jsonlite") install.packages("curl")

  3. Start a Fresh R session.

  4. Run the below commands to install, reticulate, tensorflow, and keras from github: devtools::install_github("rstudio/reticulate") devtools::install_github("rstudio/tensorflow") devtools::install_github("rstudio/keras")

  5. Run the below commands (Some of them may be redundant if preceding ones include the functionalities. Entering them here anyway as it could help.). library(keras) install_keras() library(reticulate) library(tensorflow)

  6. Test TensorFlow installation with the command below: tf$constant("Hello World!")

If you get the below output, you're there. There could be some text before the output (ignore them). tf.Tensor(b'Hello World,', shape=(), dtype=string)

When running a DNN or RNN, after installing the packages, calling just library(keras) shoudl be adequate.

Have fun!

This might be a bit different but I find it easier to hand manage dependent env. So you can open conda prompt and execute:

conda create -n env_name python=3.6 tensorflow

Then in R before you do anything call

library(keras)
library(tensorflow)
use_condaenv(condaenv = "env_name",required = T)

By default you wil get TF 2.xx, you can specify that in conda env.

EDIT: For TF gpu you need to specify conda create -n env_name python=3.6 tensorflow-gpu and you will get CUDa and CUDNN if you have GPU on your PC.

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