<jupyter_start><jupyter_text>Installing a library called tensor flow dataset<jupyter_code>pip install -U tensorflow_datasets
from __future__ import absolute_import, division, print_function

import logging
logging.getLogger("tensorflow").setLevel(logging.ERROR)
import tensorflow as tf
import tensorflow_datasets as tfds
# tf.compat.v1.logging.set_verbrosity(tf.compat.v1.logging.ERROR)

#Helper libraries
import math
import numpy as np
import matplotlib.pyplot as plt

#improve progress bar display
import tqdm
import tqdm.auto
tqdm.tqdm = tqdm.auto.tqdm

print(tf.__version__)
#future

# tf.enable_eager_excecution()
#Loading it using tensor flow datasets
dataset,metadata = tfds.load('fashion_mnist',as_supervised=True, with_info=True)
#Split the dataset into train and test dataset
train_dataset , test_dataset = dataset['train'], dataset['test']
#Human readable script that will map the numeric values to human readable script
class_names = ['T-shirt/top','Trouser','Pullover','Dress','Coat',
               'Sandal','Shirt','Sneaker','Bag','Ankle boot']
#Explore our data
num_train_examples = metadata.splits['train'].num_examples
num_test_examples = metadata.splits['test'].num_examples

print("Number of training examples:{}".format(num_train_examples))
print("Number of testing examples:{}".format(num_test_examples))


#Preprocess the data flattening the data to 1D ie backpropergation
#Note that the value of each image is a pixel value ranging from 0-255
#For our model to work properly we need to place the values to range between [0-1]
def normalize (images,labels):
  images = tf.cast(images, tf.float32)
  images /= 255
  return images,labels
#We then use the map function to equate the above with our training and test data
train_dataset = train_dataset.map(normalize)
test_dataset = test_dataset.map(normalize)
#Lets try to plot an image and see what it looks like
#We first take a single image and remove the color  by reshaping
for image, label in test_dataset.take(1):
  break
image = image.numpy().reshape((28,28))
#plot the shape
plt.figure()
plt.imshow(image, cmap=plt.cm.binary)
plt.colorbar()
plt.grid(False)
plt.show()
from IPython.core.pylabtools import figsize
#How about the first 
plt.figure(figsize(10,10))
i = 0
for image, label in test_dataset.take(25):
  image = image.numpy().reshape((28,28))
  #plot the shape
  plt.subplot(5,5,i+1)
  plt.xticks([])
  plt.yticks([])
  plt.grid(False)
  plt.imshow(image, cmap=plt.cm.binary)
  plt.xlabel(class_names[label])
  i+= 1
plt.show()
#Here is the fun part....Lets build our model
#Back propergation ...we flatten our model to 1D with 784 elements
model = tf.keras.Sequential([
     tf.keras.layers.Flatten(input_shape = (28,28,1)),
     #We apply a dense layer with 128 neurons
     tf.keras.layers.Dense(128, activation=tf.nn.relu),
     #The output is a  dense layer with 10 outputs
     tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
#Now we  compile our model and perform some optimization ...this is the gradient descend process
model.compile(optimizer='adam',
              loss = 'sparse_categorical_crossentropy',
              metrics = ['accuracy'])
BATCH_SIZE = 32
train_dataset = train_dataset.repeat().shuffle(num_train_examples).batch(BATCH_SIZE)
test_dataset = test_dataset.batch(BATCH_SIZE)
#Finally we now train our model combining all of the above
model.fit(train_dataset,epochs=5,steps_per_epoch=math.ceil(num_train_examples/BATCH_SIZE))
#Now lets test it
test_loss,test_accuracy = model.evaluate(test_dataset, steps=math.ceil(num_test_examples/32))
print('Accuracy on test dataset:',test_accuracy)
#Lets now make predictions
for test_images, test_labels in test_dataset.take(1):
  test_images = test_images.numpy()
  test_labels = test_labels.numpy()
  predictions = model.predict(test_images)
predictions.shape
#Lets see what our model predicted for the first image
predictions[0]
#Lets print the largest index
np.argmax(predictions[0])
#Confirm with the coresponding label
test_labels[0] 
#Lets plot the output
def plot_image (i,prediction_array,true_label,images):
  prediction_array,true_label,img = prediction_array[i],true_label[i],images[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])

  plt.imshow(img[...,0],cmap=plt.cm.binary)

  predicted_label = np.argmax[prediction_array]
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'
  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(prediction_array),
                                class_names[true_label]),
                                color = color)
  def plot_value_array(i,predictions_array,true_labels):
    prediction_array,true_labels = predictions_array[i],true_labels[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    thinplot = plt.bar(range(10), prediction_array,color='purple')
    plt.ylim([0,1])
    predicted_label = np.argmax[prediction_array]

    thinplot[predicted_label].set_color('red')
    thinplot[true_labels].set_color('blue')


i = 0
plt.figure(figsize=(6,2))
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)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11302352266207688, 'fraction_numerical': 0.01549053356282272, 'mean_word_length': 4.977142857142857, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '453044', 'license': 'MIT', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1716, 'n_words': 432, 'path': 'FashionDataset.ipynb', 'repo_name': 'Marriane791/Authentication-and-authorisation', 'size': '5229'}