pytorch
  1. pytorch-cnn-introduction

CNN Introduction - ( Convolutional Neural Network in PyTorch )

Heading h2

Syntax

PyTorch

import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

Example

PyTorch

import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms

transform = transforms.Compose([transforms.ToTensor(),
                                transforms.Normalize((0.5,), (0.5,))])

trainset = torchvision.datasets.MNIST(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                            shuffle=True, num_workers=2)

testset = torchvision.datasets.MNIST(root='./data', train=False,
                                        download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                            shuffle=False, num_workers=2)

classes = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

net = Net()

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

for epoch in range(2):
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        inputs, labels = data

        optimizer.zero_grad()

        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
        if i % 2000 == 1999:  
            print('[%d, %5d] loss: %.3f' %
                    (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0

print('Finished Training')

Output

[1,  2000] loss: 2.238
[1,  4000] loss: 0.943
[1,  6000] loss: 0.626
[1,  8000] loss: 0.487
[1, 10000] loss: 0.428
[1, 12000] loss: 0.380
[2,  2000] loss: 0.330
[2,  4000] loss: 0.314
[2,  6000] loss: 0.311
[2,  8000] loss: 0.285
[2, 10000] loss: 0.268
[2, 12000] loss: 0.253
Finished Training

Explanation

Convolutional Neural Networks (CNN) are deep neural networks commonly used for analyzing visual imagery, such as images and videos.

CNNs are designed to recognize patterns in images. They achieve this by using convolution filters to scan the image, identifying and "learning" important features in the image to classify it. CNNs also use pooling layers to reduce the spatial dimensions of the image, making it easier to process.

PyTorch is a powerful tool for building CNNs. The example above shows how to build a simple CNN using PyTorch, for classifying images in the MNIST dataset.

Use

CNNs are used in a wide range of applications, from image classification and object detection, to speech recognition and natural language processing. They are especially useful when dealing with large, complex datasets that require high levels of accuracy.

PyTorch is an excellent tool for building CNNs, with a powerful set of features and an easy-to-use interface.

Important Points

  • CNNs are deep neural networks commonly used for analyzing visual imagery
  • CNNs use convolution filters to scan the image and identify important features
  • PyTorch is a powerful tool for building CNNs, with an easy-to-use interface and powerful features

Summary

In conclusion, CNNs are a powerful tool for analyzing visual imagery, and are widely used in a range of applications. PyTorch provides a powerful set of features for building CNNs, and is an excellent choice for researchers and developers looking to work with deep learning.

Published on: