<jupyter_start><jupyter_text><jupyter_code>!pip install keras-tcn
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import mnist
from keras.utils import to_categorical
from tcn import compiled_tcn
import gzip
import sys
import _pickle as cPickle
import numpy as np
def load_data(image_path,label_path, kind='train'):


    with gzip.open(label_path, 'rb') as lbpath:
        labels = np.frombuffer(lbpath.read(), dtype=np.uint8,
                               offset=8)

    with gzip.open(image_path, 'rb') as imgpath:
        images = np.frombuffer(imgpath.read(), dtype=np.uint8,
                               offset=16).reshape(len(labels), 784)

    return images, labels
from google.colab import drive
drive.mount("/content/gdrive/")
train_image = "gdrive/MyDrive/FashionMNIST/train-images.gz"
train_label = "gdrive/MyDrive/FashionMNIST/train-labels.gz"
test_image = "gdrive/MyDrive/FashionMNIST/test-images.gz"
test_label = "gdrive/MyDrive/FashionMNIST/test-labels.gz"
(train_images, train_labels) = load_data(train_image,train_label)
(test_images, test_labels) = load_data(test_image,test_label)
'''train_images = tf.reshape(train_images, shape=[-1, 28, 28])
test_images = tf.reshape(test_images, shape=[-1, 28, 28])'''
plt.imshow(train_images[0].reshape([28,28]))
# following command if getting SSL error while loading the dataset
#!open /Applications/Python\ 3.7/Install\ Certificates.command
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = tf.cast(train_images, tf.float32) / 255.0
test_images = tf.cast(test_images, tf.float32) / 255.0
for x in range(5):
    print(class_names[train_labels[x]])
    plt.imshow(tf.reshape(train_images[x],(28,28)),cmap=plt.cm.binary)
    plt.show()
train_data = train_images
test_data = test_images
model = compiled_tcn(return_sequences=False,
                         num_feat=1,
                         num_classes=10,
                         nb_filters=20,
                         kernel_size=6,
                         dilations=[2 ** i for i in range(9)],
                         nb_stacks=1,
                         max_len=train_data[0:1].shape[1],
                         use_skip_connections=True)
print(model.input.shape, train_images[0:1].shape)
print(f'x_train.shape = {train_images.shape}')
print(f'y_train.shape = {train_labels.shape}')
print(f'x_test.shape = {test_images.shape}')
print(f'y_test.shape = {test_labels.shape}')
model.summary()
history = model.fit(train_data, train_labels, epochs=10,validation_data=(test_data, test_labels))
def predict(model,image):

  accuracy = max(model.predict(tf.reshape(image,(1,784)))[0])*100
  pc = model.predict(tf.reshape(image,(1,784))).argmax(-1)[0]
  name = class_names[pc]

  print(pc,name," accuracy:", accuracy,"%")
  plt.imshow(tf.reshape(image,(28,28)),cmap=plt.cm.binary)
  plt.show()
for x in test_images[:30]:
  predict(model,x)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Val Loss')
plt.title('Training History')
plt.ylabel('Loss')
plt.xlabel('No. epoch')
plt.legend(loc="upper left")
plt.show()
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Val Accuracy')
plt.title('Training Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('No. epoch')
plt.legend(loc="upper left")
plt.show()
print("Model's training and validation accuracys are {0}% and {1}% respectively".format(history.history['accuracy'][-1]*100,history.history['val_accuracy'][-1]*100))
!jupyter nbconvert --to html main_ht.ipynb<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.149878476910613, 'fraction_numerical': 0.023764515257899, 'mean_word_length': 4.1804195804195805, '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': '243052', 'license': 'MIT', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1267, 'n_words': 231, 'path': 'main_ht.ipynb', 'repo_name': 'YugantM/FMNIST', 'size': '3703'}