### R/Keras Examples for FFNs & CNNs ### Based on “Deep Learning wth R” by Chollet & Allaire; ### https://www.manning.com/books/deep-learning-with-r ##install Keras: install.packages("keras") library(keras) #CPU-based: install_keras() ##Loading the MNIST dataset in Keras: library(keras) mnist <- dataset_mnist() train_images <- mnist$train$x train_labels <- mnist$train$y test_images <- mnist$test$x test_labels <- mnist$test$y str(train_images) # int [1:60000, 1:28, 1:28] 0 0 0 0 0 0 0 0 0 0 ... str(test_labels) # int [1:10000(1d)] 7 2 1 0 4 1 4 9 5 9 ... 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 <- to_categorical(train_labels) test_labels <- to_categorical(test_labels) ###FFN: network <- keras_model_sequential() %>% layer_dense(units = 512, activation = "relu", input_shape = c(28 * 28)) %>% layer_dense(units = 10, activation = "softmax") network %>% compile( optimizer = "rmsprop", loss = "categorical_crossentropy", metrics = c("accuracy") ) network %>% fit(train_images, train_labels, epochs = 5, batch_size = 128) #Epoch 1/5 469/469 [==============================] - 2s 4ms/step - loss: 0.2575 - accuracy: 0.9254 #Epoch 2/5 469/469 [==============================] - 2s 3ms/step - loss: 0.1031 - accuracy: 0.9695 #Epoch 3/5 469/469 [==============================] - 2s 3ms/step - loss: 0.0688 - accuracy: 0.9792 #Epoch 4/5 469/469 [==============================] - 2s 3ms/step - loss: 0.0489 - accuracy: 0.9857 #Epoch 5/5 469/469 [==============================] - 2s 3ms/step - loss: 0.0380 - accuracy: 0.9887 metrics <- network %>% evaluate(test_images, test_labels, verbose = 0) metrics # loss accuracy # 0.06546536 0.98079997 ###CNN: library(keras) model <- keras_model_sequential() %>% layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = "relu", input_shape = c(28, 28, 1)) %>% 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_conv_2d(filters = 64, kernel_size = c(3, 3), activation = "relu") %>% layer_flatten() %>% layer_dense(units = 64, activation = "relu") %>% layer_dense(units = 10, activation = "softmax") mnist <- dataset_mnist() c(c(train_images, train_labels), c(test_images, test_labels)) %<-% mnist train_images <- array_reshape(train_images, c(60000, 28, 28, 1)) train_images <- train_images / 255 test_images <- array_reshape(test_images, c(10000, 28, 28, 1)) test_images <- test_images / 255 train_labels <- to_categorical(train_labels) test_labels <- to_categorical(test_labels) model %>% compile( optimizer = "rmsprop", loss = "categorical_crossentropy", metrics = c("accuracy") ) model %>% fit( train_images, train_labels, epochs = 5, batch_size=64 ) results <- model %>% evaluate(test_images, test_labels) results #$loss #[1] 0.02563557 #$acc #[1] 0.993