# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="6000qI_T0Enh" colab_type="text"
# Import Modules

# + id="0m2JWFliFfKT" colab_type="code" colab={}
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms


# + [markdown] id="5tXCuK_J0KPL" colab_type="text"
# Build Network 
#
# CNN+pad --> cnn+pad --> pool --> cnn+pad --> cnn+pad --> pool --> cnn --> cnn --> cnn --> flatten --> predicton

# + id="h_Cx9q2QFgM7" colab_type="code" colab={}
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, 3, padding=1) #input - 1x 28 x 28 --> OUtput 32x 28 x 28 | RF 3x3
        self.conv2 = nn.Conv2d(32, 64, 3, padding=1) #input - 32x 28 x 28 --> OUtput 64 x 28 x 28 | RF 5x5
        self.pool1 = nn.MaxPool2d(2, 2) #input - 64 x 28 x 28 --> OUtput 64 x 14 x 14  | RF 6x6
        self.conv3 = nn.Conv2d(64, 128, 3, padding=1) #input - 64 x 14 x 14 --> OUtput 128 x 14 x 14 | RF 10 x 10
        self.conv4 = nn.Conv2d(128, 256, 3, padding=1) #input - 128 x 14 x 14 --> OUtput  256 x 14 x 14 | RF 16 x 16
        self.pool2 = nn.MaxPool2d(2, 2) #input - 256 x 14 x 14  --> OUtput 256 x 7 x 7 | RF 24 x 24
        self.conv5 = nn.Conv2d(256, 512, 3) #input - 256 x 7 x 7 --> OUtput  512 x 2 x 2 | RF 26 x 26
        self.conv6 = nn.Conv2d(512, 1024, 3) #input - 512 x 5 x 5 --> OUtput 1024 x 3 x 3  | RF 28 x 28
        self.conv7 = nn.Conv2d(1024, 10, 3) #input - 1024 x 3 x 3 --> OUtput 10 x 1 x 1 | RF 32 x 32

    def forward(self, x):
        x = self.pool1(F.relu(self.conv2(F.relu(self.conv1(x)))))
        x = self.pool2(F.relu(self.conv4(F.relu(self.conv3(x)))))
        x = F.relu(self.conv6(F.relu(self.conv5(x))))
        x = F.relu(self.conv7(x)) 
        x = x.view(-1, 10)  
        return F.log_softmax(x)


# + [markdown] id="xAEh_E010mSx" colab_type="text"
# assign the gpu for train and data loading

# + colab_type="code" id="xdydjYTZFyi3" colab={"base_uri": "https://localhost:8080/", "height": 434} outputId="aa83b2bc-d6dd-4b4e-b59c-d275e0597ce8"
# !pip install torchsummary
from torchsummary import summary
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
model = Net().to(device)
summary(model, input_size=(1, 28, 28))

# + [markdown] id="REyi4zoS0uZS" colab_type="text"
# Buid Dataset 

# + id="DqTWLaM5GHgH" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 391, "referenced_widgets": ["2808fa490dc94ba787b3412c298c4237", "9f0e492ff6a34ef98771df8397456d51", "82a41b697a33404db2fd4ff8947c107e", "e05f9e6994374df79e57c1386c439d81", "f799251175b149a0b85ec832e81d8283", "d0bc67f4a306436c9d5d4681c02cfdea", "232fada63e5446469bb115449e9b1add", "32557e309c3a4c559fab7a75d3315884", "a2a654c831074f6cb0b685057a9150fd", "cbc45d51af4e4d7290a769f070bd8c23", "<KEY>", "d6e1b256ace847b79ebec1813c1cf30c", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "dd4d414809154828bdbe0dfe89115b44", "48f2c848c42942feacf7ff10adb0e5e3", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "<KEY>", "1070509b01374e179a736f3b73e9d805", "0c8085ef5e4a46949cfc1d862c80ddad", "05f324695b0e4a499aefbd6cbd8050e2", "<KEY>", "<KEY>", "<KEY>", "3d93eee0fa7b469d96ce501521c471b5", "eb56e3ec54af4a9492f89788b1d62ebb"]} outputId="6404a081-2c1b-4d60-f157-a49d8ceac2eb"


torch.manual_seed(1)
batch_size = 128

kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
train_loader = torch.utils.data.DataLoader(
    datasets.MNIST('../data', train=True, download=True,
                    transform=transforms.Compose([
                        transforms.ToTensor(),
                        transforms.Normalize((0.1307,), (0.3081,))
                    ])),
    batch_size=batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
    datasets.MNIST('../data', train=False, transform=transforms.Compose([
                        transforms.ToTensor(),
                        transforms.Normalize((0.1307,), (0.3081,))
                    ])),
    batch_size=batch_size, shuffle=True, **kwargs)


# + [markdown] id="MZlFJK_Q0xyP" colab_type="text"
# Build Train and Test network and Data Flow

# + id="8fDefDhaFlwH" colab_type="code" colab={}
from tqdm import tqdm
def train(model, device, train_loader, optimizer, epoch):
    model.train()
    pbar = tqdm(train_loader)
    for batch_idx, (data, target) in enumerate(pbar):
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad()
        output = model(data)
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()
        pbar.set_description(desc= f'loss={loss.item()} batch_id={batch_idx}')


def test(model, device, test_loader):
    model.eval()
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            test_loss += F.nll_loss(output, target, reduction='sum').item()  # sum up batch loss
            pred = output.argmax(dim=1, keepdim=True)  # get the index of the max log-probability
            correct += pred.eq(target.view_as(pred)).sum().item()

    test_loss /= len(test_loader.dataset)

    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))


# + [markdown] id="ijbGwGIT02R_" colab_type="text"
# Declare Optimizer and Loss Functions and Epoch

# + id="MMWbLWO6FuHb" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 124} outputId="785a311d-72dc-4da5-81a7-dc063fba49f5"

model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)

for epoch in range(1, 2):
    train(model, device, train_loader, optimizer, epoch)
    test(model, device, test_loader)

# + id="OVj9oZaEgzag" colab_type="code" colab={}


# + id="So5uk4EkHW6R" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="a076c19d-ac6c-4376-a230-7640c5f3622d"

model = Net().to(device)
optimizer = optim.Adam(model.parameters(), lr=0.005,)

for epoch in range(1, 100):
    train(model, device, train_loader, optimizer, epoch)
    test(model, device, test_loader)

# + id="4UMIT0A11uNq" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered