<jupyter_start><jupyter_text>https://colab.research.google.com/drive/1m2cg3D1x3j5vrFc-Cu0gMvc48gWyCOuGforceEdit=true&sandboxMode=true&scrollTo=a_bM6nQ-PZBY<jupyter_code>%tensorflow_version 2.x # this line is not required unless you are in a notebook
# tensorflow and tf.keras
import tensorflow as tf
from tensorflow import keras

# helper libraies
import numpy as np
import matplotlib.pyplot as plt
fashion_mnist = keras.datasets.fashion_mnist # load dataset
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() # split into test and training
train_images.shape
train_images[0, 23, 23] # let's have a look at one pixel
train_labels[:10] #let's have a look at the first 10 training labels
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
plt.figure()
plt.imshow(train_images[420])
plt.colorbar()
plt.grid(False)
plt.show()
print(train_labels[420])
print('It is a/an', class_names[train_labels[420]] + '.')
train_images = train_images / 255.0
test_images = test_images / 255.0
model = keras.Sequential([
                          keras.layers.Flatten(input_shape=(28, 28)), # input layer (1)
                          keras.layers.Dense(128, activation='relu'), # hidden layer (2)
                          keras.layers.Dense(10, activation='softmax') # output layer (3)
                          ])
model.compile(
    optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy']
)
model.fit(train_images, train_labels, epochs=10) # we pass the data, labels and epochs and watch the magic!
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=1)

print('Test accuracy: ', test_acc)
prediction = model.predict(test_images)
print(class_names[np.argmax(prediction[0])])
plt.figure()
plt.imshow(test_images[0])
plt.colorbar()
plt.grid(False)
plt.show()

print(test_labels[0])
print('It is a/an', class_names[test_labels[0]] + '.')
test_images.shape
prediction[0]
np.argmax(prediction[0])
test_labels[0]
COLOR = 'white'
plt.rcParams['text.color'] = COLOR
plt.rcParams['axes.labelcolor'] = COLOR

def predict(model, image, correct_label):
  class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
  prediction = model.predict(np.array([image]))
  predicted_class = class_names[np.argmax(prediction)]

  show_image(image, class_names[correct_label], predicted_class)

def show_image(img, label, guess):
  plt.figure()
  plt.imshow(img, cmap=plt.cm.binary)
  plt.title("Excpected: " + label)
  plt.xlabel("Guess: " + guess)
  plt.colorbar()
  plt.grid(False)
  plt.show()
  print('Excpected: ' + label, '|| Guess: ' + guess)

def get_number():
  while True:
    num = input("Pick a number: ")
    if num.isdigit():
      num = int(num)
      if 0 <= num <= 1000:
        return int(num)
    else:
      print("Try again...")
      
num = get_number()
image = test_images[num]
label = test_labels[num]
predict(model, image, label)<jupyter_output>Pick a number: 45
{'fraction_non_alphanumeric': 0.14387326220497898, 'fraction_numerical': 0.02133850630455868, 'mean_word_length': 4.316151202749141, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '71505', 'license': 'Apache-2.0', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1089, 'n_words': 240, 'path': 'Neural_Networks.ipynb', 'repo_name': 'lostapple9/Machine-Learning-1', 'size': '3093'}