<jupyter_start><jupyter_text>Importing necessary modules<jupyter_code>import numpy as np
import pandas as pd
from torch.utils.data import Dataset, DataLoader
from torch import nn
from torchvision import transforms
import matplotlib.pyplot as plt
from torch.autograd import Variable
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
from sklearn.metrics import confusion_matrix
import torch
from torch.utils.data import Dataset, DataLoader<jupyter_output><empty_output><jupyter_text>Extracting FashionMNIST dataset<jupyter_code>train_data=torchvision.datasets.FashionMNIST("./data",download=True,transform=torchvision.transforms.Compose([transforms.ToTensor()]))
train_data
test_data=torchvision.datasets.FashionMNIST("/.data",download=True,train=False,transform=torchvision.transforms.Compose([transforms.ToTensor()]))
train_data<jupyter_output><empty_output><jupyter_text><jupyter_code>num_epochs = 5
count = 0
# Lists for visualization of loss and accuracy 
loss_list = []
iteration_list = []
accuracy_list = []
predictions_list = []
labels_list = []
# Loading our data
train_loader = torch.utils.data.DataLoader(train_data, 
                                           batch_size=100)
test_loader = torch.utils.data.DataLoader(test_data,
                                          batch_size=100)
train_loader<jupyter_output><empty_output><jupyter_text>We have 10 types of clothes in FashionMNIST dataset.Making a method that return the name of class for the label number. ex. if the label is 5, we return Sandal.<jupyter_code>def output_label(label):
  output_mapping = {
      0: "T-shirt/Top",
      1: "Trouser",
      2: "Pullover",
      3: "Dress",
      4: "Coat", 
      5: "Sandal", 
      6: "Shirt",
      7: "Sneaker",
      8: "Bag",
      9: "Ankle Boot"}
  input = (label.item() if type(label) == torch.Tensor else label)
  return output_mapping[input]<jupyter_output><empty_output><jupyter_text>Visualising some images<jupyter_code>a = next(iter(train_loader))
a[0].size()
image, label = next(iter(train_data))
plt.imshow(image.squeeze(), cmap="gray")
print(label)<jupyter_output>9<jupyter_text>Defining out basic neural network of two linear layers<jupyter_code>class FashionNN(nn.Module):
    def __init__(self):
      super(FashionNN, self).__init__()
      input_dim, hidden1, output_dim = 784, 100, 10
      self.fc1 = nn.Linear(input_dim, hidden1) 
      self.fc2 = nn.Linear(hidden1, output_dim)
        

    def forward(self, x):
      x = self.fc1(x)
      x = self.fc2(x)
      x=x.view(x.size(0),-1)
      return x<jupyter_output><empty_output><jupyter_text>Using CrossEntropyLoss() Using Adam optimizer for optimization<jupyter_code>model = FashionNN()

error = nn.CrossEntropyLoss()

learning_rate = 0.001
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
print(model)
model.to("cpu")<jupyter_output><empty_output><jupyter_text>Training and testing our model with the dataset we genereated<jupyter_code>for epoch in range(num_epochs):
  for images, labels in train_loader:
    images, labels = images.to("cpu"), labels.to("cpu")
    train = images.reshape(images.size(0),-1)
    labels = Variable(labels)

    # Forward pass
    outputs = model(train)
    loss = error(outputs, labels)
    # Initializing a gradient as 0 so there is no mixing of gradient among the batches
    optimizer.zero_grad()

    #Propagating the error backward
    loss.backward()
    # Optimizing the parameters
    optimizer.step()
    
    count += 1
    
# Testing the model
    
    if not (count % 50):
       total = 0
       correct = 0
       for images, labels in test_loader:
         images, labels = images.to("cpu"), labels.to("cpu")
         labels_list.append(labels)
         test = images.reshape(images.size(0),-1)
         
         outputs = model(test)
            
         predictions = torch.max(outputs, 1)[1].to("cpu")
         predictions_list.append(predictions)
         correct += (predictions == labels).sum()
            
         total += len(labels)
            
         accuracy = correct * 100 / total
         loss_list.append(loss.data)
         iteration_list.append(count)
         accuracy_list.append(accuracy)
        
    if not (count % 500):
      print("Iteration: {}, Loss: {}, Accuracy: {}%".format(count, loss.data, accuracy))<jupyter_output>/pytorch/aten/src/ATen/native/BinaryOps.cpp:81: UserWarning: Integer division of tensors using div or / is deprecated, and in a future release div will perform true division as in Python 3. Use true_divide or floor_divide (// in Python) instead.<jupyter_text>Printing the confusion matrix<jupyter_code>from itertools import chain 

predictions_l = [predictions_list[i].tolist() for i in range(len(predictions_list))]
labels_l = [labels_list[i].tolist() for i in range(len(labels_list))]
predictions_l = list(chain.from_iterable(predictions_l))
labels_l = list(chain.from_iterable(labels_l))
import sklearn.metrics as metrics

confusion_matrix(labels_l, predictions_l)
print("Classification report for CNN :\n%s\n"
      % (metrics.classification_report(labels_l, predictions_l)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1088841780689921, 'fraction_numerical': 0.012526498361919445, 'mean_word_length': 3.9287749287749287, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '460618', 'license': 'MIT', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1642, 'n_words': 381, 'path': 'Basic_neural_network_on_Fashion_MNIST_using_pytorch.ipynb', 'repo_name': 'MayankG514/Fashion_MNIST_classifier', 'size': '5189'}