简体   繁体   English

R keras 包错误:未找到 Python 模块 tensorflow.contrib.keras.python.keras

[英]R keras package Error: Python module tensorflow.contrib.keras.python.keras was not found

I have keras installed with devtools from GitHub in R and TensorFlow installed in Python.我在 R 中安装了来自 GitHub 的devtools keras ,在 Python 中安装了 TensorFlow。

However when I run an example Keras command like:但是,当我运行一个示例 Keras 命令时,例如:

model <- keras_model_sequential() 

I get the following:我得到以下信息:

Error: Python module tensorflow.contrib.keras.python.keras was not found.错误:未找到 Python 模块 tensorflow.contrib.keras.python.keras。

 Detected Python configuration: python: C:\\Python35\\python.exe libpython: C:/Python35/python35.dll pythonhome: C:\\Python35 version: 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] Architecture: 64bit numpy: C:\\Python35\\lib\\site-packages\\numpy numpy_version: 1.13.0 tensorflow: C:\\Python35\\lib\\site-packages\\tensorflow python versions found: C:\\Python35\\python.exe C:\\Python27\\\\python.exe C:\\Python35\\\\python.exe C:\\Python36\\\\python.exe

I had a similar problem.我有一个类似的问题。 Restart rstudio, load keras and tensorflow libraries, and type use_condaenv("r-tensorflow") .重启 rstudio,加载 keras 和 tensorflow 库,然后输入use_condaenv("r-tensorflow") That fixed it for me.那为我修好了。

First, you can install the R tensorflow package from here .首先,您可以从这里安装 R tensorflow 包。

Then, you can just install the latest tensorflow version, using install_tensorflow function, as shown in this answer .然后,您可以使用 install_tensorflow 函数安装最新的 tensorflow 版本,如本答案所示。

After that, just install and update R-keras library.之后,只需安装和更新 R-keras 库。 It should use the last version of TensorFlow now, and this could potentially solve your problem.它现在应该使用最新版本的 TensorFlow,这可能会解决您的问题。 Also, make sure you install the tensorflow version that matches your Python version.此外,请确保安装与您的 Python 版本匹配的 tensorflow 版本。

I had a similar problem with a conda installation on a Mac (so using install_keras(method = 'conda', conda = '/opt/anaconda3/bin/conda') , which created a virtual environment called r-reticulate under ~/.conda/envs . Then when I tried to instantiate a model just like you do, I was getting an error 'Error: Python module tensorflow.python.keras was not found.'我有一个Mac上康达安装有类似的问题(因此使用install_keras(method = 'conda', conda = '/opt/anaconda3/bin/conda')其创建被称为虚拟环境r-reticulate~/.conda/envs 。然后,当我尝试像您一样实例化模型时,出现错误“错误:未找到 Python 模块 tensorflow.python.keras”。

For me what resolved it is after loading library(keras) execute use_condaenv("r-reticulate", required = TRUE) and then everything worked.对我来说,在加载library(keras)执行use_condaenv("r-reticulate", required = TRUE)之后解决了它,然后一切正常。

I faced a similar problem and below steps helped to overcome the issue.我遇到了类似的问题,以下步骤有助于克服这个问题。

  1. Install TensorFlow and Keras from rstudio github.从 rstudio github 安装 TensorFlow 和 Keras。
  • devtools::install_github("rstudio/tensorflow") devtools::install_github("rstudio/tensorflow")
  • devtools::install_github("rstudio/keras") devtools::install_github("rstudio/keras")
  1. Execute the below执行以下
  • tensorflow::install_tensorflow()张量流::安装_张量流()
  • tensorflow::tf_config()张量流:: tf_config()

I faced a similar problem.我遇到了类似的问题。 The issue was solved by updating the tensorflow module from 1.0.1 to 1.2.1通过将tensorflow模块从 1.0.1 更新到 1.2.1 解决了该问题

In Windows, I tried all above given solution but didn't work.在 Windows 中,我尝试了上面给出的所有解决方案,但没有奏效。 It worked for me when I created env using, both in spyder and R当我在 spyder 和 R 中使用创建 env 时,它对我有用

conda create -n keras-tf tensorflow keras

In windows在窗口中

library(keras)
library(tensorflow)
use_condaenv("keras-tf", required = T)

In Python在 Python 中

import tensorflow as tf

Some times your env and system don't have any problems, it is just the way how you configure Tensorflow and Keras that cause these errors to be raised.有时您的 env 和系统没有任何问题,只是您配置 Tensorflow 和 Keras 的方式导致这些错误出现。

below is how I fixed mine:以下是我修复我的方法:

{install.packages("tensorflow")
install.packages("keras")
tensorflow::tf_config()
mytf <- tf$compat$v1} 

the configuration results shows the following output:配置结果显示以下输出:

2021-12-09 11:43:39.007746: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-12-09 11:43:39.007931: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
TensorFlow v2.6.2 ()
Python v3.7 (C:/Users/myName/AppData/Local/r-miniconda/envs/r-reticulate/python.exe)

then type:然后输入:

install_keras()

after that you can test:之后你可以测试:

mnist <- keras::dataset_mnist() ; mnist

this will give you an output like:这会给你一个输出,如:

$train
$train$x
, , 1

         [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
    [1,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0
    [2,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0
    [3,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0
    [4,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0
    [5,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0
    [6,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0
    [7,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0
    [8,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0    
....
$train$y
   [1] 5 0 4 1 9 2 1 3 1 4 3 5 3 6 1 7 2 8 6 9 4 0 9 1 1 2 4 3 2 7 3 8 6 9 0 5 6 0 7 6 1 8 7 9 3
  [46] 9 8 5 9 3 3 0 7 4 9 8 0 9 4 1 4 4 6 0 4 5 6 1 0 0 1 7 1 6 3 0 2 1 1 7 9 0 2 6 7 8 3 9 0 4
  [91] 6 7 4 6 8 0 7 8 3 1 5 7 1 7 1 1 6 3 0 2 9 3 1 1 0 4 9 2 0 0 2 0 2 7 1 8 6 4 1 6 3 4 5 9 1
 [136] 3 3 8 5 4 7 7 4 2 8 5 8 6 7 3 4 6 1 9 9 6 0 3 7 2 8 2 9 4 4 6 4 9 7 0 9 2 9 5 1 5 9 1 2 3
 [181] 2 3 5 9 1 7 6 2 8 2 2 5 0 7 4 9 7 8 3 2 1 1 8 3 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 0 5 1 4 2 8 3 2 4 1 5 4 6 0 7 9 8 4 9 8 0 1 1 0 2 2 3 2 4 4 5
 [721] 8 6 5 7 7 8 8 9 7 4 7 3 2 0 8 6 8 6 1 6 8 9 4 0 9 0 4 1 5 4 7 5 3 7 4 9 8 5 8 6 3 8 6 9 9
....
[ reached getOption("max.print") -- omitted 9000 entries ]

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

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