Assignment 02# - Machine Learning 2023
Assignment 02# - Machine Learning 2023
Machine Learning
Assignment 02#
Submitted To:
Sir Hafiz Abdurrahman
Submitted By:
Ijaz ull haq
F2019266400
21/11/2023
It is a widely used and deeply understood dataset and, for the most part, is “solved.” Top-
performing models are deep learning convolutional neural networks that achieve a
classification accuracy of above 99%, with an error rate between 0.4 %and 0.2% on the
hold out test dataset.
The example below loads the MNIST dataset using the Keras API and creates a plot of
the first nine images in the training dataset.
Load Dataset
We know some things about the dataset.
For example, we know that the images are all pre-aligned (e.g. each image only contains
a hand-drawn digit), that the images all have the same square size of 28×28 pixels, and
that the images are grayscale.
Therefore, we can load the images and reshape the data arrays to have a single color
channel.
1 # load dataset
2 (trainX, trainY), (testX, testY) = mnist.load_data()
3 # reshape dataset to have a single channel
4 trainX = trainX.reshape((trainX.shape[0], 28, 28, 1))
5 testX = testX.reshape((testX.shape[0], 28, 28, 1))
We also know that there are 10 classes and that classes are represented as unique
integers.
We can, therefore, use a one hot encoding for the class element of each sample,
transforming the integer into a 10 element binary vector with a 1 for the index of the class
value, and 0 values for all other classes. We can achieve this with
the to_categorical() utility function.
1 # one hot encode target values
2 trainY = to_categorical(trainY)
3 testY = to_categorical(testY)
The load_dataset() function implements these behaviors and can be used to load the
dataset.
1 # load train and test dataset
2 def load_dataset():
3 # load dataset
4 (trainX, trainY), (testX, testY) = mnist.load_data()
5 # reshape dataset to have a single channel
6 trainX = trainX.reshape((trainX.shape[0], 28, 28, 1))
7 testX = testX.reshape((testX.shape[0], 28, 28, 1))
8 # one hot encode target values
9 trainY = to_categorical(trainY)
10 testY = to_categorical(testY)
11 return trainX, trainY, testX, testY
Define Model
Next, we need to define a baseline convolutional neural network model for the problem.
The model has two main aspects: the feature extraction front end comprised of
convolutional and pooling layers, and the classifier backend that will make a prediction.
For the convolutional front-end, we can start with a single convolutional layer with a small
filter size (3,3) and a modest number of filters (32) followed by a max pooling layer. The
filter maps can then be flattened to provide features to the classifier.
Given that the problem is a multi-class classification task, we know that we will require an
output layer with 10 nodes in order to predict the probability distribution of an image
belonging to each of the 10 classes. This will also require the use of a softmax activation
function. Between the feature extractor and the output layer, we can add a dense layer to
interpret the features, in this case with 100 nodes.
The define_model() function below will define and return this model.
1 # define cnn model
2 def define_model():
3 model = Sequential()
4 model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=(28, 28, 1)))
5 model.add(MaxPooling2D((2, 2)))
6 model.add(Flatten())
7 model.add(Dense(100, activation='relu', kernel_initializer='he_uniform'))
8 model.add(Dense(10, activation='softmax'))
9 # compile model
10 opt = SGD(learning_rate=0.01, momentum=0.9)
11 model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
12 return model
Evaluate Model
After the model is defined, we need to evaluate it.
The model will be evaluated using five-fold cross-validation. The value of k=5 was chosen
to provide a baseline for both repeated evaluation and to not be so large as to require a
long running time. Each test set will be 20% of the training dataset, or about 12,000
examples, close to the size of the actual test set for this problem.
The training dataset is shuffled prior to being split, and the sample shuffling is performed
each time, so that any model we evaluate will have the same train and test datasets in
each fold, providing an apples-to-apples comparison between models.
The evaluate_model() function below implements these behaviors, taking the training
dataset as arguments and returning a list of accuracy scores and training histories that
can be later summarized.
1 # evaluate a model using k-fold cross-validation
2 def evaluate_model(dataX, dataY, n_folds=5):
3 scores, histories = list(), list()
4 # prepare cross validation
5 kfold = KFold(n_folds, shuffle=True, random_state=1)
6 # enumerate splits
7 for train_ix, test_ix in kfold.split(dataX):
8 # define model
9 model = define_model()
10 # select rows for train and test
11 trainX, trainY, testX, testY = dataX[train_ix], dataY[train_ix], dataX[test_ix], dataY[test_ix]
12 # fit model
13 history = model.fit(trainX, trainY, epochs=10, batch_size=32, validation_data=(testX, testY), verbose=0)
14 # evaluate model
15 _, acc = model.evaluate(testX, testY, verbose=0)
16 print('> %.3f' % (acc * 100.0))
17 # stores scores
18 scores.append(acc)
19 histories.append(history)
20 return scores, histories
Complete Example
We need a function that will drive the test harness.
We now have everything we need; the complete code example for a baseline
convolutional neural network model on the MNIST dataset is listed below.