Sau*_*abh 6 python r centos7 tensorflow
在过去的 5 天里,我试图让 Keras/Tensorflow 包在 R 中工作。我使用 RStudio 进行安装并使用了conda
, miniconda
,virtualenv
但最终每次都会崩溃。安装库不应该是一场噩梦,尤其是当我们谈论 R(最好的统计语言之一)和 TensorFlow(最好的深度学习库之一)时。有人可以分享在 CentOS 7 上安装 Keras/Tensorflow 的可靠方法吗?
以下是我在 RStudio 中安装的步骤tensorflow
。
由于 RStudio 每次运行时都会崩溃,因此tensorflow::tf_config()
我无法检查出了什么问题。
devtools::install_github("rstudio/reticulate")
devtools::install_github("rstudio/keras") # This package also installs tensorflow
library(reticulate)
reticulate::install_miniconda()
reticulate::use_miniconda("r-reticulate")
library(tensorflow)
tensorflow::tf_config() **# Crashes at this point**
sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)
Matrix products: default
BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tensorflow_2.7.0.9000 keras_2.7.0.9000 reticulate_1.22-9000
loaded via a namespace (and not attached):
[1] Rcpp_1.0.7 lattice_0.20-45 png_0.1-7 zeallot_0.1.0
[5] rappdirs_0.3.3 grid_3.6.0 R6_2.5.1 jsonlite_1.7.2
[9] magrittr_2.0.1 tfruns_1.5.0 rlang_0.4.12 whisker_0.4
[13] Matrix_1.3-4 generics_0.1.1 tools_3.6.0 compiler_3.6.0
[17] base64enc_0.1-3
Run Code Online (Sandbox Code Playgroud)
更新 1 安装 Tensorflow 时 RStudio 不崩溃的唯一方法是执行以下步骤 -
首先,我使用 conda 创建了一个新的虚拟环境
conda create --name py38 python=3.8.0
conda activate py38
conda install tensorflow=2.4
Run Code Online (Sandbox Code Playgroud)
然后在 RStudio 中,我安装了 reticulate 并激活了我之前使用 conda 创建的虚拟环境
devtools::install_github("rstudio/reticulate")
library(reticulate)
reticulate::use_condaenv("/root/.conda/envs/py38", required = TRUE)
reticulate::use_python("/root/.conda/envs/py38/bin/python3.8", required = TRUE)
reticulate::py_available(initialize = TRUE)
ts <- reticulate::import("tensorflow")
Run Code Online (Sandbox Code Playgroud)
当我尝试tensorflow
在 RStudio 中导入时,它会加载库,/lib64/libstdc++.so.6
然后/root/.conda/envs/py38/lib/libstdc++.so.6
出现以下错误 -
Error in py_module_import(module, convert = convert) :
ImportError: Traceback (most recent call last):
File "/root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
File "/home/R/x86_64-redhat-linux-gnu-library/3.6/reticulate/python/rpytools/loader.py", line 39, in _import_hook
module = _import(
ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /root/.conda/envs/py38/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/errors
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
Run Code Online (Sandbox Code Playgroud)
这是里面的内容/lib64/libstdc++.so.6
> strings /lib64/libstdc++.so.6 | grep GLIBC
GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
GLIBCXX_3.4.15
GLIBCXX_3.4.16
GLIBCXX_3.4.17
GLIBCXX_3.4.18
GLIBCXX_3.4.19
GLIBC_2.3
GLIBC_2.2.5
GLIBC_2.14
GLIBC_2.4
GLIBC_2.3.2
GLIBCXX_DEBUG_MESSAGE_LENGTH
Run Code Online (Sandbox Code Playgroud)
为了解决库问题,我添加了RStudio 中正确libstdc++.so.6
库的路径。GLIBCXX_3.4.20
system('export LD_LIBRARY_PATH=/root/.conda/envs/py38/lib/:$LD_LIBRARY_PATH')
Run Code Online (Sandbox Code Playgroud)
并且
Sys.setenv("LD_LIBRARY_PATH" = "/root/.conda/envs/py38/lib")
Run Code Online (Sandbox Code Playgroud)
但我仍然遇到同样的错误ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20'
。不知何故,RStudio 仍然/lib64/libstdc++.so.6
首先加载,而不是/root/.conda/envs/py38/lib/libstdc++.so.6
相反RStudio
,如果我在控制台中执行上述步骤R
,那么我也会得到完全相同的错误。
更新2: 解决方案已发布在这里
也许我失败的尝试会帮助其他人解决这个问题;我的方法:
\nsudo yum install epel-release\nsudo yum install R\nsudo yum install libxml2-devel\nsudo yum install openssl-devel\nsudo yum install libcurl-devel\nsudo yum\xc2\xa0install\xc2\xa0libXcomposite\xc2\xa0libXcursor\xc2\xa0libXi\xc2\xa0libXtst\xc2\xa0libXrandr\xc2\xa0alsa-lib\xc2\xa0mesa-libEGL\xc2\xa0libXdamage\xc2\xa0mesa-libGL\xc2\xa0libXScrnSaver\n
Run Code Online (Sandbox Code Playgroud)\nconda init\nconda create --name tf\nconda activate tf\nconda\xc2\xa0install\xc2\xa0-c conda-forge tensorflow\n
Run Code Online (Sandbox Code Playgroud)\n**从此 conda env 中,您可以在 python 中导入tensorflow,不会出现错误;现在通过 R 访问 tf
\nsudo yum install centos-release-scl\nsudo yum install devtoolset-7-gcc*\n
Run Code Online (Sandbox Code Playgroud)\nscl enable devtoolset-7 R\ninstall.packages("remotes")\nremotes::install_github(\'rstudio/reticulate\')\nreticulate::use_condaenv("tf", conda = "~/anaconda3/bin/conda")\nreticulate::repl_python()\n# This works as expected but the command "import tensorflow" crashes R\n# Error: *** caught segfault *** address 0xf8, cause \'memory not mapped\'\n\n# Also tried:\ninstall.packages("devtools")\ndevtools::install_github(\'rstudio/tensorflow\')\ndevtools::install_github(\'rstudio/keras\')\nlibrary(tensorflow)\ninstall_tensorflow() # "successful"\ntensorflow::tf_config()\n# Error: *** caught segfault *** address 0xf8, cause \'memory not mapped\'\n
Run Code Online (Sandbox Code Playgroud)\ndevtools::install_github(\'rstudio/tensorflow@v2.4.0\')\ndevtools::install_github(\'rstudio/keras@v2.4.0\')\nlibrary(tensorflow)\ntf_config()\n# Error: *** caught segfault *** address 0xf8, cause \'memory not mapped\'\n
Run Code Online (Sandbox Code Playgroud)\n# deactivate conda\nsudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \nexport R_VERSION=4.0.0\ncurl -O https://cdn.rstudio.com/r/centos-7/pkgs/R-${R_VERSION}-1-1.x86_64.rpm\nsudo yum install R-${R_VERSION}-1-1.x86_64.rpm\n\nscl enable devtoolset-7 /opt/R/4.0.0/bin/R\ninstall.packages("devtools")\ndevtools::install_github(\'rstudio/reticulate\')\nreticulate::use_condaenv("tf", conda = "~/anaconda3/bin/conda")\nreticulate::repl_python()\n# \'import tensorflow\' resulted in "core dumped"\n
Run Code Online (Sandbox Code Playgroud)\n我猜问题出在 R/CentOS 上,因为您可以正常通过 python 导入和使用tensorflow,但我不确定还可以尝试什么。
\n我还想说,我对 Ubuntu 没有任何问题(Ubuntu 受tensorflow、macOS 和 Windows 的特别支持),并且我遇到了这些可能有帮助的文档:https://wiki.hpcc.msu。 edu/display/ITH/Installing+TensorFlow+using+anaconda / https://wiki.hpcc.msu.edu/pages/viewpage.action?pageId=22709999
\n更新于 2022 年 7 月 29 日 经过几个月的解决这个问题后,我觉得在 CentOS 上浪费时间编码 R 真是太愚蠢了。最流行、最稳定的 R 代码操作系统是 Ubuntu。默认情况下,CentOS仅支持3.6版本的R,而当前最稳定的R版本是4.2。CentOS 上默认的 R 3.6 版本中,大多数库都已过时,并且与针对 R 4.2+ 更新的其他库发生冲突。根据我的经验,如果您开始在 Ubuntu 上编写 R 代码,您将避免很多痛苦和挫折。我不赞助Ubuntu,以上陈述仅是我的经验,其他人可能有不同的经验。
原始答案 花了我超过15天的时间,我终于解决了这个问题。
启动一个干净的 CentOS 7 VM,安装 R 和依赖项(取自 Jared 的答案)-
yum install epel-release
yum install R
yum install libxml2-devel
yum install openssl-devel
yum install libcurl-devel
yum install libXcomposite libXcursor libXi libXtst libXrandr alsa-lib mesa-libEGL libXdamage mesa-libGL libXScrnSaver
Run Code Online (Sandbox Code Playgroud)
现在,创建一个 conda 环境
yum install conda
conda clean -a # Clean cache and remove old packages, if you already have conda installed
# Install all the packages together and let conda handle versioning. It is important to give a Python version while setting up the environment. Since Tensorflow supports python 3.9.0, I have used this version
conda create -y -n "tf" python=3.9.0 ipython tensorflow keras r-essentials r-reticulate r-tensorflow
conda activate tf
Run Code Online (Sandbox Code Playgroud)
7878
在服务器上打开一个新端口(或选择任何您想要的端口号)以使用新conda
环境库访问 RStudio
iptables -A INPUT -p tcp --dport 7878 -j ACCEPT
/sbin/service iptables save
Run Code Online (Sandbox Code Playgroud)
然后按如下方式启动 RStudio -
/usr/lib/rstudio-server/bin/rserver \
--server-daemonize=0 \
--www-port 7878 \
--rsession-which-r=$(which R) \
--rsession-ld-library-path=$CONDA_PREFIX/lib
Run Code Online (Sandbox Code Playgroud)
您将在默认端口上保持完整的早期环境8787
,并在 上使用 Tensorflow 和 Keras 的新环境7878
。
以下代码现在可以在 RStudio 中正常运行
install.packages("reticulate")
install.packages("tensorflow")
library(reticulate)
library(tensorflow)
ts <- reticulate::import("tensorflow")
Run Code Online (Sandbox Code Playgroud)