# 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
4 MNIST
# load dataset
= mnist.load_data()
(train_images, train_labels), (test_images, test_labels)
= to_categorical(train_labels)
train_labels = to_categorical(test_labels)
test_labels
# transform image data
= train_images.reshape((60000, 28 * 28)) / 255
train_images = test_images.reshape((10000, 28 * 28)) / 255
test_images
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
= models.Sequential()
model = "relu", input_shape = (28 * 28,)))
model.add(layers.Dense(neuron_count, activation = "softmax"))
model.add(layers.Dense(label_count, activation
# define optimizer, loss function, and metrics
= optimizers.RMSprop(learning_rate = learning_rate, momentum = momentum)
optimizer
compile(optimizer = optimizer,
model.= "categorical_crossentropy",
loss = ["accuracy"])
metrics
# train ann model
= model.fit(train_images, train_labels, epochs = 20, batch_size = 64, verbose = 0)
history
# evaluate ann model
= model.evaluate(test_images, test_labels, verbose = 0)
test_loss, test_acc
return history, test_loss, test_acc
# set hyperparameters
= np.logspace(-1, -4, 5)
learning_rates = np.logspace(-1, -4, 5)
momentums = 2 ** np.arange(7, 12)
neuron_counts
= []
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:
= explore(
history, test_loss, test_acc
train_images,
train_labels,
test_images,
test_labels,= 10,
label_count = hyparameters["learning_rate"],
learning_rate = hyparameters["momentum"],
momentum = hyparameters["neuron_count"]
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}")