<jupyter_start><jupyter_text><jupyter_code># import tensorflow, keras, matplotlib and numpy
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
# import the fashion MNIST dataset 
fashion_mnist = keras.datasets.fashion_mnist
# separate the dataset into two
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() # type numpy.ndarray
print(train_images[0].shape)
train_images.shape # 60000 images of 28x28 pixels
np.unique(train_labels) # 10 different unique images
plt.figure()
plt.imshow(train_images[0]) # show the first image of the dataset from train_images
plt.colorbar() # see the intensity of the pixel of the image
plt.show() 
# ************ PREPROCESS THE DATASET *************
# a single image is made of 28x28 pixels of intensity of maximum 256
# individual elements of a single image is from 0-255 (look at color bar from the image above)
# we want 0-255 to be simply from 0-1: / 255 to obtain it
train_images = train_images / 255
test_images = test_images / 255 
# define the names of the labels (10)
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# subplot
plt.figure(figsize = (10, 10)) # create a new figure: figsize is size of the whole figure
for i in range(25): # we want to display a figure of 5 images by 5 images
    plt.subplot(5, 5, i+1) # (width, height, index) of subplot i != 0 
    plt.xticks([]) # disable xticks: ticks are values used to show specific points on the coordinate axis
    plt.yticks([])
    plt.grid(False) # remove grid lines
    plt.imshow(train_images[i], cmap = plt.cm.binary) # display an image: imshow(array_like or PIL_image, colormap)
    plt.xlabel(class_names[train_labels[i]]) # train_labels are arrays of int from 0-9 representing a clothing item
plt.show() # display first 25 images from train_labels dataset    
# ************ SET UP THE LAYERS **************
# input layer: flatten the two-dimensional array (28x28) into one-dimensional array (784)
# hidden layer: dense fully connected layer (relu)
# output layer: dense fully connected layer of 10 nodes (softmax)

# a model is used to make predictions 
# use keras to build the model: set and specify the layers
# an activation function's main purpose: convert an input signal of a node in a A-NN to an output signal
# relu means replified linear unit; very effective activation function 
model = keras.Sequential([
    keras.layers.Flatten(input_shape = (28, 28)),
    keras.layers.Dense(128, activation = 'relu'),
    keras.layers.Dense(10, activation = 'softmax')
])
# *********** COMPILE AND OPTIMIZE THE MODEL *************
# to do this we need to understand three things:
#1. Loss function: measures how accurate the output is (i.e. capture the differences between the real output and the actual output). Moreover, it is what is used to propagate back into the model to be able to update it (backprogation).
#2. Optimizer: measures how the model is updated (i.e. onto what extent the model is updated during backpropagation).
#3. Metrics: monitors the training to determine when to stop (trains the model until it becomes optimal i.e. predictions are good enough)

# compile the model: need to specify the optimizer, loss function and metrics
model.compile(
    optimizer = 'adam',
    loss = 'sparse_categorical_crossentropy',
    metrics = ['accuracy']
)
# ************ TRAINING THE MODEL **************
# train the model in 10 epochs (number of iterations); that is a complete presentation of the training dataset
# it means that we would feed in 60000 images and labels into the network (model we created) 10 different time (10 epochs)
# for each epoch we would see both the loss and the accuracy of the model

model.fit(train_images, train_labels, epochs = 10)
predictions = model.predict(test_images) # returns the learned label for each object in the array
predictions
predictions.shape # get 10000 predictions that was made each have 10 items of predictions
# a prediction is an array of 10 numbers: each number represents the confidence (probability) that the image (element of predictions) corresponds to each of the 10 different classes of clothing
# model is predicting a 98% probability/chance that the element is the last item from the clothing class e.g. ankle boot
predictions[0] # 10 different items for predictions[0] 
# let's see if the model made a good prediction i.e. if the first element (ankle boot) from predictions is the same element from the the test_images (ankle boot too?)
plt.figure()
plt.imshow(test_images[0])
plt.colorbar()
plt.show
# ************ EVALUATE THE PREDICTION RESULT (1) **************

# function to plot an image 
def plot_image(i, predictions_array, true_label, img):
  predictions_array, true_label, img = predictions_array, true_label[i], img[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])

  plt.imshow(img, cmap = plt.cm.binary) # black and white image

  predicted_label = np.argmax(predictions_array) # returns the index of the maximum value of an array from predictions_array e.g. returns the label (int associated to item from class of clothing)
  # test the accuracy of our model and put it visually
  if predicted_label == true_label:
    color = 'green'
  else:
    color = 'red'
  
  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                       100*np.max(predictions_array),
                                       class_names[true_label]),
             color=color)
# function to create bar plot of the predictions
def plot_value_array(i, predictions_array, true_label):
  predictions_array, true_label = predictions_array, true_label[i]

  plt.grid(False)
  plt.xticks(range(10))
  plt.yticks([])
  thisplot = plt.bar(range(10), predictions_array, color = '#777777')
  plt.ylim([0,1])
  predicted_label = np.argmax(predictions_array) # index of max probability

  thisplot[predicted_label].set_color('red') # predicted_label is int
  thisplot[true_label].set_color('green')
i = 8
plt.figure(figsize = (6, 3))
# plot two things: image and bar plot
plt.subplot(1,2,1) # figure 1 by 2 images with index 1

# plot the image with index i, image at predictions[i] 
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2) # figure 1 by 2 images with index at 2
plot_value_array(i, predictions[i], test_labels)
i = 2
plt.figure(figsize = (6, 3))
# plot two things: image and bar plot
plt.subplot(1,2,1) # figure 1 by 2 images with index 1

# plot the image with index i, image at predictions[i] 
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2) # figure 1 by 2 images with index at 2
plot_value_array(i, predictions[i], test_labels)
# *********** EVALUATE THE PREDICTIONS RESULT (2) *************

rows = 8 
cols = 4

num_images = rows * cols
plt.figure(figsize = (2*2*cols, 2*rows))

for i in range(num_images):
  plt.subplot(rows, 2*cols, 2*i+1)
  plot_image(i, predictions[i], test_labels, test_images)
  plt.subplot(rows, 2*cols, 2*i+2)
  plot_value_array(i, predictions[i], test_labels)

plt.tight_layout()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1094707520891365, 'fraction_numerical': 0.023537604456824512, 'mean_word_length': 4.540895061728395, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '139353', 'license': 'MIT', 'n_tokens_mistral': 2314, 'n_tokens_neox': 2169, 'n_words': 861, 'path': 'Fashion_MNIST.ipynb', 'repo_name': 'yuyunliu33/FashionMNIST', 'size': '7180'}