In this recipe, we will use the MNIST handwritten digit dataset. It has a training set of 60,000 examples and a test set of 10,000 examples.
We start by importing the required libraries:
library(keras)
library(abind)
library(grid)
Let's import the training and testing partitions of the data:
data = dataset_mnist()
x_train = data$train$x
x_test = data$test$x
cat("Train data dimnsions",dim(x_train)," ")
cat("Test data dimnsions",dim(x_test))
In the following screenshot, we can see that the MNIST data has 60,000 train and 10,000 test images of a size of 28x28:
Let's take a look at the data of the first image:
x_train[1,,]
In the following screenshot, you can see that the image data is in the form of a multidimensional array:
We normalize the values of our train and test datasets within 0 and 1 and flatten each image of a size of 28X28 into a 1-dimensional array of 784 elements:
x_train = x_train/ 255
x_test = x_test / 255
x_train <- array_reshape(x_train, c(nrow(x_train), 784))
x_test <- array_reshape(x_test, c(nrow(x_test), 784))
Now that we have seen how the data looks, let's move to model building.