<jupyter_start><jupyter_text><jupyter_code>import torch
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import torch.nn as nn
from torch.autograd import Variable

train_dset = dsets.MNIST('./root', train = True, download = True,
                         transform = transforms.ToTensor())
test_dset = dsets.MNIST('./root', train = False, download = True,
                        transform = transforms.ToTensor())
import matplotlib.pyplot as plt
%matplotlib inline

import numpy as np
train_dset[0]
show_img = train_dset[0][0].numpy()
show_img.shape

plt.imshow(show_img.reshape(28,28), cmap = 'gray')
batch_size = 100
n_iters = 3000

epochs = n_iters / (len(train_dset) / batch_size)
epochs = int(epochs)
epochs
 train_loader = torch.utils.data.DataLoader(dataset = train_dset, shuffle = True, batch_size = batch_size)
test_loader = torch.utils.data.DataLoader(dataset = test_dset, shuffle = True, batch_size = batch_size)
import collections
isinstance(train_loader , collections.Iterable)<jupyter_output><empty_output><jupyter_text>Build the model<jupyter_code>class feed_forward_model(nn.Module):
  def __init__(self, input_dim, hidden_dim, output_dim):
    super(feed_forward_model, self).__init__()
    self.linear1 = nn.Linear(input_dim, hidden_dim)
    self.sigmoid = nn.Sigmoid()
    self.linear2 = nn.Linear(hidden_dim, output_dim)
    
    
  def forward(self, x):
    out = self.linear1(x)
    out = self.sigmoid(out)
    out = self.linear2(out)
    
    return out
    
input_dim = 28*28
hidden_dim = 128
output_dim = 10
model = feed_forward_model(input_dim, hidden_dim, output_dim)

criterion = nn.CrossEntropyLoss()
lr = 0.1

optimizer = torch.optim.SGD(lr = lr, params = model.parameters())
print(torch.cuda.is_available())<jupyter_output>True<jupyter_text>Iterate<jupyter_code>iter = 0

for epoch in range(epochs):
  for i, (images, labels) in enumerate(train_loader):
    
    images = Variable(images.view(-1,28*28))
    
    labels = Variable(labels)
    
    
    optimizer.zero_grad()
    
    outputs = model(images)
    
    loss = criterion(outputs, labels)
    
    loss.backward()
    
    optimizer.step()
    
    iter += 1
    
    if (iter%500 == 0):
      
      correct = 0
      total = 0
      
      
      
      """
      for images, labels in test_loader:
        images = Variable(images.view(-1, 28*28))
        outputs = model(images)
        
        _, predicted = torch.max(outputs.data, 1)
        
        total += labels.size(0)
        
        correct += (predicted == labels).sum()
        
      accuracy = 100 * correct / total
      
      print('Iteration: {}. Loss: {}. Accuracy: {}' .format(iter, loss.data, accuracy))
      
      """
      for images, labels in test_loader:
        
        images = Variable(images.view(-1, 28*28))
        
        outputs = model(images)
        
        _, predicted = torch.max(outputs.data, 1)
        
        total += labels.size(0)
        
        correct += (predicted.cpu() == labels.cpu()).sum()
       
      accuracy = 100 * correct / total
      
      print('Iteration: {}. Loss: {}. Accuracy: {}'.format(iter,loss.data, accuracy))
      
      
output = model(train_dset[0][0].view(28*28))
print(output)
plt.imshow(train_dset[2][0].numpy().reshape(28,28))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12349759615384616, 'fraction_numerical': 0.022235576923076924, 'mean_word_length': 2.839677047289504, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '199740', 'license': 'MIT', 'n_tokens_mistral': 1172, 'n_tokens_neox': 1112, 'n_words': 220, 'path': 'Feed_Forward_PyTorch.ipynb', 'repo_name': 'moustafa-7/PyTorch-Course', 'size': '3328'}