This tutorial shows the hyperparameter tuning for MNIST dataset.
library(keras)
library(tensorflow)
library(kerastuneR)
if(tensorflow::tf_gpu_configured()) {
= tf$config$list_physical_devices('GPU')
physical_devices $config$experimental$set_memory_growth(physical_devices[[1]],TRUE)
tf
}
# The data, shuffled and split between train and test sets
<- dataset_mnist()
mnist <- mnist$train$x
x_train <- mnist$train$y
y_train <- mnist$test$x
x_test <- mnist$test$y
y_test
= function(x, hp) {
augment_images = hp$Boolean('use_rotation')
use_rotation if(use_rotation) {
= tf$keras$layers$experimental$preprocessing$RandomRotation(
x $Float('rotation_factor', min_value=0.05, max_value=0.2)
hp
)(x)
}= hp$Boolean('use_zoom')
use_zoom if(use_zoom) {
= tf$keras$layers$experimental$preprocessing$RandomZoom(
x $Float('use_zoom', min_value=0.05, max_value=0.2)
hp
)(x)
}
x
}
= function(hp) {
make_model = layer_input(shape=c(28, 28, 1))
inputs = tf$keras$layers$experimental$preprocessing$Rescaling(1. / 255)(inputs)
x = tf$keras$layers$experimental$preprocessing$Resizing(64L, 64L)(x)
x = augment_images(x, hp)
x = hp$Int('num_block', min_value=2, max_value=5, step=1)
num_block = hp$Int('num_filters', min_value=32, max_value=128, step=32)
num_filters for (i in 1:length(num_block)) {
= x %>% layer_conv_2d(
x
num_filters,kernel_size=3,
activation='relu',
padding='same'
%>%
) layer_conv_2d(
num_filters,kernel_size=3,
activation='relu',
padding='same'
%>% layer_max_pooling_2d(2)
)
}= hp$Choice('reduction_type', c('flatten', 'avg'))
reduction_type
if(reduction_type == 'flatten') {
= x %>% layer_flatten()
x else {
} = x %>% layer_global_average_pooling_2d()
x
}
= x %>% layer_dense(
x units=hp$Int('num_dense_units', min_value=32, max_value=512, step=32),
activation='relu'
%>% layer_dropout(
) $Float('dense_dropout', min_value = 0., max_value = 0.7)
hp
)
= x %>% layer_dense(10)
outputs = keras_model(inputs, outputs)
model = hp$Float('learning_rate', min_value = 3e-4, max_value = 3e-3)
learning_rate = optimizer_adam(lr=1e-3)
optimizer %>% compile(loss = tf$keras$losses$SparseCategoricalCrossentropy(from_logits = TRUE),
model optimizer = optimizer,
metrics = tf$keras$metrics$SparseCategoricalAccuracy(name='acc'))
%>% summary()
model return(model)
}
= RandomSearch(
tuner
make_model,objective='val_acc',
max_trials=2,
overwrite=TRUE)
=callback_early_stopping(monitor = 'val_acc', mode = 'max',
callbackspatience = 3, baseline = 0.9)
%>% fit_tuner(x_train, y_train, validation_split = 0.2,
tuner callbacks = list(callbacks), verbose=1, epochs=2)
Extract model and retrain:
= tuner %>% get_best_models(1)
best_hp = model %>% fit(x_train, y_train, validation_split = 0.2, epochs = 2) history