Overview

I wanted to consolidate a few articles (see References below) into one single source for installing Keras (Tensorflow backend) in R.


Python Side

Installing Anaconda

Keras in R will actually run Tensorflow using Python. Thus, we first need to install Python. Anaconda makes it easy to install Python, get all of the best libraries, a Python IDE, Jupyter, and most everything we would need to get started exploring Python in one convenient install. You can get Anaconda here.

Installing Keras/Tensorflow in Python

Open Anaconda Prompt and run the following code…

conda install -c conda-forge keras tensorflow
import keras

R Side

Installing Keras in R

First, we need to make sure we have DevTools installed so we can import the necessary packages, then install Keras.

install.packages("devtools")
devtools::install_github("rstudio/keras")

Set up data

To test that setup went as expected, we need to build and run a model. Before that, we need data…

#simulate some data
set.seed(123)
df <- data.frame(a=rnorm(1000),
                 b=rnorm(1000),
                 c=rnorm(1000))
y=ifelse(df$a>-.075 & df$b<.05 & df$c>0,
         sample(c(0,1),1,prob=c(.1,.9)),
         sample(c(1,0),1,prob=c(.1,.9)))
df <- cbind(y,df)

#split into train/test
test_ind <- seq(1, length(df$y), 5) 
train_x <- data.matrix(df[-test_ind,-1])
train_y <- data.matrix(df[-test_ind, 1])
test_x <-  data.matrix(df[test_ind,-1])
test_y <-  data.matrix(df[test_ind, 1])

Build and run model

If you can get through the next code block, everything is set up properly and you’re good to go.

library(keras)

# set keras seed
use_session_with_seed(345)

# defining a keras sequential model
model <- keras_model_sequential()

# model architecture
model %>% 
  layer_dense(units = 20, input_shape = ncol(train_x)) %>%
  layer_dropout(rate=0.25)%>%
  layer_activation(activation = 'relu') %>% 
  layer_dense(units = 1) %>%
  layer_activation(activation = 'sigmoid')

# compiling the defined model with metric = accuracy and optimiser as adam.
model %>% compile(
  loss = 'binary_crossentropy',
  optimizer = 'adam',
  metrics = 'binary_accuracy'
)

# fitting the model on the training dataset
model %>% fit(train_x, train_y, 
              epochs = 1000)

# score and produce predictions
score <- model %>% evaluate(test_x, test_y)
pred <- model %>% predict(test_x)

References

I sourced the above information from the following links…