How to do it...

The MNIST dataset is included in keras and can be accessed using the dataset_mnist() function:

  1. Let's load the data into the R environment:
mnist <- dataset_mnist()x_train <- mnist$train$xy_train <- mnist$train$yx_test <- mnist$test$xy_test <- mnist$test$y
  1. Our training data is of the form (images, width, height). Due to this, we'll convert the data into a one-dimensional array and rescale it:
# Reshaping the datax_train <- array_reshape(x_train , c(nrow(x_train),784))x_test <- array_reshape(x_test , c(nrow(x_test),784))# Rescaling the datax_train <- x_train/255x_test <- x_test/255
  1. Our target data is an integer vector and contains values from 0 to 9. We need to one-hot encode our target variable in order to convert it into ...

Get Deep Learning with R Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.