Keras is a high-level neural networks API that was developed to enabling fast experimentation with Deep Learning in both Python and R. According to its author Taylor Arnold: Being able to go from idea to result with the least possible delay is key to doing good research. The ideas behind deep learning are simple, so why should their implementation be painful?
Keras comes with 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
- Fast implementation of dense neural networks, convolution neural networks (CNN) and recurrent neural networks (RNN) in R or Python, on top of TensorFlow or Theano.
The R interface to Keras uses TensorFlow™ as it’s underlying computation engine. First, you have to install the keras R package from GitHub:
install_tensorflow() function you can then install TensorFlow:
This will provide you with a default installation of TensorFlow suitable for use with the keras R package. See the article on TensorFlow installation to learn about more advanced options, including installing a version of TensorFlow that takes advantage of Nvidia GPUs if you have the correct CUDA libraries installed.
R: Getting started in 30 seconds
Keras uses models to organize layers. Sequential models are the simplest structure, simply stacking layers. More complex architectures require the Keras functional API, which allows to build arbitrary graphs of layers.
Here is an example of a sequential model (hosted on this website):
library(keras) model keras_model_sequential() model %>% layer_dense(units = 64, input_shape = 100) %>% layer_activation(activation = 'relu') %>% layer_dense(units = 10) %>% layer_activation(activation = 'softmax') model %>% compile( loss = 'categorical_crossentropy', optimizer = optimizer_sgd(lr = 0.02), metrics = c('accuracy') )
The above demonstrates the little effort needed to define your model. Now, you can iteratively train your model on batches of training data:
model %>% fit(x_train, y_train, epochs = 5, batch_size = 32)
Next, performance evaluation can be prompted in a single line of code:
loss_and_metrics %>% evaluate(x_test, y_test, batch_size = 128)
Similarly, generating predictions on new data is easily done:
classes %>% predict(x_test, batch_size = 128)
Building more complex models, for example, to answer questions or classify images, is just as fast.
A step-by-step implementation of several Neural Network architectures with Keras in Python can be found on DataCamp. Similarly, one may use this quick cheatsheet to deploy the most basic models.