TensorFlow 2.0 - Causing R Session to Crash

Problem

I'm running an Rmarkdown, using reticulate to use a Python 3.7 environment I created with Conda. The problem I'm running into is during model fitting, RStudio Crashes.

Reproducible Example

Terminal - Create Conda Environment

conda create -n py3.7 python=3.7 tensorflow scikit-learn pandas numpy matplotlib

Rmarkdown

The R setup.

library(tidyverse)
library(reticulate)

use_condaenv("py3.7", required = TRUE)

Python in RMarkdown

This example comes from the Basic classification: Classify images of clothing

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)

2.0.0

fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10)
])
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
model.summary()

Here's where RStudio Crashes.

model.fit(train_images, train_labels, epochs=10)

I was able to fix this issue by installing tensorflow using the R package tensorflow. I'm guessing there was something incompatible between the libraries and environments I selected with Python 3.7, Tensorflow 2.0.0, and Conda.

Fix

Run this in R.

library(tensorflow)

install_tensorflow(method = "conda", envname = "py3.6", conda_python_version = "3.6")

The Sweet Taste of Success

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.