0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Basics of Machine Learning: Classification (MNIST)

Posted at

Introduction

The operation was tested under the following conditions:

・Environment: Google Colaboratory
・Date: November 19, 2025
・Runtime: T4 GPU

1. Preparation

Mount Google Drive
from google.colab import drive
drive.mount('/content/drive')
Move to the directory where files will be saved
# Modify according to your environment
%cd /content/drive/MyDrive/Lecture_03_Classification/
Check versions, etc.
import sys
import tensorflow as tf
import tensorflow.keras
import pandas as pd
import sklearn as sk
import tensorflow as tf
print(f"Tensor Flow Version: {tf.__version__}")
print(f"Keras Version: {tensorflow.keras.__version__}")
print()
print(f"Python {sys.version}")
print(f"Pandas {pd.__version__}")
print(f"Scikit-Learn {sk.__version__}")
gpu = len(tf.config.list_physical_devices('GPU'))>0
print("GPU is", "available" if gpu else "NOT AVAILABLE")
Nicely formatted time string
def hms_string(sec_elapsed):
    h = int(sec_elapsed / (60 * 60))
    m = int((sec_elapsed % (60 * 60)) / 60)
    s = sec_elapsed % 60
    return f"{h}:{m:>02}:{s:>05.2f}"

2. Check the dataset used for training

Download the dataset
import tensorflow.keras
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras import regularizers
from tensorflow.keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
print("Shape of x_train: {}".format(x_train.shape))
print("Shape of y_train: {}".format(y_train.shape))

print()
print("Shape of x_test: {}".format(x_test.shape) )
print("Shape of y_test: {}".format(y_test.shape) )
Display a single digit image
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

digit = 105 # Change to choose new digit
a = x_train[digit]
plt.imshow(a, cmap='gray', interpolation='nearest')

print("Image (#{}): Which is digit '{}'".format(digit, y_train[digit]) )

from IPython.display import display
Display one digit (as numeric data)
import pandas as pd
pd.set_option('display.max_columns', 29)
pd.set_option('display.max_rows', 29)

print("Shape for dataset: {}".format(x_train.shape))
print("Labels: {}".format(y_train))

# single MNIST digit
digit = 105
single = x_train[digit]
print("Shape for single: {}".format(single.shape))

pd.DataFrame(single.reshape(28,28))
Display multiple digits randomly
import random

ROWS = 6
random_indices = random.sample(range(x_train.shape[0]), ROWS*ROWS)
sample_images = x_train[random_indices, :]
plt.clf()
fig, axes = plt.subplots(ROWS, ROWS, figsize=(ROWS,ROWS),sharex=True, sharey=True)

for i in range(ROWS*ROWS):
  subplot_row = int(i/ROWS)
  subplot_col = int(i%ROWS)
  ax = axes[subplot_row, subplot_col]

  plottable_image = np.reshape(sample_images[i,:], (28,28))
  ax.imshow(plottable_image, cmap='gray_r')

  ax.set_xbound([0,28])

plt.tight_layout()
plt.show()

3. Preprocessing of input data

Preprocessing of input data
import tensorflow.keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras import backend as K

batch_size = 128
num_classes = 10
epochs = 12
 #input image dimensions
img_rows, img_cols = 28, 28

if K.image_data_format() == 'channels_first':
  x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
  x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
  input_shape = (1, img_rows, img_cols)

else:
  x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
  x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
  input_shape = (img_rows, img_cols, 1)

x_train= x_train.astype('float32')
x_test = x_test.astype('float32')


x_train /= 255.0
x_test /= 255.0


print("Training samples: {}".format(x_train.shape[0]))
print("Test samples: {}".format(x_test.shape[0]))

# convert class vectors to binary class matrices
y_train = tensorflow.keras.utils.to_categorical(y_train, num_classes)
y_test = tensorflow.keras.utils.to_categorical(y_test, num_classes)

print('x_train shape:', x_train.shape)
print('y_train shape:', y_train.shape)
print('x_test shape:', x_test.shape)
print('y_test shape:', y_test.shape)

4. Build a Neural Network

Create a simple fully connected (Dense layers)neural network
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(512, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
Visualize the model structure using plot mode
from tensorflow.keras.utils import plot_model
plot_model(model, show_shapes=True, to_file="model.png", dpi=60)

5. Train the model

Train the model
import tensorflow as tf
import time

start_time = time.time()

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=2,
          validation_data=(x_test,y_test)
          )

score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss: {}'.format(score[0]))
print('Test accuracy: {}'.format(score[1]))

elapsed_time = time.time() - start_time
print("Elapsed time: {}".format(hms_string(elapsed_time)))

6. Evaluate the trained neural network

Evaluate the trained neural network
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss: {}'.format(score[0]))
print('Test accuracy: {}'.format(score[1]))

from sklearn import metrics

# For GPU just grab the first 100 images
small_x = x_test[1:100]
small_y = y_test[1:100]

small_y2 = np.argmax(small_y, axis=1)
pred = model.predict(small_x)
pred = np.argmax(pred,axis=1)
score = metrics.accuracy_score(small_y2, pred)
print('Accuracy with a limited number of samples: {}'.format(score))

7. Save and Load

Save the trained neural network
import os
save_path = "."

model.save(os.path.join(save_path,"my_network.keras"))

Load the saved neural network
from tensorflow.keras.models import load_model
import os

save_path = "." # Assuming the model was saved in the current directory

model2 = load_model(os.path.join(save_path, 'my_network.keras'))
model2.summary() # Output model information

Test the loaded model
from sklearn import metrics

small_x = x_test[1:1000]
small_y = y_test[1:1000]
small_y2 = np.argmax(small_y, axis=1)
pred2 = model2.predict(small_x)
pred2 = np.argmax(pred2,axis=1)
score = metrics.accuracy_score(small_y2, pred2)
print('Accuracy with a limited number of samples: {}'.format(score))

Exercise

Improve the network architecture so that the Test accuracy after training becomes as high as possible.

Goal: Achieve a test accuracy of 0.99 or higher.

Hints:

 ・Introduce a Convolutional Neural Network (CNN)
 ・Introduce Dropout

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?