RStudio crashes when running keras

Viewed 1039

My RStudio session crashes when trying to run functions of the keras R package. I get an error "R Session Aborted" as a message in a window but can't recover any additional information about its causes.

enter image description here

I cannot reproduce the error when running the same set of commands in an R session from the terminal. The following script runs through just fine when running it with R in the shell, but crashes at the line mnist_y <- to_categorical(mnist_y, 10) when running it in RStudio. In both cases, I first activate a conda session in the terminal with conda activate r-reticulate. The code example taken from Bradley Boehmke's awesome book 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 

My questions are:

  • What causes RStudio to crash while a R session in the terminal does not?
  • How can I recover error messages from the aborted R(Studio) session?
  • Are there any additional environmental variables, possibly related to the crash, loaded in RStudio that are not loaded in the terminal-R session? I've had issues before with setting the Python environment correctly and receive the following message about an issue with the scipy version, upon installing keras:
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.

Thanks for any help.

0 Answers
Related