简体   繁体   English

在 R 工作室中加载 keras package 时出错

[英]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:我在 windows 10 上使用 R4.0.1 和 Rstudio1.3.959。我已经安装了张量流:

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) Output:张量(“常量:0”,形状=(),dtype=字符串)

tf$constant(1.5)

Output: Tensor("Const_1:0", shape=(), dtype=float32) Output:张量(“Const_1:0”,形状=(),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) Output: TensorFlow v1.13.2 () Python v3.6 (C:/Users/users/AppData/Local/r-min/python)

From here it seems the tensor flow is installed properly and is working fine.从这里看来,张量流已正确安装并且工作正常。 However, I faced problems in loading Keras library.但是,我在加载 Keras 库时遇到了问题。 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由于无法加载 keras package 我无法运行以下代码

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:我还可以看到默认的 python 版本用作:

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.我是 Windows 10 用户,我今天遇到了同样的问题。 After checking multiple posts, the below steps worked for me.检查多个帖子后,以下步骤对我有用。

  1. Update R to the latest 4.1.2 version.将 R 更新到最新的 4.1.2 版本。 R Studio might say that R is up to date (which happened to me today), but it may not be. R Studio 可能会说 R 是最新的(今天发生在我身上),但它可能不是。 install.packages("installr") library(installr) updateR() install.packages("installr") library(installr) updateR()

  2. Update Rcpp, jsonlite, and curl packages even if they claim to be up to date.更新 Rcpp、jsonlite 和 curl 包,即使它们声称是最新的。 install.packages("Rcpp") install.packages("jsonlite") install.packages("curl") install.packages("Rcpp") install.packages("jsonlite") install.packages("curl")

  3. Start a Fresh R session.开始一个的 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") 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) library(keras) install_keras() library(reticulate) library(tensorflow)

  6. Test TensorFlow installation with the command below: tf$constant("Hello World!")使用以下命令测试 TensorFlow 安装: tf$constant("Hello World!")

If you get the below output, you're there.如果你得到下面的 output,你就在那里。 There could be some text before the output (ignore them). output 之前可能有一些文本(忽略它们)。 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.运行 DNN 或 RNN 时,安装软件包后,仅调用library(keras)应该就足够了。

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 提示符并执行:

conda create -n env_name python=3.6 tensorflow

Then in R before you do anything call然后在 R 做任何事情之前调用

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.默认情况下,您将获得 TF 2.xx,您可以在 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.编辑:对于 TF gpu,您需要指定conda create -n env_name python=3.6 tensorflow-gpu如果您的 PCA3.0 上有 Z52F9EC21735243AD9917CDNN,您将获得 CUDa 和 CUDNN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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