<jupyter_start><jupyter_text>Explore the data<jupyter_code>train_images.shape
len(train_labels)
train_labels<jupyter_output><empty_output><jupyter_text>Preprocess the data<jupyter_code>plt.figure()
plt.imshow(train_images[1])
plt.colorbar()
plt.grid(False)
plt.show()
train_images = train_images / 255.0

test_images = test_images / 255.0
# build figure
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>Build the model A 28 x 28 image is transformed to a flattened array and feed into two densly connected neural netoworks(NNs) and finally ends up with 10 logits, each of which has a value from 0.0 to 1.0.<jupyter_code>model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28,28)),
    tf.keras.layers.Dense(128,activation='relu'),
    tf.keras.layers.Dense(10)
])
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Feed the model<jupyter_code>model.fit(train_images, train_labels, epochs=10)<jupyter_output>Epoch 1/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.3078 - accuracy: 0.8953
Epoch 2/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.2389 - accuracy: 0.9109
Epoch 3/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.2308 - accuracy: 0.9137
Epoch 4/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.2202 - accuracy: 0.9169
Epoch 5/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.2130 - accuracy: 0.9188
Epoch 6/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.2063 - accuracy: 0.9227
Epoch 7/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.2012 - accuracy: 0.9244
Epoch 8/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.1937 - accuracy: 0.9271
Epoch 9/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.1889 - accuracy: 0.9285
Epoch 10/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.182[...]<jupyter_text>Evaluate accuracy<jupyter_code>test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('\nTest accuracy', test_acc)<jupyter_output>313/313 - 1s - loss: 0.3778 - accuracy: 0.8824 - 862ms/epoch - 3ms/step

Test accuracy 0.8823999762535095<jupyter_text>Make prediction<jupyter_code>prob_model = tf.keras.Sequential([model,tf.keras.layers.Softmax()])
predictions = prob_model.predict(test_images)
predictions.shape

predictions[0]
np.argmax(predictions[0])
test_labels[0]<jupyter_output><empty_output><jupyter_text>Class prediction figures<jupyter_code>def plot_image(i, predictions_array, true_label, img):
  true_label, img = 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):
  true_label = 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)

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue')<jupyter_output><empty_output><jupyter_text>Verify predictions<jupyter_code>i =1
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i,predictions[i],test_labels,test_images)
plt.subplot(1,2,2)
plot_value_array(i,predictions[i],test_labels)
plt.show()
i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i,predictions[i],test_labels,test_images)
plt.subplot(1,2,2)
plot_value_array(i,predictions[i],test_labels)
plt.show()
# Plot the first X test images, their predicted labels, and the true labels.
# Color correct predictions in blue and 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):
  #subplot(nrows, ncols, index, **kwargs)
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  plot_image(i, predictions[i], test_labels, test_images)
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  plot_value_array(i, predictions[i], test_labels)
plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Use the trained model<jupyter_code># Grab an image from the test dataset.
img = test_images[1]

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 = prob_model.predict(img)

print(predictions_single)
plot_value_array(1, predictions_single[0], test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)
plt.show()
np.argmax(predictions_single[0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19004353586977096, 'fraction_numerical': 0.06852167329169033, 'mean_word_length': 5.563975155279503, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '410719', 'license': 'Apache-2.0', 'n_tokens_mistral': 2220, 'n_tokens_neox': 1961, 'n_words': 351, 'path': '01_ML_basics_with_Keras/01 basic image classification.ipynb', 'repo_name': 'snippetfactory/TF_tutorials', 'size': '5283'}