<jupyter_start><jupyter_text><jupyter_code>import torch
from torchvision import datasets,transforms
train_data = datasets.MNIST('/content/drive/MyDrive/MNIST',download=True,train=True,transform=transforms.ToTensor())
test_data = datasets.MNIST('/content/drive/MyDrive/MNIST',download=True,train=False,transform=transforms.ToTensor())
from torch.utils.data import DataLoader,random_split
train_ds, val_ds = random_split(train_data,[50000,10000])
batch_size = 64
train_dl = DataLoader(train_ds,batch_size=batch_size,shuffle=True)
val_dl = DataLoader(val_ds,batch_size=batch_size)<jupyter_output><empty_output><jupyter_text>Define Device to switch between CPU to GPU<jupyter_code>def get_default_device():
  if torch.cuda.is_available():
    return torch.device('cuda')
  return torch.device('cpu')
get_default_device()
def to_device(data,device):
  if isinstance(data, (list,tuple)):
    return [to_device(x, device) for x in data]
  return data.to(device)
class DeviceDataLoader():
  def __init__(self,dl,device):
    self.dl = dl
    self.device = device

  def __iter__(self):
    for xb in self.dl:
      yield to_device(xb,self.device)
    
  def __len__(self):
    return len(self.dl)<jupyter_output><empty_output><jupyter_text>Define DataLoader<jupyter_code>train_dl = DeviceDataLoader(train_dl,get_default_device())
val_dl = DeviceDataLoader(val_dl,get_default_device())<jupyter_output><empty_output><jupyter_text>Define Linear Model for MNIST<jupyter_code>import torch.nn as NN
input_size = 784 # 28 * 28 * 1 - Image size of MNIST
num_of_classes = 10
class MNISTLinear(NN.Module):
  def __init__(self):
    super().__init__()
    self.linear= NN.Linear(input_size,num_of_classes)
    # self.linear2 = NN.Linear(32,num_of_classes)
  
  def forward(self,xb):
    xb = xb.view(xb.size(0),-1)
    return self.linear(xb)
model_test = to_device(MNISTLinear(),get_default_device())
import torch.nn.functional as F
for x,y in train_dl:
  preds = model_test(x)
  probs =F.softmax(preds,dim=1)
  _,pred_label = torch.max(probs,dim=1)
  print(pred_label)
  print(y)
  break;<jupyter_output>tensor([7, 5, 3, 1, 6, 3, 3, 7, 3, 3, 0, 3, 3, 0, 0, 3, 3, 3, 0, 3, 0, 3, 7, 3,
        0, 7, 0, 0, 7, 9, 3, 3, 3, 0, 6, 3, 7, 3, 7, 6, 3, 3, 7, 7, 3, 3, 3, 3,
        0, 3, 7, 7, 3, 3, 0, 0, 7, 7, 0, 7, 3, 3, 3, 0], device='cuda:0')
tensor([1, 1, 8, 7, 9, 2, 3, 1, 6, 0, 4, 6, 0, 8, 3, 6, 6, 5, 6, 6, 2, 6, 1, 0,
        8, 6, 3, 5, 1, 3, 5, 2, 6, 2, 3, 3, 9, 6, 1, 4, 5, 0, 4, 1, 3, 6, 9, 6,
        8, 5, 9, 1, 4, 5, 3, 4, 5, 1, 4, 7, 8, 2, 5, 3], device='cuda:0')<jupyter_text>Define Accuracy<jupyter_code>def accuracy(preds,label):
  _,pred_label = torch.max(preds,dim=1)
  return torch.tensor(torch.sum(pred_label == label).item()/len(label))
accuracy(preds,y)
loss_fn = F.cross_entropy
import torch.optim as Optim
def fit(epochs,lr,train_dl,val_dl,model,loss_fn,optim_fn=Optim.SGD):
  optimizer = optim_fn(model.parameters(),lr)

  for epoch in range(epochs):
    train_loss = []
    train_acc = []
    for xb,y in train_dl:
      preds = model(xb)
      loss = loss_fn(preds,y)
      train_loss.append(loss)
      acc = accuracy(preds,y)
      train_acc.append(acc)
      loss.backward()
      optimizer.step()
      optimizer.zero_grad()
    avg_loss = torch.stack(train_loss).mean()
    avg_acc = torch.stack(train_acc).mean()
    print("Epoch [{}], train_loss: {:.4f}, train_acc: {:.4f}".format(epoch, avg_loss,avg_acc))
mnist_model = to_device(MNISTLinear(),device=get_default_device())
fit(10,lr=0.001,train_dl=train_dl,val_dl=val_dl,model=mnist_model,loss_fn=loss_fn)
class MNISTLinear2(NN.Module):
  def __init__(self):
    super().__init__()
    self.linear= NN.Linear(input_size,32)
    self.linear2 = NN.Linear(32,num_of_classes)
  
  def forward(self,xb):
    xb = xb.view(xb.size(0),-1)
    out = self.linear(xb)
    out = F.relu(out)
    out = self.linear2(out)
    return out
mnist_model = to_device(MNISTLinear2(),device=get_default_device())
fit(10,lr=0.01,train_dl=train_dl,val_dl=val_dl,model=mnist_model,loss_fn=loss_fn)<jupyter_output>Epoch [0], train_loss: 1.4147, train_acc: 0.6702
Epoch [1], train_loss: 0.5767, train_acc: 0.8600
Epoch [2], train_loss: 0.4353, train_acc: 0.8844
Epoch [3], train_loss: 0.3845, train_acc: 0.8947
Epoch [4], train_loss: 0.3571, train_acc: 0.9002
Epoch [5], train_loss: 0.3394, train_acc: 0.9048
Epoch [6], train_loss: 0.3265, train_acc: 0.9076
Epoch [7], train_loss: 0.3159, train_acc: 0.9109
Epoch [8], train_loss: 0.3071, train_acc: 0.9128
Epoch [9], train_loss: 0.2991, train_acc: 0.9147<jupyter_text>Test against Validation Set<jupyter_code>def evalute(model,)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15359758724687633, 'fraction_numerical': 0.06311934510986644, 'mean_word_length': 4.990967741935484, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '410761', 'license': 'Apache-2.0', 'n_tokens_mistral': 2137, 'n_tokens_neox': 1892, 'n_words': 396, 'path': 'MNIST_GPU.ipynb', 'repo_name': 'satish860/see_food', 'size': '4642'}