运行keras时RStudio崩溃

时间:2020-08-02 15:00:50

标签: python r keras rstudio conda

尝试运行keras R软件包的功能时,我的RStudio会话崩溃。我在窗口中收到一条错误消息“ R Session Aborted”,但无法恢复有关其原因的任何其他信息。

enter image description here

在来自终端的R会话中运行相同的命令集时,我无法重现该错误。在Shell中使用R运行以下脚本时,它运行得很好,但是在RStudio中运行时,它在行mnist_y <- to_categorical(mnist_y, 10)处崩溃。在这两种情况下,我都首先使用conda activate r-reticulate在终端中激活一个conda会话。摘自布拉德利·勃姆克(Bradley Boehmke)很棒的书Hands-On Machine Learning in R的代码示例:

## installing keras and tensorflow
library(keras)
reticulate::use_condaenv()
install_keras(method = "conda", conda = reticulate::conda_binary())

## This sometimes produces an error: 
# Error: could not find a Python environment for /usr/bin/python

library(tensorflow)
reticulate::use_condaenv()
install_tensorflow(method = "conda", conda = reticulate::conda_binary())


# Helper packages
library(dplyr)         # for basic data wrangling

# Modeling packages
library(keras)         # for fitting DNNs

# Import MNIST training data
mnist <- dslabs::read_mnist()
mnist_x <- mnist$train$images
mnist_y <- mnist$train$labels

# Rename columns and standardize feature values
colnames(mnist_x) <- paste0("V", 1:ncol(mnist_x))
mnist_x <- mnist_x / 255

# One-hot encode response
mnist_y <- to_categorical(mnist_y, 10)

# Specify the model
model <- keras_model_sequential() %>%
  
  # Network architecture
  layer_dense(units = 128, activation = "relu", input_shape = ncol(mnist_x)) %>%
  layer_dense(units = 64, activation = "relu") %>%
  layer_dense(units = 10, activation = "softmax") %>%
  
  # Backpropagation
  compile(
    loss = 'categorical_crossentropy',
    optimizer = optimizer_rmsprop(),
    metrics = c('accuracy')
  )

## Output when running from shell R-session:

# 2020-08-02 18:20:05.039290: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
# 2020-08-02 18:20:05.079542: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fa1140c86f0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
# 2020-08-02 18:20:05.079563: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version

# Train the model
fit1 <- model %>%
  fit(
    x = mnist_x,
    y = mnist_y,
    epochs = 25,
    batch_size = 128,
    validation_split = 0.2,
    verbose = FALSE
  )

# Display output
fit1

## Returns:

# Final epoch (plot to see history):
# loss: 0.002402
# accuracy: 0.9991
# val_loss: 0.1655
# val_accuracy: 0.9753 

我的问题是:

  • 是什么原因导致RStudio在终端中的R会话没有崩溃的情况下崩溃?
  • 如何从中止的R(Studio)会话中恢复错误消息?
  • 是否在RStudio中加载了没有在终端R会话中加载的其他可能与崩溃相关的环境变量?安装正确的keras之前,我在正确设置Python环境时遇到了问题,并收到以下有关scipy版本问题的消息:
ERROR: After October 2020 you may experience errors when installing or updating packages. This is because pip will change the way that it resolves dependency conflicts (full output pasted in at the bottom).

We recommend you use --use-feature=2020-resolver to test your packages with the new resolver before it becomes the default.

tensorflow 2.2.0 requires scipy==1.4.1; python_version >= "3", but you'll have scipy 1.5.2 which is incompatible.

感谢您的帮助。

0 个答案:

没有答案