【问题标题】:Trying to install keras & tensorflow on an Amazon AWS Instance尝试在 Amazon AWS 实例上安装 keras 和 tensorflow
【发布时间】:2020-05-07 21:55:24
【问题描述】:

我在尝试在 Ubuntu EC2 实例上安装 keras 和 tensorflow 时遇到了一些问题。

我在库中加载并尝试安装 keras。

library(keras)
library(tensorflow)
install_keras(tensorflow = "gpu")

我收到以下错误:

Error: could not find a Python environment for /usr/bin/python

我还运行了以下命令以及其他一些终端命令来尝试安装 conda 和 python 等。

sudo apt install git libpython3.7-dev

我也跟着 keras.io 网站尝试安装它。我在服务器上有一个名为/usr/bin/python3.6: 的文件夹,但是当我尝试访问它时出现错误Directory /usr/bin/python3.6: no such file or directory。我在服务器上的home 目录中有两个用户帐户(以及两个同名文件夹)ubuntumyUsername_folder

/home/myUsername_folder 我有.rstudio 文件。 (/home/myUsername_folder/.rstudio) 和一些数据集。 在home/ubuntu 文件夹中,我安装了R /home/ubuntu/R/x86_64-pc-linux-gnu-library/3.6。 在/ 根目录中,我有usrvarlib64tmpbin 等文件夹。

我如何告诉 R Python 的位置?

编辑:

我重新启动了 RStudio 并运行:

> library(keras)
> library(tensorflow)
> install_keras(tensorflow = "gpu")
Using virtual environment '~/.virtualenvs/r-reticulate' ...
/home/myUser/.virtualenvs/r-reticulate/bin/python: No module named pip
Error in strsplit(output, "\\s+")[[1]] : subscript out of bounds
In addition: Warning message:
In system2(python, c("-m", "pip", "--version"), stdout = TRUE) :
  running command ''/home/myUser/.virtualenvs/r-reticulate/bin/python' -m pip --version' had status 1

尝试安装pip 使用:

sudo apt-get install python3-pip

Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-pip is already the newest version (9.0.1-2.3~ubuntu1.18.04.1).
0 upgraded, 0 newly installed, 0 to remove and 39 not upgraded.

我运行以下命令来安装 Anaconda:

wgethttps://repo.anaconda.com/archive/Anaconda3-2019.10-Linux-x86_64.sh bash Anaconda3-2019.10-Linux-x86_64.sh

然后跑:

> install_keras(method = c("conda"),
+               conda = "auto", version = "default", tensorflow = "gpu")
ERROR: The install method you used for conda--probably either `pip install conda`
or `easy_install conda`--is not compatible with using conda as an application.
If your intention is to install conda as a standalone application, currently
supported install methods include the Anaconda installer and the miniconda
installer.  You can download the miniconda installer from
https://conda.io/miniconda.html.

Error: Error 1 occurred creating conda environment r-reticulate

编辑:我试过了:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh

然后在 RStudio 中

> install_keras(method = c("conda"),
+               conda = "/home/myUser/miniconda3/bin/",
+               version = "default", tensorflow = "gpu")
sh: 1: /home/myuser/miniconda3/bin/: Permission denied
Error: Error 126 occurred creating conda environment r-reticulate

我更改了权限:

sudo chmod -R 777 /home/myUser/miniconda3/bin/

在运行install_keras(...) 后得到同样的错误。

编辑:

我在 RStudio 中运行

tf_config()

找不到 TensorFlow 的安装。

Python environments searched for 'tensorflow' package:
 /usr/bin/python3.6
 /usr/bin/python3.6
 /usr/bin/python2.7
 /home/myuser/anaconda3/bin/python3.7
 /usr/bin/python3.6
 /home/myuser/miniconda3/bin/python3.7

您可以使用 install_tensorflow() 函数安装 TensorFlow。

install_tensorflow()

错误:找不到 /usr/bin/python 的 Python 环境

我也试过use_python("/home/myuser/anaconda3/bin/python3.7")

编辑

上述问题仍然存在,但我可以运行以下 R 代码(“有效”):

nn_dat = iris %>% as_tibble %>%
  mutate(sepal_l_feat = scale(Sepal.Length),
         sepal_w_feat = scale(Sepal.Width),
         petal_l_feat = scale(Petal.Length),
         petal_w_feat = scale(Petal.Width),          
         class_num    = as.numeric(Species) - 1, # factor, so = 0, 1, 2
         class_label  = Species) %>%
  select(contains("feat"), class_num, class_label)
nn_dat %>% head(3)

test_f = 0.20
nn_dat = nn_dat %>%
  mutate(partition = sample(c('train','test'), nrow(.), replace = TRUE, prob = c(1 - test_f, test_f)))

x_train = nn_dat %>% filter(partition == 'train') %>% select(contains("feat")) %>% as.matrix
y_train = nn_dat %>% filter(partition == 'train') %>% pull(class_num) %>% to_categorical(3)
x_test  = nn_dat %>% filter(partition == 'test')  %>% select(contains("feat")) %>% as.matrix
y_test  = nn_dat %>% filter(partition == 'test')  %>% pull(class_num) %>% to_categorical(3)

