Keras API in R
We learned about the Keras API in Chapter 3. In R, this API is implemented with the keras
R package. The keras
R package implements most of the functionality of the Keras Python interface, including both the sequential API and the functional API.
As an example, we provide a walkthrough of the MLP Model for classifying handwritten digits from the MNIST dataset at the following link: https://keras.rstudio.com/articles/examples/mnist_mlp.html.
Note
You can follow along with the code in the Jupyter R notebook ch-17c_Keras_in_R
.
- First, load the library:
library(keras)
- Define the hyper-parameters:
batch_size <- 128 num_classes <- 10 epochs <- 30
- Prepare the data:
# The data, shuffled and split between train and test sets c(c(x_train, y_train), c(x_test, y_test)) %<-% dataset_mnist() x_train <- array_reshape(x_train, c(nrow(x_train), 784)) x_test <- array_reshape(x_test, c(nrow(x_test), 784)) # Transform RGB values into [0,1] range x_train <- x_train / 255 x_test <...