在R中出现错误:需要h5py Python包以保存和加载模型

6

我正在使用R 3.4.2版本。我在跟随decryptr 软件包的示例。

library(decryptr)

# Download captchas
captcha <- download_tjmg(dest = 'img')
keras_tjmg <- decryptrModels::read_model('tjmg')

最后一行代码出现了错误

Error in keras::load_model_hdf5(file_path) : 
  The h5py Python package is required to save and load models

我试过从终端重新安装h5py包。我还安装了keras包。我可以在python中导入h5py包,但是一些原因导致Rstudio无法识别它。


你在使用Anaconda吗?我曾经遇到过这个问题,我通过在(r-tensorflow)环境中pip安装h5py来解决它。 - Athos
2个回答

2

我以前从未安装过这些软件包,但是我按照以下步骤进行并成功了!除了你报告的错误外,它还给了我各种错误,所以这需要花费一些时间。

最后,我得出了以下结论:在终端中执行以下操作:

pip uninstall h5py
pip install h5py
pip uninstall keras
pip install keras

我在Python中检查了一下import h5py, 运行结果是正确的(第一次运行时出现了DLL错误)。然后在重新启动后的R中:

install.packages("reticulate")
install.packages("tensorflow")
install.packages("keras")
install.packages("h5py")
devtools::install_github("decryptr/decryptr")
devtools::install_github("decryptr/decryptrModels")

library(keras)
install_keras()

# Check where Python's looked for
reticulate::py_config() 

library(decryptr)
library(decryptrModels)

captcha <- captcha_download_tjmg(dest = 'img')
keras_tjmg <- read_model('tjmg')

我的 sessionInfo() 如下:

R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
other attached packages:
[1] decryptrModels_0.0.0.9000 decryptr_0.0.1.9000      
[3] keras_2.0.8.9001         
loaded via a namespace (and not attached):
 [1] Rcpp_0.12.13          prettyunits_1.0.2     assertthat_0.2.0     
 [4] R6_2.2.2              jsonlite_1.5          magrittr_1.5         
 [7] httr_1.3.1            tfruns_1.0            rlang_0.1.2          
[10] progress_1.1.2        curl_2.8.1            whisker_0.3-2        
[13] reticulate_1.2.0.9005 tools_3.4.1           purrr_0.2.3          
[16] compiler_3.4.1        base64enc_0.1-3       tensorflow_1.4.0.9003

如果这个可行,请告诉我。


2
install.packages("h5py") 是错误的 - 没有 R 包 h5py,它是一个 Python 包 - Marmite Bomber

0

尽管我的回答有些晚了几年,但我可以在Ubuntu 18.04中通过安装libpython-dev来解决问题。似乎reticulate需要这个库来支持R中的Keras/Tensorflow:

sudo apt-get update -qq
sudo apt-get install -qq -y --no-install-recommends libpython-dev


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接