<jupyter_start><jupyter_text>Fashion MNIST dataset<jupyter_code>fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels),(test_images, test_labels) = fashion_mnist.load_data()
print("train images:",train_images.shape)
print("train labels:",len(train_labels))
print("train labels:",train_labels)

print("test images:",test_images.shape)
print("test labels:",test_labels.shape)
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']<jupyter_output><empty_output><jupyter_text>Preprocessing of the data<jupyter_code>plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.show()
### scaling of these dataset between 0 and 255 to fit NN

train_images = train_images / 255.0
test_images = test_images / 255.0
### display the scaled down image version

plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i],cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
plt.show()<jupyter_output><empty_output><jupyter_text>Building the Model Building the neural network requires configuring the layers of the model, then compiling the model.<jupyter_code>### 1. Setup the layers

model = keras.Sequential([
    keras.layers.Flatten(input_shape=[28,28]),  ## flatten the 28x28 pixels to 1-d vector 786 rows
    keras.layers.Dense(128, activation=tf.nn.relu),  ## These are densely-connected, or fully-connected, neural layers with 128 nodes or neurons
    keras.layers.Dense(10, activation=tf.nn.softmax) ## using softmax to classify 10 classes
])
### 2. compile the layers

model.compile(optimizer = 'adam',loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code>model.fit(train_images, train_labels, epochs=5)<jupyter_output>Epoch 1/5
60000/60000 [==============================] - 10s 171us/sample - loss: 0.4998 - acc: 0.8238
Epoch 2/5
60000/60000 [==============================] - 10s 171us/sample - loss: 0.3776 - acc: 0.8648
Epoch 3/5
60000/60000 [==============================] - 10s 173us/sample - loss: 0.3377 - acc: 0.8763
Epoch 4/5
60000/60000 [==============================] - 10s 173us/sample - loss: 0.3146 - acc: 0.8854
Epoch 5/5
60000/60000 [==============================] - 10s 173us/sample - loss: 0.2957 - acc: 0.8908<jupyter_text>Evaluate accuracy<jupyter_code>test_loss, test_acc = model.evaluate(test_images, test_labels)

print("Test Accuracy is: ",test_acc)
print("Test Loss is: ",test_loss)<jupyter_output>10000/10000 [==============================] - 0s 50us/sample - loss: 0.3458 - acc: 0.8737
Test Accuracy is:  0.8737
Test Loss is:  0.34579968147277834<jupyter_text>It turns out, the accuracy on the test dataset is a little less than the accuracy on the training dataset. This gap between training accuracy and test accuracy is an example of overfitting. Overfitting is when a machine learning model performs worse on new data than on their training data.  Making Predictions<jupyter_code>predictions = model.predict(test_images)
predictions[0]<jupyter_output><empty_output><jupyter_text>A prediction is an array of 10 numbers. These describe the "confidence" of the model that the image corresponds to each of the 10 different articles of clothing<jupyter_code>np.argmax(predictions[0])  ## to find the maximum confidence label
### Plotting and understanding the prediction across test data

def plot_image(i, predictions_array, true_label, img):
    predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
  
    plt.imshow(img, cmap=plt.cm.binary)
  
    predicted_label = np.argmax(predictions_array)
    if predicted_label == true_label:
        color = 'blue'
    else:
        color = 'red'
  
    plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)

def plot_value_array(i, predictions_array, true_label):
    predictions_array, true_label = predictions_array[i], true_label[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    thisplot = plt.bar(range(10), predictions_array, color="#777777")
    plt.ylim([0, 1])
    predicted_label = np.argmax(predictions_array)
  
    thisplot[predicted_label].set_color('red')
    thisplot[true_label].set_color('blue')
    
### plotting the first test data

i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)
plt.show()
i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)
plt.show()
# Plot the first X test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
    plt.subplot(num_rows, 2*num_cols, 2*i+1)
    plot_image(i, predictions, test_labels, test_images)
    plt.subplot(num_rows, 2*num_cols, 2*i+2)
    plot_value_array(i, predictions, test_labels)
plt.show()<jupyter_output><empty_output><jupyter_text>Predicting a single test image<jupyter_code># Grab an image from the test dataset
img = test_images[0]

print(img.shape)

# Add the image to a batch where it's the only member.
img = (np.expand_dims(img,0))

print(img.shape)
predictions_single = model.predict(img)

print(predictions_single)
plot_value_array(0, predictions_single, test_labels)
plt.xticks(range(10), class_names, rotation=45)
plt.show()
prediction_result = np.argmax(predictions_single[0])
print(prediction_result)<jupyter_output>9
{'fraction_non_alphanumeric': 0.14905691871140042, 'fraction_numerical': 0.04807210816224337, 'mean_word_length': 5.158273381294964, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '256082', 'license': 'Apache-2.0', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2023, 'n_words': 476, 'path': 'tf/Basic Classification.ipynb', 'repo_name': 'rishuatgithub/MLPy', 'size': '5991'}