Skip to contents

Setup

Prepare the data

# Model / data parameters
num_classes <- 10
input_shape <- c(28, 28, 1)

# Load the data and split it between train and test sets
c(c(x_train, y_train), c(x_test, y_test)) %<-% dataset_mnist()

# Scale images to the [0, 1] range
x_train <- x_train / 255
x_test <- x_test / 255
# Make sure images have shape (28, 28, 1)
x_train <- op_expand_dims(x_train, -1)
x_test <- op_expand_dims(x_test, -1)


dim(x_train)
## [1] 60000    28    28     1
dim(x_test)
## [1] 10000    28    28     1
# convert class vectors to binary class matrices
y_train <- to_categorical(y_train, num_classes)
y_test <- to_categorical(y_test, num_classes)

Build the model

model <- keras_model_sequential(input_shape = input_shape)
model |>
  layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = "relu") |>
  layer_max_pooling_2d(pool_size = c(2, 2)) |>
  layer_conv_2d(filters = 64, kernel_size = c(3, 3), activation = "relu") |>
  layer_max_pooling_2d(pool_size = c(2, 2)) |>
  layer_flatten() |>
  layer_dropout(rate = 0.5) |>
  layer_dense(units = num_classes, activation = "softmax")

summary(model)
## Model: "sequential"
## ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
## ┃ Layer (type)                     Output Shape                  Param # 
## ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
## │ conv2d_1 (Conv2D)               │ (None, 26, 26, 32)     │           320
## ├─────────────────────────────────┼────────────────────────┼───────────────┤
## │ max_pooling2d_1 (MaxPooling2D)  │ (None, 13, 13, 32)     │             0
## ├─────────────────────────────────┼────────────────────────┼───────────────┤
## │ conv2d (Conv2D)                 │ (None, 11, 11, 64)     │        18,496
## ├─────────────────────────────────┼────────────────────────┼───────────────┤
## │ max_pooling2d (MaxPooling2D)    │ (None, 5, 5, 64)       │             0
## ├─────────────────────────────────┼────────────────────────┼───────────────┤
## │ flatten (Flatten)               │ (None, 1600)           │             0
## ├─────────────────────────────────┼────────────────────────┼───────────────┤
## │ dropout (Dropout)               │ (None, 1600)           │             0
## ├─────────────────────────────────┼────────────────────────┼───────────────┤
## │ dense (Dense)                   │ (None, 10)             │        16,010
## └─────────────────────────────────┴────────────────────────┴───────────────┘
##  Total params: 34,826 (136.04 KB)
##  Trainable params: 34,826 (136.04 KB)
##  Non-trainable params: 0 (0.00 B)

Train the model

batch_size <- 128
epochs <- 15

model |> compile(
  loss = "categorical_crossentropy",
  optimizer = "adam",
  metrics = "accuracy"
)

model |> fit(
  x_train, y_train,
  batch_size = batch_size,
  epochs = epochs,
  validation_split = 0.1
)
## Epoch 1/15
## 422/422 - 5s - 12ms/step - accuracy: 0.8845 - loss: 0.3815 - val_accuracy: 0.9783 - val_loss: 0.0811
## Epoch 2/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9643 - loss: 0.1152 - val_accuracy: 0.9865 - val_loss: 0.0548
## Epoch 3/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9738 - loss: 0.0838 - val_accuracy: 0.9880 - val_loss: 0.0456
## Epoch 4/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9782 - loss: 0.0689 - val_accuracy: 0.9893 - val_loss: 0.0419
## Epoch 5/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9812 - loss: 0.0609 - val_accuracy: 0.9907 - val_loss: 0.0374
## Epoch 6/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9823 - loss: 0.0562 - val_accuracy: 0.9910 - val_loss: 0.0364
## Epoch 7/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9849 - loss: 0.0489 - val_accuracy: 0.9922 - val_loss: 0.0329
## Epoch 8/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9846 - loss: 0.0484 - val_accuracy: 0.9917 - val_loss: 0.0330
## Epoch 9/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9860 - loss: 0.0441 - val_accuracy: 0.9922 - val_loss: 0.0318
## Epoch 10/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9869 - loss: 0.0406 - val_accuracy: 0.9923 - val_loss: 0.0316
## Epoch 11/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9876 - loss: 0.0388 - val_accuracy: 0.9923 - val_loss: 0.0314
## Epoch 12/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9884 - loss: 0.0369 - val_accuracy: 0.9925 - val_loss: 0.0305
## Epoch 13/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9878 - loss: 0.0360 - val_accuracy: 0.9928 - val_loss: 0.0276
## Epoch 14/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9890 - loss: 0.0332 - val_accuracy: 0.9932 - val_loss: 0.0290
## Epoch 15/15
## 422/422 - 1s - 2ms/step - accuracy: 0.9900 - loss: 0.0308 - val_accuracy: 0.9925 - val_loss: 0.0296

Evaluate the trained model

score <- model |> evaluate(x_test, y_test, verbose=0)
score
## $accuracy
## [1] 0.9912
##
## $loss
## [1] 0.02584332