Import library

library(keras)

Importing the data

mnist <- dataset_mnist()
## Loaded Tensorflow version 2.8.0
  • mnist is list; it contains trainx, trainy, testx, testy
class(mnist)
## [1] "list"
  • the dim of “mnist\(train\)x” is 60000 28 28
# head(mnist)

preparing the data

  • randomly sampling 1000 cases for training and 100 for testing
set.seed(123)
index <- sample(nrow(mnist$train$x), 1000)
x_train <- mnist$train$x[index,,]
y_train <- (mnist$train$y[index])

index <- sample(nrow(mnist$test$x), 100)
x_test <- mnist$test$x[index,,]
y_test <- (mnist$test$y[index])
  • dim of four data sets
dim(x_train)
## [1] 1000   28   28
dim(y_train)
## [1] 1000
dim(x_test)
## [1] 100  28  28
dim(y_test)
## [1] 100

Generate tensors

  • each image is 28*28 pixel size; pass these values to computer
img_rows <- 28
img_cols <- 28
  • using array_reshape() function to transform list data into tensors
x_train <- array_reshape(x_train,
                         c(nrow(x_train),
                           img_rows,
                           img_cols, 1))
x_test <- array_reshape(x_test,
                        c(nrow(x_test),
                          img_rows,
                          img_cols, 1))
input_shape <- c(img_rows,
                 img_cols, 1)
  • this below is tensor data
dim(x_train)
## [1] 1000   28   28    1

Normalization and one-hot-encoded (dummy)

  • training (features) data is rescaled by dividing the maxmimum to be normalized
x_train <- x_train / 255
x_test <- x_test / 255
  • converse targets into one-hot-encoded (dummy) type using to_categorical() function
num_classes = 10
y_train <- to_categorical(y_train, num_classes)
y_test <- to_categorical(y_test, num_classes)
y_train[1,]
##  [1] 0 0 0 0 0 0 1 0 0 0

Creating the model

model <- keras_model_sequential() %>%
  layer_conv_2d(filters = 32,
                kernel_size = c(3,3),
                activation = 'relu',
                input_shape = input_shape) %>% 
  
  layer_conv_2d(filters = 64,
                kernel_size = c(3,3),
                activation = 'relu') %>% 
  
  layer_max_pooling_2d(pool_size = c(2, 2)) %>% 
  
  layer_dropout(rate = 0.25) %>% 
  
  layer_flatten() %>% 
  layer_dense(units = 128,
              activation = 'relu') %>% 
  
  layer_dropout(rate = 0.5) %>% 
  layer_dense(units = num_classes,
              activation = 'softmax')
  • summary of model
model %>% summary()
## Model: "sequential"
## ________________________________________________________________________________
##  Layer (type)                       Output Shape                    Param #     
## ================================================================================
##  conv2d_1 (Conv2D)                  (None, 26, 26, 32)              320         
##  conv2d (Conv2D)                    (None, 24, 24, 64)              18496       
##  max_pooling2d (MaxPooling2D)       (None, 12, 12, 64)              0           
##  dropout_1 (Dropout)                (None, 12, 12, 64)              0           
##  flatten (Flatten)                  (None, 9216)                    0           
##  dense_1 (Dense)                    (None, 128)                     1179776     
##  dropout (Dropout)                  (None, 128)                     0           
##  dense (Dense)                      (None, 10)                      1290        
## ================================================================================
## Total params: 1,199,882
## Trainable params: 1,199,882
## Non-trainable params: 0
## ________________________________________________________________________________

compiling

  • loss function is categorical crossentropy; the gradient descent will be optimized by adadelta;
model %>% compile(
  loss = loss_categorical_crossentropy,
  optimizer = optimizer_adadelta(),
  metrics = c('accuracy')
)

Training

batch_size <- 128
epochs <- 10

# Train model
history <- model %>% fit(
  x_train, y_train,
  batch_size = batch_size,
  epochs = epochs,
  validation_split = 0.2
)
plot(history)

Evaluating the accuracy

score <- model %>% evaluate(x_test,
                            y_test)
score
##      loss  accuracy 
## 0.3416184 0.9200000