<jupyter_start><jupyter_text>Steps :1. Load Dataset2. MakeData set iterable3. Create a Model class4. Instantiate a Model Class5. Instantiate a Loss class6. Instantiate a Optimizer Class7. Train Model<jupyter_code># Step 1 :  Load the MNIST Train Dataset

import torch
import torch.nn as nn
from torch.autograd import Variable
import torchvision.datasets as dsets
import torchvision.transforms as transforms
train_dataset = dsets.MNIST(root="./data",
                            train=True,
                            transform=transforms.ToTensor(),
                            download=True
                            )

test_dataset = dsets.MNIST(root="./data",
                           train=False,
                           transform=transforms.ToTensor())
#Step 2 :  Make dataset Iterable 
batch_size=100
n_iters = 3000
n_epochs = n_iters / (len(train_dataset)/batch_size)
n_epochs = int(n_epochs)
train_dataloader = torch.utils.data.DataLoader(dataset=train_dataset,
                                               batch_size=batch_size,
                                               shuffle=True)

test_dataloader = torch.utils.data.DataLoader(dataset=test_dataset,
                                              batch_size=batch_size,
                                              shuffle=False)
# Step 3 : Create a Model Class 

class FeedforwardNNModelSIG(nn.Module):
  def __init__(self,input_dim,hidden_dim,output_dim):
      super().__init__()
      
      #Linear Layar
      self.fc1=nn.Linear(input_dim,hidden_dim)

      #Non Linear Layaer 
      self.sigmoid=nn.Sigmoid()

      #Linear Layar(readout layer)
      self.fc2=nn.Linear(hidden_dim,output_dim)

  def forward(self,x):
      # Linear
      out = self.fc1(x)
      # Non-Linear
      out = self.sigmoid(out)
      # Linear (readout layer)
      out = self.fc2(out)

      return out

# Step 4 : Intantiate Model Class

input_dim=28*28
hidden_dim=50
output_dim=10
model = FeedforwardNNModelSIG(input_dim,hidden_dim,output_dim)
# Step 5 : Intantiate loss class
criterion = nn.CrossEntropyLoss()
# Step 6 : Intantiate optimizer
learning_rate = 0.1
optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate)
# Step 7 : Train the Model
##1 Convert inputs/labels to Variables
##2 Clear the gradients 
##3 Get output given input
##4 Get Loss
##5 Get gradients wrt parameters
##6 Update paramters using Gradients
##7 Repeat

iter=0
for epoch in range(n_epochs):
  for i,(images, labels) in enumerate(train_dataloader):

      ##1 Convert inputs/labels to Variable
      images = Variable(images.view(-1,28*28))
      labels = Variable(labels)

      ##2 Clear the gradients 
      optimizer.zero_grad()

      ##3 Get output given input
      outputs = model(images)

      ##4 Get Loss
      loss = criterion(outputs,labels)

      ##5 Get gradients wrt parameters
      loss.backward()
    
      ##6 Update paramters using Gradients
      optimizer.step()

      ##7 Repeat

      iter +=1
      if iter % 500 == 0 :
        correct = 0.
        total = 0.
      # Iter thorough test dataset
        for images,labels in test_dataloader:
          images = Variable(images.view(-1,28*28))
          labels = Variable(labels)

          outputs = model(images)
          # Get the max value of the prediction
          _,predicted=torch.max(outputs.data,1)
          # number of labels 
          total += labels.size(0)
          # Total correct predictions
          correct += (predicted==labels).sum()

        accuracy = 100*(correct/total)
        print("Iteration: {} , Loss:{} , Accuracy:{}".format(iter,loss.data,accuracy))
def print_graph(g, level=0):
    if g == None: return
    print('#'*level*1, g)
    for subg in g.next_functions:
        #print(subg)
        print_graph(subg[0], level+1)

print_graph(loss.grad_fn, 1)
# Step 3 : Create a Model Class 

class FeedforwardNNModelTANH(nn.Module):
  def __init__(self,input_dim,hidden_dim,output_dim):
      super().__init__()
      
      #Linear Layar
      self.fc1=nn.Linear(input_dim,hidden_dim)

      #Non Linear Layaer 
      self.tanh=nn.Tanh()

      #Linear Layar(readout layer)
      self.fc2=nn.Linear(hidden_dim,output_dim)

  def forward(self,x):
      # Linear
      out = self.fc1(x)
      # Non-Linear
      out = self.tanh(out)
      # Linear (readout layer)
      out = self.fc2(out)

      return out
# Step 4 : Intantiate Model Class

