<jupyter_start><jupyter_text>Will be training an ANN on `mnist` datsset<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
## Imports
import numpy as np, pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
import torch
import torch.nn as nn ##neural network module
import torch.nn.functional as F

from torch.utils.data import DataLoader
from torchvision import datasets, transforms

import time

## Libraries have been imported.
## let's create a transform var

t = transforms.ToTensor()
## load the training and testing data
path= 'content/drive/Mydrive/ANN MNIST/'
train_data = datasets.MNIST(root=path, train=True, download=True, transform=t)
test_data = datasets.MNIST(root= path, train=False, download=True, transform =t)
train_data
test_data
ex_image, ex_lab = train_data[0]
#plt.imshow(ex_image.reshape(28,28))
##plt.imshow(ex_image[0])
plt.imshow(torch.squeeze(ex_image), cmap='gist_yarg' ) ##default cmap 'viridis', 'gray can also be used'
plt.axis('off')
plt.show()

## all  these 3 options printing out the results
## loading in batches
torch.manual_seed(21)
train_loader = DataLoader(train_data, batch_size=100, shuffle=True)
test_loader = DataLoader(test_data, batch_size=500, shuffle=False)
for img, lab in train_loader:
  print(img.size())
  print(lab)
  break
from torchvision.utils import make_grid

print(*lab[:8].numpy(), sep='       ')
im_set = make_grid(img[:8])
plt.figure(figsize=(8,10))
plt.imshow(np.transpose(im_set.numpy(),(1,2,0)))
plt.axis('off')
plt.show()
q = torch.tensor([1,2,3])
q.numpy()
## Let's set the ann

class myANN(nn.Module):
  def __init__(self, inp_size, out_size, hidden=[120,84,42]):
    super().__init__()

    self.fully_con1 = nn.Linear(inp_size, hidden[0])
    self.fully_con2 = nn.Linear(hidden[0], hidden[1])
    self.fully_con3 = nn.Linear(hidden[1], hidden[2])
    self.fully_con4 = nn.Linear(hidden[2], out_size)

  
  def forward(self, data):
    data = F.relu(self.fully_con1(data))
    data = F.sigmoid(self.fully_con2(data))
    data = F.relu(self.fully_con3(data))
    data = self.fully_con4(data)

    return F.log_softmax(data, dim=1) ##why dim=1, not dim=0? dim=0 will give number in batches. dim=1 gives num of samples in a batch

model = myANN(784, 10)
model
tot=1
for param in model.parameters():
  print(param.numel())
  tot *= param.numel()

print(f'Total number of paramters: {tot}')
## Let's set our criterions
lr = 0.001
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr = lr)<jupyter_output><empty_output><jupyter_text>Training and evaluating our model<jupyter_code>start = time.time()

num_epoch = 16

trainLoss, trainCorr, testLoss, testCorr = list(), list(), list(), list()

for i in range(num_epoch):
  trn_corr = 0
  tst_corr = 0

  for b_no, (Xtrain, Ytrain) in enumerate(train_loader):
    
    b_no += 1
    Xtrain_flat = Xtrain.view(100,-1) ##100 -->batch size for train
    y_pred = model(Xtrain_flat) ##100 predictions returned

    loss = criterion(y_pred, Ytrain)

    ##  y_pred will be a 10  valued variable with probabilities for different classes

    predicted = torch.max(y_pred.data, 1)[1]
    batch_corr = (predicted==Ytrain).sum()
    trn_corr += batch_corr

    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    if b_no % 200 ==0:
      accuracy = trn_corr.item()/b_no
      print('EPOCH {} Batch {} LOSS {} Accuracy: {}'.format(i, b_no, loss.item(), accuracy))

    
  trainLoss.append(loss.item())
  trainCorr.append(trn_corr.item())

  with torch.no_grad():
    ##no_grad ensures weights and biases are not updated
    for b_no, (Xtest, Ytest) in enumerate(test_loader):
      b_no+=1
      y_val = model(Xtest.view(500,-1)) ##batch size in test is 500
      predicted = torch.max(y_val.data, 1)[1]

      tst_corr += (predicted==Ytest).sum()
  
  loss = criterion(y_val, Ytest)
  testLoss.append(loss.item())
  testCorr.append(tst_corr.item())



end = time.time()

print("Total time in training:", (end-start)/60, 'minutes')
plt.plot(trainLoss, label='Training Loss')
plt.plot(testLoss, label = 'Test Loss')
plt.legend()
trainacc  = [t/600 for t in trainCorr]
testacc  = [t/100 for t in testCorr]
plt.plot(trainacc, label='Training correct')
plt.plot(testacc, label = 'Test Correct')
plt.legend()
##testing on whole test data
test_load_all = DataLoader(test_data, batch_size=len(test_data), shuffle = True)

with torch.no_grad():
  correct = 0
  for xtest, ytest in test_load_all:
    y_val = model(xtest.view(len(xtest),-1))
    pred = torch.max(y_val,1)[1]
    correct += (pred == ytest).sum()

acc = correct.item()/len(test_data)
acc*100

##Accuracy on unseen test<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1400679117147708, 'fraction_numerical': 0.0233446519524618, 'mean_word_length': 4.6375598086124405, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '152430', 'license': 'MIT', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1690, 'n_words': 419, 'path': 'ANN_with_Mnist.ipynb', 'repo_name': 'ShashwatVv/naiveDL', 'size': '4712'}