- Keras and TensorFlow are the state of the art in deep learning tools and with the keras package you can now access both with a fluent R interface.
- To prepare this data for training we one-hot encode the vectors into binary class matrices using the Keras function:
The core data structure of Keras is a model, a way to organize layers.

- We begin by creating a sequential model and then adding layers using the pipe ( ) operator:
The argument to the first layer specifies the shape of the input data (a length 784 numeric vector representing a grayscale image).

- Use the function to print the details of the model:
Next, compile the model with appropriate loss function, optimizer, and metrics:

Use the function to train the model for 30 epochs using batches of 128 images:

The object returned by includes loss and accuracy metrics which we can plot:

Evaluate the model’s performance on the test data:

Keras provides a vocabulary for building deep learning models that is simple, elegant, and intuitive.

- After you’ve become familiar with the basics, these articles are a good next step:
Keras provides a productive, highly flexible framework for developing deep learning models.

We are excited to announce that the keras package is now available on CRAN. The package provides an R interface to Keras, a high-level neural networks API developed with a focus on enabling fast experimentation. Keras has the following key features:

@rstudio: *R interface to Keras, now on CRAN: #deeplearning #keras #rstats*

We are excited to announce that the keras package is now available on CRAN. The package provides an R interface to Keras, a high-level neural networks API developed with a focus on enabling fast experimentation. Keras has the following key features:

Allows the same code to run on CPU or on GPU, seamlessly.

User-friendly API which makes it easy to quickly prototype deep learning models.

Built-in support for convolutional networks (for computer vision), recurrent networks (for sequence processing), and any combination of both.

Supports arbitrary network architectures: multi-input or multi-output models, layer sharing, model sharing, etc. This means that Keras is appropriate for building essentially any deep learning model, from a memory network to a neural Turing machine.

Is capable of running on top of multiple back-ends including TensorFlow, CNTK, or Theano.

If you are already familiar with Keras and want to jump right in, check out https://keras.rstudio.com which has everything you need to get started including over 20 complete examples to learn from.

To learn a bit more about Keras and why we’re so excited to announce the Keras interface for R, read on!

Interest in deep learning has been accelerating rapidly over the past few years, and several deep learning frameworks have emerged over the same time frame. Of all the available frameworks, Keras has stood out for its productivity, flexibility and user-friendly API. At the same time, TensorFlow has emerged as a next-generation machine learning platform that is both extremely flexible and well-suited to production deployment.

Not surprisingly, Keras and TensorFlow have of late been pulling away from other deep learning frameworks:

The good news about Keras and TensorFlow is that you don’t need to choose between them! The default backend for Keras is TensorFlow and Keras can be integrated seamlessly with TensorFlow workflows. There is also a pure-TensorFlow implementation of Keras with deeper integration on the roadmap for later this year.

Keras and TensorFlow are the state of the art in deep learning tools and with the keras package you can now access both with a fluent R interface.

To begin, install the keras R package from CRAN as follows:

function:

We can learn the basics of Keras by walking through a simple example: recognizing handwritten digits from the MNIST dataset. MNIST consists of 28 x 28 grayscale images of handwritten digits like these:

The dataset also includes labels for each image, telling us which digit it is. For example, the labels for the above images are 5, 0, 4, and 1.

function. Here we load the dataset then create variables for our test and training data:

x

of grayscale values. To prepare the data for training we convert the 3-d arrays into matrices by reshaping width and height into a single dimension (28×28 images are flattened into length 784 vectors). Then, we convert the grayscale values from integers ranging between 0 to 255 into floating point values ranging between 0 and 1:

y

function:

The core data structure of Keras is a model, a way to organize layers. The simplest type of model is the sequential model, a linear stack of layers.

) operator:

argument to the first layer specifies the shape of the input data (a length 784 numeric vector representing a grayscale image). The final layer outputs a length 10 numeric vector (probabilities for each digit) using a softmax activation function.

function to print the details of the model:

Model ________________________________________________________________________________ Layer (type) Output Shape Param # ================================================================================ dense_1 (Dense) (None, 256) 200960 ________________________________________________________________________________ dropout_1 (Dropout) (None, 256) 0 ________________________________________________________________________________ dense_2 (Dense) (None, 128) 32896 ________________________________________________________________________________ dropout_2 (Dropout) (None, 128) 0 ________________________________________________________________________________ dense_3 (Dense) (None, 10) 1290 ================================================================================ Total params: 235,146 Trainable params: 235,146 Non-trainable params: 0 ________________________________________________________________________________

Next, compile the model with appropriate loss function, optimizer, and metrics:

function to train the model for 30 epochs using batches of 128 images:

includes loss and accuracy metrics which we can plot:

Evaluate the model’s performance on the test data:

$loss [1] 0.1149 $acc [1] 0.9807

Generate predictions on new data:

[1] 7 2 1 0 4 1 4 9 5 9 0 6 9 0 1 5 9 7 3 4 9 6 6 5 4 0 7 4 0 1 3 1 3 4 7 2 7 1 2 [40] 1 1 7 4 2 3 5 1 2 4 4 6 3 5 5 6 0 4 1 9 5 7 8 9 3 7 4 6 4 3 0 7 0 2 9 1 7 3 2 [79] 9 7 7 6 2 7 8 4 7 3 6 1 3 6 9 3 1 4 1 7 6 9 [ reached getOption(“max.print”) — omitted 9900 entries ]

Keras provides a vocabulary for building deep learning models that is simple, elegant, and intuitive. Building a question answering system, an image classification model, a neural Turing machine, or any other model is just as straightforward.

The Guide to the Sequential Model article describes the basics of Keras sequential models in more depth.

Over 20 complete examples are available (special thanks to @dfalbel for his work on these!). The examples cover image classification, text generation with stacked LSTMs, question-answering with memory networks, transfer learning, variational encoding, and more.

After you’ve become familiar with the basics, these articles are a good next step:

Guide to the Sequential Model. The sequential model is a linear stack of layers and is the API most users should start with.

Guide to the Functional API. The Keras functional API is the way to go for defining complex models, such as multi-output models, directed acyclic graphs, or models with shared layers.

Training Visualization. There are a wide variety of tools available for visualizing training. These include plotting of training metrics, real time display of metrics within the RStudio IDE, and integration with the TensorBoard visualization tool included with TensorFlow.

Using Pre-Trained Models. Keras includes a number of deep learning models (Xception, VGG16, VGG19, ResNet50, InceptionVV3, and MobileNet) that are made available alongside pre-trained weights. These models can be used for prediction, feature extraction, and fine-tuning.

Frequently Asked Questions. Covers many additional topics including streaming training data, saving models, training on GPUs, and more.

Keras provides a productive, highly flexible framework for developing deep learning models. We can’t wait to see what the R community will do with these tools!