<jupyter_start><jupyter_text>05_Convolutional_Neural_NetworksIn this notebook, we will see how to define simple convolutional neural networks.<jupyter_code>import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms

import matplotlib.pyplot as plt
%matplotlib inline

torch.manual_seed(777)  # reproducibility<jupyter_output><empty_output><jupyter_text>Convolutional Neural Network<jupyter_code># Hyper parameters
num_epochs = 5
num_classes = 10
batch_size = 100
learning_rate = 0.001

# Device configuration
# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device = torch.device('cpu')

# Convolutional neural network (two convolutional layers)
class ConvNet(nn.Module):
    def __init__(self, num_classes):
        super(ConvNet, self).__init__()
        # torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, bias=True)
        # torch.nn.MaxPool2d(kernel_size, stride=None, padding=0)
        # torch.nn.Sequential(*args): a sequential container.
        # Modules will be added to it in the order they are passed in the constructor.
        
        # floor((28-5+2*2)/1)+1 = 28, 28 / 2 = 14
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        # floor((14-5+2*2)/1)+1 = 14, 14 / 2 = 7
        self.layer2 = nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.fc = nn.Linear(7*7*32, num_classes)
        
    def forward(self, x):
        # x: (batch_size, 1, 28, 28) -> out: (batch_size, 16, 14, 14)
        out = self.layer1(x)
        # out: (batch_size, 16, 14, 14) -> out: (batch_size, 32, 7, 7)
        out = self.layer2(out)
        # out: (batch_size, 32, 7, 7) -> out: (batch_size, 32*7*7)
        out = out.reshape(out.size(0), -1)
        # out: (batch_size, 32*7*7) -> out: (batch_size, num_classes)
        out = self.fc(out)
        return out

model = ConvNet(num_classes).to(device)
print(model)<jupyter_output><empty_output><jupyter_text>Loss function and Optimizer<jupyter_code>criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)<jupyter_output><empty_output><jupyter_text>DataLoader<jupyter_code># MNIST dataset 
train_dataset = torchvision.datasets.MNIST(root='./data', 
                                           train=True, 
                                           transform=transforms.ToTensor(),  
                                           download=True)

test_dataset = torchvision.datasets.MNIST(root='./data', 
                                          train=False, 
                                          transform=transforms.ToTensor())

# Data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, 
                                           batch_size=batch_size, 
                                           shuffle=True)

test_loader = torch.utils.data.DataLoader(dataset=test_dataset, 
                                          batch_size=batch_size, 
                                          shuffle=False)<jupyter_output><empty_output><jupyter_text>Train the network<jupyter_code># Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
    running_loss = 0.0
    for i, (images, labels) in enumerate(train_loader):  
        # Move tensors to the configured device
        images = images.to(device)
        labels = labels.to(device)
        
        # Forward pass
        outputs = model(images)
        loss = criterion(outputs, labels)
        
        # zero the parameter gradients
        optimizer.zero_grad()
        # backward + optimize
        loss.backward()
        optimizer.step()
        
        running_loss += loss.item()
        if (i+1) % 100 == 0:
            print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' 
                   .format(epoch+1, num_epochs, i+1, total_step, running_loss / 100))
            running_loss = 0.0<jupyter_output><empty_output><jupyter_text>Test the network<jupyter_code># Test the model
# In test phase, we don't need to compute gradients (for memory efficiency)
with torch.no_grad():
    correct = 0
    total = 0
    for images, labels in test_loader:
        images = images.to(device)
        labels = labels.to(device)
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

    print('Accuracy of the network on the 10000 test images: {} %'.format(100 * correct / total))<jupyter_output><empty_output><jupyter_text>Save/Load the network parameters<jupyter_code># Save the model checkpoint
torch.save(model.state_dict(), './data/cnn_model.ckpt')

# Load the model checkpoint if needed
# new_model = NeuralNet(input_size, hidden_size, num_classes).to(device)
# new_model.load_state_dict(torch.load('./data/cnn_model.ckpt'))<jupyter_output><empty_output><jupyter_text>Practice: CIFAR10The CIFAR-10 dataset has the following specification:- The images in CIFAR-10 are of size 3x32x32, i.e. 3-channel color images of 32x32 pixels in size.- CIFAR-10 has the ten classes: ‘airplane’, ‘automobile’, ‘bird’, ‘cat’, ‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’.You have to define a convolutional neural network for performing image classifcation on the CIFAR-10 dataset as well as train and test the network.The architecture of the network should be:- first layer: conv2d(in_channels=3, out_channels=6, kernel_size=5) + ReLU- second layer: MaxPool2d(kernel_size=2, stride=2)- third layer: conv2d(in_channels=6, out_channels=16, kernel_size=5) + ReLU- fourth layer: Linear(in_features=?, out_features=120) + ReLU- fifth layer: Linear(in_features=120, out_features=84) + ReLU- sixth layer: Linear(84, num_classes)<jupyter_code># Hyper parameters
num_epochs = 5
num_classes = 10
batch_size = 100
learning_rate = 0.001

# Device configuration
device = torch.device('cpu')

# transform images to tensors of normalized range [-1, 1]
transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])


train_dataset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size,
                                          shuffle=True, num_workers=2)

test_dataset = torchvision.datasets.CIFAR10(root='./data', train=False,
                                       download=True, transform=transform)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size,
                                         shuffle=False, num_workers=2)

# Write the code to define the convolutional neural network for CIFAR-10
class Net(nn.Module):
    def __init__(self, num_classes):
        super(Net, self).__init__()
        # ============ YOUR CODE HERE ============
        
        # ========================================
    
    def forward(self, x):
        # ============ YOUR CODE HERE ============
        return x # DUMMY
        # ========================================

model = Net(num_classes).to(device)
print(model)

# Write the code to train and test the network.
# ============ YOUR CODE HERE ============
        
# ========================================<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13524914315844977, 'fraction_numerical': 0.027946216715001318, 'mean_word_length': 2.833754421424962, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 27, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '250099', 'license': 'MIT', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2270, 'n_words': 545, 'path': '_pages/AI/PyTorch/src/05_Convolutional_Neural_Networks.ipynb', 'repo_name': 'shpimit/shpimit.github.io', 'size': '7586'}