--- title: "MNIST Example" author: "Prof. Eric A. Suess" format: html: embed-resources: true --- ## MNIST Feedforward neural network with **one** hidden layer Load the packages ```{r} library(tensorflow) library(keras) ``` Load the MNIST dataset. ```{r} mnist <- dataset_mnist() train_images <- mnist$train$x train_labels <- mnist$train$y test_images <- mnist$test$x test_labels <- mnist$test$y ``` Examine the data. ```{r} str(train_images) str(train_labels) str(test_images) str(test_labels) ``` Preprocess the data. ```{r} train_images <- array_reshape(train_images, c(60000, 28 * 28)) train_images <- train_images / 255 test_images <- array_reshape(test_images, c(10000, 28 * 28)) test_images <- test_images / 255 ``` Specify a neural network model. ```{r} model <- keras_model_sequential(list( layer_dense(units = 512, activation = "relu"), layer_dense(units = 10, activation = "softmax") )) ``` ```{r} ## ------------------------------------------------------------------------- compile(model, optimizer = "rmsprop", loss = "sparse_categorical_crossentropy", metrics = "accuracy") ``` Train the model. ```{r} fit(model, train_images, train_labels, epochs = 5, batch_size = 128) ``` Make predictions. Inference. ```{r} test_digits <- test_images[1:10, ] predictions <- predict(model, test_digits) str(predictions) predictions[1, ] which.max(predictions[1, ]) predictions[1, 8] test_labels[1] ``` Evaluate the model. ```{r} metrics <- evaluate(model, test_images, test_labels) metrics["accuracy"] ``` ## MNIST Feedforward neural network with **two** hidden layers Reload the images. ```{r} mnist <- dataset_mnist() train_images <- mnist$train$x train_labels <- mnist$train$y test_images <- mnist$test$x test_labels <- mnist$test$y ``` View an image. ```{r} digit <- train_images[1, , ] par(mar = c(0, 0, 0, 0)) plot(as.raster(abs(255 - digit), max = 255)) ``` ```{r} train_labels[1] ``` ```{r} digit <- test_images[1, , ] par(mar = c(0, 0, 0, 0)) plot(as.raster(abs(255 - digit), max = 255)) ``` ```{r} test_labels[1] ``` Preprocess the data. ```{r} train_images <- array_reshape(train_images, c(60000, 28 * 28)) train_images <- train_images / 255 test_images <- array_reshape(test_images, c(10000, 28 * 28)) test_images <- test_images / 255 train_labels <- mnist$train$y test_labels <- mnist$test$y ``` New model. ```{r} model <- keras_model_sequential(list( layer_dense(units = 512, activation = "relu"), layer_dense(units = 10, activation = "softmax") )) ``` ```{r} compile(model, optimizer = "rmsprop", loss = "sparse_categorical_crossentropy", metrics = c("accuracy")) ``` Train the model. ```{r} fit(model, train_images, train_labels, epochs = 5, batch_size = 128) ``` Inference ```{r} predictions <- predict(model, test_images) str(predictions) predictions[1, ] which.max(predictions[1, ]) - 1 predictions[1, 8] test_labels[1] predicted_labels <- max.col(predictions) - 1 matches <- predicted_labels == test_labels cat(sprintf("accuracy: %.2f\n", mean(matches))) ```