<jupyter_start><jupyter_text><jupyter_code>!pip install torch torchvision
import torch
import numpy as np
from torchvision import datasets, transforms
import torch.nn.functional as F
from torch import nn

import matplotlib.pyplot as plt
#transform
transform = transforms.Compose([transforms.ToTensor(),
                                transforms.Normalize((0.5,), (0.5,))   # mean and std deviation -1 to 1
                                ])
# load data
train_data = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
train_data
def im_convert(tensor):
  image = tensor.clone().detach().numpy()
  image = image.transpose(1,2,0) #28/28/1
  print(image.shape)
  image = image * np.array((0.5,0.5,0.5)) + np.array((0.5,0.5,0.5))
  image = image.clip(0,1)
  return image
# train loader
train_loader = torch.utils.data.DataLoader(dataset=train_data, batch_size=100, shuffle=True)
dataiter = iter(train_loader)
images, labels = dataiter.next()

fig = plt.figure(figsize=(20,8))

for i in np.arange(20):
  ax = fig.add_subplot(2, 10, i+1)
  plt.imshow(im_convert(images[i]))
  ax.set_title([labels[i].item()])
validation_dataset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)
validation_dataset
validation_loader = torch.utils.data.DataLoader(validation_dataset, batch_size=100, shuffle=False)
validation_loader
# Classifier

class Classifier(nn.Module):
    
    def __init__(self, D_in, H1, H2, D_out):
        super().__init__()
        self.linear1 = nn.Linear(D_in, H1)
        self.linear2 = nn.Linear(H1, H2)
        self.linear3 = nn.Linear(H2, D_out)
        
    def forward(self, x):
        x = F.relu(self.linear1(x))  
        x = F.relu(self.linear2(x))
        x = self.linear3(x)
        return x
model = Classifier(784, 125, 65, 10)
model
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
optimizer
epochs = 15
running_loss_history = []
running_corrects_history = []
val_running_loss_history = []
val_running_corrects_history = []
 
for e in range(epochs):
  
  running_loss = 0.0
  running_corrects = 0.0
  val_running_loss = 0.0
  val_running_corrects = 0.0
  
  for inputs, labels in train_loader:
    inputs = inputs.view(inputs.shape[0], -1)
    outputs = model(inputs)
    loss = criterion(outputs, labels)
    
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    _, preds = torch.max(outputs, 1)
    running_loss += loss.item()
    running_corrects += torch.sum(preds == labels.data)
 
  else:
    with torch.no_grad():
      for val_inputs, val_labels in validation_loader:
        val_inputs = val_inputs.view(val_inputs.shape[0], -1)
        val_outputs = model(val_inputs)
        val_loss = criterion(val_outputs, val_labels)
        
        _, val_preds = torch.max(val_outputs, 1)
        val_running_loss += val_loss.item()
        val_running_corrects += torch.sum(val_preds == val_labels.data)
      
    epoch_loss = running_loss/len(train_loader)
    epoch_acc = running_corrects.float()/ len(train_loader)
    running_loss_history.append(epoch_loss)
    running_corrects_history.append(epoch_acc)
    
    val_epoch_loss = val_running_loss/len(validation_loader)
    val_epoch_acc = val_running_corrects.float()/ len(validation_loader)
    val_running_loss_history.append(val_epoch_loss)
    val_running_corrects_history.append(val_epoch_acc)
    print('epoch :', (e+1))
    print('training loss: {:.4f}, acc {:.4f} '.format(epoch_loss, epoch_acc.item()))
    print('validation loss: {:.4f}, validation acc {:.4f} '.format(val_epoch_loss, val_epoch_acc.item()))<jupyter_output>epoch : 1
training loss: 0.9339, acc 77.1517 
validation loss: 0.4206, validation acc 88.7800 
epoch : 2
training loss: 0.3731, acc 89.4933 
validation loss: 0.3197, validation acc 91.2700 
epoch : 3
training loss: 0.3128, acc 90.9783 
validation loss: 0.2847, validation acc 91.7600 
epoch : 4
training loss: 0.2813, acc 91.8000 
validation loss: 0.2559, validation acc 92.6700 
epoch : 5
training loss: 0.2569, acc 92.6350 
validation loss: 0.2413, validation acc 93.0500 
epoch : 6
training loss: 0.2365, acc 93.2183 
validation loss: 0.2278, validation acc 93.5800 
epoch : 7
training loss: 0.2188, acc 93.7600 
validation loss: 0.2099, validation acc 93.9300 
epoch : 8
training loss: 0.2034, acc 94.1883 
validation loss: 0.2062, validation acc 93.9500 
epoch : 9
training loss: 0.1893, acc 94.5883 
validation loss: 0.1922, validation acc 94.2800 
epoch : 10
training loss: 0.1769, acc 94.9300 
validation loss: 0.1767, validation acc 94.8500 
epoch : 11
training loss: 0.1663, acc 95.1917 
va[...]<jupyter_text>Plotting Training Loss<jupyter_code>plt.plot(running_loss_history, label='training loss')
plt.plot(val_running_loss_history, label='validation loss')
plt.legend()<jupyter_output><empty_output><jupyter_text>Plotting Accuracy<jupyter_code>plt.plot(running_corrects_history, label='training accuracy')
plt.plot(val_running_corrects_history, label='validation accuracy')
plt.legend()
!pip3 install pillow==4.0.0
import PIL.ImageOps
import requests
from PIL import Image
 
url = 'https://images.homedepot-static.com/productImages/007164ea-d47e-4f66-8d8c-fd9f621984a2/svn/architectural-mailboxes-house-letters-numbers-3585b-5-64_1000.jpg'
response = requests.get(url, stream = True)
img = Image.open(response.raw)
plt.imshow(img)
# pre-processing

img = PIL.ImageOps.invert(img)
img = img.convert('1')
img = transform(img) 
plt.imshow(im_convert(img))

# prediction

img = img.view(img.shape[0], -1)
output = model(img)
_, pred = torch.max(output, 1)
print(pred.item())
### Validation Prediction

dataiter = iter(validation_loader)
images, labels = dataiter.next()
images_ = images.view(images.shape[0], -1)
output = model(images_)
_, preds = torch.max(output, 1)
 
fig = plt.figure(figsize=(25, 4))
 
for idx in np.arange(20):
  ax = fig.add_subplot(2, 10, idx+1, xticks=[], yticks=[])
  plt.imshow(im_convert(images[idx]))
  ax.set_title("{} ({})".format(str(preds[idx].item()), str(labels[idx].item())), color=("green" if preds[idx]==labels[idx] else "red"))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1317160534028004, 'fraction_numerical': 0.06300879192445458, 'mean_word_length': 4.259417808219178, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 1, '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': '208628', 'license': 'MIT', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2207, 'n_words': 463, 'path': 'projects/mnist/pytorch_udemy_vision_01.ipynb', 'repo_name': 'futureseadev/hgwxx7', 'size': '6142'}