input_dim=28*28
hidden_dim=100
output_dim=10
model = FeedforwardNNModelTANH(input_dim,hidden_dim,output_dim)
# Step 5 : Intantiate loss class
criterion = nn.CrossEntropyLoss()
# Step 6 : Intantiate optimizer
learning_rate = 0.1
optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate)
# Step 7 : Train the Model
##1 Convert inputs/labels to Variables
##2 Clear the gradients 
##3 Get output given input
##4 Get Loss
##5 Get gradients wrt parameters
##6 Update paramters using Gradients
##7 Repeat

iter=0
for epoch in range(n_epochs):
  for i,(images, labels) in enumerate(train_dataloader):

      ##1 Convert inputs/labels to Variable
      images = Variable(images.view(-1,28*28))
      labels = Variable(labels)

      ##2 Clear the gradients 
      optimizer.zero_grad()

      ##3 Get output given input
      outputs = model(images)

      ##4 Get Loss
      loss = criterion(outputs,labels)

      ##5 Get gradients wrt parameters
      loss.backward()
    
      ##6 Update paramters using Gradients
      optimizer.step()

      ##7 Repeat

      iter +=1
      if iter % 500 == 0 :
        correct = 0.
        total = 0.
      # Iter thorough test dataset
        for images,labels in test_dataloader:
          images = Variable(images.view(-1,28*28))
          labels = Variable(labels)

          outputs = model(images)
          # Get the max value of the prediction
          _,predicted=torch.max(outputs.data,1)
          # number of labels 
          total += labels.size(0)
          # Total correct predictions
          correct += (predicted==labels).sum()

        accuracy = 100*(correct/total)
        print("Iteration: {} , Loss:{} , Accuracy:{}".format(iter,loss.data,accuracy))
print_graph(loss.grad_fn, 1)
# Step 3 : Create a Model Class 

class FeedforwardNNModelRelu(nn.Module):
  def __init__(self,input_dim,hidden_dim,output_dim):
      super().__init__()
      
      #Linear Layar
      self.fc1=nn.Linear(input_dim,hidden_dim)

      #Non Linear Layaer 
      self.relu=nn.ReLU()

      #Linear Layar(readout layer)
      self.fc2=nn.Linear(hidden_dim,output_dim)

  def forward(self,x):
      # Linear
      out = self.fc1(x)
      # Non-Linear
      out = self.relu(out)
      # Linear (readout layer)
      out = self.fc2(out)

      return out
# Step 4 : Intantiate Model Class

input_dim=28*28
hidden_dim=100
output_dim=10
model = FeedforwardNNModelRelu(input_dim,hidden_dim,output_dim)
# Step 5 : Intantiate loss class
criterion = nn.CrossEntropyLoss()
# Step 6 : Intantiate optimizer
learning_rate = 0.1
optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate)
# Step 7 : Train the Model
##1 Convert inputs/labels to Variables
##2 Clear the gradients 
##3 Get output given input
##4 Get Loss
##5 Get gradients wrt parameters
##6 Update paramters using Gradients
##7 Repeat

iter=0
for epoch in range(n_epochs):
  for i,(images, labels) in enumerate(train_dataloader):

      ##1 Convert inputs/labels to Variable
      images = Variable(images.view(-1,28*28))
      labels = Variable(labels)

      ##2 Clear the gradients 
      optimizer.zero_grad()

      ##3 Get output given input
      outputs = model(images)

      ##4 Get Loss
      loss = criterion(outputs,labels)

      ##5 Get gradients wrt parameters
      loss.backward()
    
      ##6 Update paramters using Gradients
      optimizer.step()

      ##7 Repeat

      iter +=1
      if iter % 500 == 0 :
        correct = 0.
        total = 0.
      # Iter thorough test dataset
        for images,labels in test_dataloader:
          images = Variable(images.view(-1,28*28))
          labels = Variable(labels)

          outputs = model(images)
          # Get the max value of the prediction
          _,predicted=torch.max(outputs.data,1)
          # number of labels 
          total += labels.size(0)
          # Total correct predictions
          correct += (predicted==labels).sum()

        accuracy = 100*(correct/total)
        print("Iteration: {} , Loss:{} , Accuracy:{}".format(iter,loss.data,accuracy))
# Step 3 : Create a Model Class multiple hidden

