""" MNIST classification with a fully-connected (dense) neural network. Each 28x28 image is flattened into a linear list of 784 inputs """ ######### COPY PASTE from import ... line to (x_train, ... line ########################################################################## import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers EPOCHS = 5 BATCH_SIZE = 128 tf.random.set_seed(0) # Load data and scale pixel values to [0, 1] (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() ########################################################################## ########## ###### TYPE IN THE NEXT 4 individual lines and then the 3 lines that show one example ###### and then change the 0 to 10 in the ones you typed to see different example ########## # Get a feel for the data you have loaded in type the following single lines in and # observe the output: #### x_train.shape #shows 60000 rows of training examples, each with 784 values (the gray-scale pixel values) #### y_train.shape #shows 60000 values which are the labels of each of the 60000 training examples #### x_train[0] #shows the 784 pixel values note: that many are 0 and others are close to 255, # you will need to scroll to see non-zeros #### print(y_train[0]) #shows the label of the 0th training image which is 5 # this next chunk will show the image of the 0th training example which is a handwritten 5 #### import matplotlib.pyplot as plt plt.imshow(x_train[0], cmap="binary") plt.axis("off") plt.show() # show the image of the 0th training example which is a handwritten 5 # go back to x_train[0] cell and below and instead of looking at the 0th #training example and its label enter say 10 #so x_train[10], y_train[10] and in imshow(x_train[10] etc. ########################################################################## ######### COPY PASTE next 2 lines which convert the input values from 0-255 to 0-1 ########################################################################## x_train = x_train.astype("float32") / 255.0 x_test = x_test.astype("float32") / 255.0 ########################################################################## ########## ###### TYPE IN THE NEXT 2 individual lines ########## # The two lines above converted the values from 0-255 to decimal values from 0-1 # notice: x_train[10] # now has decimal values rather than whole numbers from 0 to 255 # see the shape of one training example: x_train[10].shape # it is two dimensional 28 rows x 28 columns ######### COPY PASTE next 4 lines which flatten each 28x28 image into 784 values ########################################################################## x_train = x_train.reshape(-1, 28 * 28) x_test = x_test.reshape(-1, 28 * 28) print("Training input shape:", x_train.shape) # (60000, 784) print("Test input shape: ", x_test.shape) # (10000, 784) ########################################################################## # the above lines flatten the 2 dimensional images into 1 dimension of 784 values each # so now x_train[10].shape # is 1 dimension of 784 values ######### COPY PASTE next 11 lines which sets up the artifical neural network ########################################################################## model = keras.Sequential([ keras.Input(shape=(784,)), layers.Dense(8, activation="relu"), layers.Dense(10, activation="softmax"), ], name="dense_mnist") model.summary() model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) ########################################################################## ######### COPY PASTE from model.fit to second print which does the training and then testing ########################################################################## model.fit(x_train, y_train, epochs=EPOCHS, batch_size=BATCH_SIZE, validation_split=0.1, verbose=2) test_loss, test_acc = model.evaluate(x_test, y_test, verbose=0) print(f"\nDense NN parameters: {model.count_params():,}") print(f"Dense NN test accuracy: {test_acc:.4f}") ##########################################################################