model = keras_model_sequential()
model %>% 
  layer_dense(units = 4, activation = 'relu', input_shape = 4) %>% 
  layer_dense(units = 3, activation = 'softmax')
model %>% summary

model %>% compile(
  loss      = 'categorical_crossentropy',
  optimizer = optimizer_rmsprop(),
  metrics   = c('accuracy')
)

history = model %>% fit(
  x = x_train, y = y_train,
  epochs           = 200,
  batch_size       = 20,
  validation_split = 0
)
plot(history)

我在这里收到这条消息:

/home/myuser/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/myuser/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/myuser/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/myuser/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/myuser/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/myuser/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
/home/myuser/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/myuser/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/myuser/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/myuser/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/myuser/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/myuser/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
WARNING:tensorflow:
The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
  * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
  * https://github.com/tensorflow/addons
  * https://github.com/tensorflow/io (for I/O related ops)
If you depend on functionality not listed there, please file an issue.

2020-01-21 21:49:45.344207: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-01-21 21:49:45.384211: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2400050000 Hz
2020-01-21 21:49:45.384567: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55fc4e3c43b0 executing computations on platform Host. Devices:
2020-01-21 21:49:45.384598: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): <undefined>, <undefined>
2020-01-21 21:49:45.448620: W tensorflow/compiler/jit/mark_for_compilation_pass.cc:1412] (One-time warning): Not using XLA:CPU for cluster because envvar TF_XLA_FLAGS=--tf_xla_cpu_global_jit was not set.  If you want XLA:CPU, either set that envvar, or use experimental_jit_scope to enable XLA:CPU.  To confirm that XLA is active, pass --vmodule=xla_compilation_cache=1 (as a proper command-line flag, not via TF_XLA_FLAGS) or set the envvar XLA_FLAGS=--xla_hlo_profile.

然后运行:

perf = model %>% evaluate(x_test, y_test)
print(perf)

$loss
[1] 0.2995828

$acc
[1] 0.84375

所以我可以使用kerastensor 流,但我仍然收到上述错误消息。

【问题讨论】:

  • 在尝试安装 pip 之前您是否激活了 conda 或虚拟环境?
  • 我将如何激活它?在安装 Anaconda 和 Miniconda 时,我可以选择 Do you wish the installer to initialize Miniconda3 by running conda init?,我写了 yes
  • 尝试使用 $conda info --envs 查看您的 conda 环境。如果你能看到你命名的环境,那么使用命令conda activate env_name
  • 我跑了conda info --envs,它给了# conda environments: # /home/myuser/anaconda3 base * /home/myuser/miniconda3,然后,conda activate /home/myuser/anaconda3。然后它“什么也没做”,但添加了(/home/myuser/anaconda3) myuser@ip-132-21-61-131:~/folder_1,而之前它只是(base) myuser@ip-132-21-61-131:~/folder_1
  • 我认为问题在于Directory /usr/bin/python3.6: no such file or directory 目录中没有任何内容,这是install_keras() 试图查找 Python 的位置,但是该文件夹已创建。

标签: python r tensorflow amazon-ec2 keras


【解决方案1】:

好吧,我以前没有使用过 R,但是,在使用 Tensorflow 等库时,我遇到了类似的环境问题。在为深度学习工作安装 Tensorflow 和 Keras 时,我遇到了同样的问题。 就我而言,我发现漏洞问题是由于 Tensorflow 库本身造成的。我使用 Windows,但这可能也适用于 linux,所以我所做的是:

  1. 从您的计算机中卸载所有 Conda、Python 和 R(包括 tensorflow 等附加库)。
  2. 我下载了“ANACONDA”的las版本,但只下载了ANACONDA,没有别的。这实际上就是您消除错误所需的全部内容。
  3. 继续安装,然后您将拥有 Anaconda 导航器。 这包括默认情况下(在 anaconda 导航器内)的 app 和 python,例如 Jupyter、Spyder 和 R。
  4. 转到环境部分,并创建一个专用环境,您将在其中安装 Tensorflow 库。您可以在 Anaconda 导航器中的导航器中键入名称,以找到您正在搜索的库。
  5. 确保在刚刚创建的新环境中安装了所需的孔库。
  6. 返回主页部分,并确保您已选择“新创建的环境”安装您需要的应​​用程序,您可能在下面拥有这些应用程序。其中R是可行的。

这应该可以,希望对您有所帮助。这种方式比传统的 pip 安装过程更加清晰明了。请注意,Tensorflow 不是一个简单的库,通过使用这种方法,一切都被 ANACONDA 导航器组织得井井有条,没有未来的错误。

【讨论】:

    【解决方案2】:

    确保 conda 或 virtualenv 已正确安装。

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 2014-06-26
      • 2017-09-21
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 2015-04-04
      相关资源
      最近更新 更多