class FeedforwardNNModelMRelu(nn.Module):
  def __init__(self,input_dim,hidden_dim,output_dim):
      super().__init__()
      
      #Linear Layer
      self.fc1=nn.Linear(input_dim,hidden_dim)

      #Non Linear Layer 
      self.relu1=nn.ReLU()

      #Linear Layer(readout layer)
      self.fc2=nn.Linear(hidden_dim,hidden_dim)

      #Non Linear Layer 
      self.relu2=nn.ReLU()

      #Linear Layer(readout layer)
      self.fc3=nn.Linear(hidden_dim,hidden_dim)

      #Non Linear Layer 
      self.relu3=nn.ReLU()

      #Linear Layer
      self.fc4=nn.Linear(hidden_dim,hidden_dim)

      #Non Linear Layer 
      self.relu4=nn.ReLU()

      #Linear Layer(readout layer)
      self.fc5=nn.Linear(hidden_dim,hidden_dim)

      #Non Linear Layer 
      self.relu5=nn.ReLU()

      #Linear Layer(readout layer)
      self.fc6=nn.Linear(hidden_dim,output_dim)




  def forward(self,x):

      out = self.fc1(x)
      out = self.relu1(out)
      
      out = self.fc2(out)
      out = self.relu2(out)
      
      out = self.fc3(out)
      out = self.relu3(out)

      out = self.fc4(out)
      out = self.relu4(out)

      out = self.fc5(out)
      out = self.relu5(out)
      
      # Linear(readout layer)
      out = self.fc6(out)

      return out
# Step 4 : Intantiate Model Class

input_dim=28*28
hidden_dim=1000
output_dim=10
model = FeedforwardNNModelMRelu(input_dim,hidden_dim,output_dim)
# Step 5 : Intantiate loss class
criterion = nn.CrossEntropyLoss()
# Step 6 : Intantiate optimizer
learning_rate = 0.1
optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate)
# Step 7 : Train the Model
##1 Convert inputs/labels to Variables
##2 Clear the gradients 
##3 Get output given input
##4 Get Loss
##5 Get gradients wrt parameters
##6 Update paramters using Gradients
##7 Repeat

iter=0
for epoch in range(n_epochs):
  for i,(images, labels) in enumerate(train_dataloader):

      ##1 Convert inputs/labels to Variable
      images = Variable(images.view(-1,28*28))
      labels = Variable(labels)

      ##2 Clear the gradients 
      optimizer.zero_grad()

      ##3 Get output given input
      outputs = model(images)

      ##4 Get Loss
      loss = criterion(outputs,labels)

      ##5 Get gradients wrt parameters
      loss.backward()
    
      ##6 Update paramters using Gradients
      optimizer.step()

      ##7 Repeat

      iter +=1
      if iter % 500 == 0 :
        correct = 0.
        total = 0.
      # Iter thorough test dataset
        for images,labels in test_dataloader:
          images = Variable(images.view(-1,28*28))
          labels = Variable(labels)

          outputs = model(images)
          # Get the max value of the prediction
          _,predicted=torch.max(outputs.data,1)
          # number of labels 
          total += labels.size(0)
          # Total correct predictions
          correct += (predicted==labels).sum()

        accuracy = 100*(correct/total)
        print("Iteration: {} , Loss:{} , Accuracy:{}".format(iter,loss.data,accuracy))
print_graph(loss.grad_fn, 1)<jupyter_output># <NllLossBackward object at 0x7f61cbdc8048>
## <LogSoftmaxBackward object at 0x7f61cbd9dc18>
### <AddmmBackward object at 0x7f61cbd9d908>
#### <AccumulateGrad object at 0x7f61cbd9d710>
#### <ReluBackward0 object at 0x7f61cbd9da90>
##### <AddmmBackward object at 0x7f61cbdcfac8>
###### <AccumulateGrad object at 0x7f61cbdcfa20>
###### <ReluBackward0 object at 0x7f61cbdcf128>
####### <AddmmBackward object at 0x7f61cbdcf198>
######## <AccumulateGrad object at 0x7f61cbdcfb70>
######## <ReluBackward0 object at 0x7f61cbdcf588>
######### <AddmmBackward object at 0x7f61cbdcfdd8>
########## <AccumulateGrad object at 0x7f61cbdcf4a8>
########## <ReluBackward0 object at 0x7f61cbc68048>
########### <AddmmBackward object at 0x7f61cbc682b0>
############ <AccumulateGrad object at 0x7f61cbdcfb38>
############ <ReluBackward0 object at 0x7f61cbdcfcc0>
############# <AddmmBackward object at 0x7f61cbdcf390>
############## <AccumulateGrad object at 0x7f622659c6d8>
############## <TBackward object at 0x7f61cb[...]
{'fraction_non_alphanumeric': 0.11030582988212807, 'fraction_numerical': 0.03265371137304874, 'mean_word_length': 2.7150887573964497, 'pattern_counts': {'":': 0, '<': 24, '<?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': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '201540', 'license': 'MIT', 'n_tokens_mistral': 4311, 'n_tokens_neox': 3934, 'n_words': 1000, 'path': 'Documents/Pytorch/FeedForwardNN_colabs.ipynb', 'repo_name': 'IJyotir/ijyotir.github.io', 'size': '12556'}