4  MNIST

# import libraries
import numpy as np
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import models, layers, optimizers, backend
# load dataset
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# transform image data
train_images = train_images.reshape((60000, 28 * 28)) / 255
test_images = test_images.reshape((10000, 28 * 28)) / 255

train_images.shape, train_labels.shape, test_images.shape, test_labels.shape
def explore(train_images, 
            train_labels, 
            test_images, 
            test_labels,
            label_count,
            neuron_count,
            learning_rate,
            momentum):

    # define ann architecture
    model = models.Sequential()
    model.add(layers.Dense(neuron_count, activation = "relu", input_shape = (28 * 28,)))
    model.add(layers.Dense(label_count, activation = "softmax"))

    # define optimizer, loss function, and metrics
    optimizer = optimizers.RMSprop(learning_rate = learning_rate, momentum = momentum)

    model.compile(optimizer = optimizer,
                  loss = "categorical_crossentropy",
                  metrics = ["accuracy"])

    # train ann model
    history = model.fit(train_images, train_labels, epochs = 20, batch_size = 64, verbose = 0)

    # evaluate ann model
    test_loss, test_acc = model.evaluate(test_images, test_labels, verbose = 0)

    return history, test_loss, test_acc
# set hyperparameters
learning_rates = np.logspace(-1, -4, 5)
momentums = np.logspace(-1, -4, 5)
neuron_counts = 2 ** np.arange(7, 12)

hyparameters_list = []
for learning_rate in learning_rates:
    for momentum in momentums:
        for neuron_count in neuron_counts:
            hyparameters_list.append({
                "learning_rate": learning_rate,
                "momentum": momentum,
                "neuron_count": neuron_count
            })
output = []
for hyparameters in hyparameters_list:
    history, test_loss, test_acc = explore(
        train_images,
        train_labels,
        test_images,
        test_labels,
        label_count = 10,
        learning_rate = hyparameters["learning_rate"],
        momentum = hyparameters["momentum"],
        neuron_count = hyparameters["neuron_count"]
    )
    
    output.append({
        "history": history,
        "test_loss": test_loss, 
        "test_acc": test_acc,
        "hyperparameters": hyparameters
    })
    backend.clear_session()

    print(f"A model is trained with hyperparameters of {hyparameters}")