pytorch
  1. pytorch-architecture-of-dnn

Architecture of DNN - ( Deep Neural Network with PyTorch )

Heading h2

Syntax

PyTorch

import torch.nn as nn

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(784, 512)
        self.fc2 = nn.Linear(512, 256)
        self.fc3 = nn.Linear(256, 10)

    def forward(self, x):
        x = x.view(-1, 784)
        x = nn.functional.relu(self.fc1(x))
        x = nn.functional.relu(self.fc2(x))
        x = self.fc3(x)
        return nn.functional.log_softmax(x, dim=1)

Example

PyTorch

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms

# Define hyperparameters
batch_size = 64
learning_rate = 0.01
epochs = 10

# Load dataset
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)

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)

# Define model architecture
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(784, 512)
        self.fc2 = nn.Linear(512, 256)
        self.fc3 = nn.Linear(256, 10)

    def forward(self, x):
        x = x.view(-1, 784)
        x = nn.functional.relu(self.fc1(x))
        x = nn.functional.relu(self.fc2(x))
        x = self.fc3(x)
        return nn.functional.log_softmax(x, dim=1)

# Initialize model and optimizer
model = Net()
optimizer = optim.SGD(model.parameters(), lr=learning_rate)

# Train the model
for epoch in range(epochs):
    for batch_idx, (data, target) in enumerate(train_loader):
        optimizer.zero_grad()
        output = model(data)
        loss = nn.functional.nll_loss(output, target)
        loss.backward()
        optimizer.step()
    print('Train Epoch: {} \t Loss: {:.6f}'.format(
        epoch+1, loss.item()))

# Test the model
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
    for data, target in test_loader:
        output = model(data)
        test_loss += nn.functional.nll_loss(output, target, reduction='sum').item()
        pred = output.argmax(dim=1, keepdim=True)
        correct += pred.eq(target.view_as(pred)).sum().item()

test_loss /= len(test_loader.dataset)

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

Output

Train Epoch: 1 	 Loss: 0.150916
Train Epoch: 2 	 Loss: 0.057207
Train Epoch: 3 	 Loss: 0.052148
Train Epoch: 4 	 Loss: 0.033629
Train Epoch: 5 	 Loss: 0.045159
Train Epoch: 6 	 Loss: 0.014956
Train Epoch: 7 	 Loss: 0.007916
Train Epoch: 8 	 Loss: 0.041537
Train Epoch: 9 	 Loss: 0.014107
Train Epoch: 10 	 Loss: 0.015124
Test set: Average loss: 0.0790, Accuracy: 9764/10000 (98%)

Explanation

A Deep Neural Network (DNN) is a type of artificial neural network that has many hidden layers, allowing it to learn complex features and relationships in data. These networks are used in many applications including image recognition, natural language processing, and speech recognition.

PyTorch is a popular framework for building DNNs due to its dynamic computational graph and ability to handle complex graph-based models. In PyTorch, a DNN is defined as a subclass of the nn.Module class, which provides an abstract interface to construct neural networks by composing individual network components, called "modules," such as linear layers, convolutional layers, and activation functions.

Use

DNNs can be used for a wide range of applications including image classification, object detection, speech recognition, natural language processing, and more. PyTorch is a great framework for building DNNs due to its dynamic computation graph and easy-to-use API.

Important Points

  • A Deep Neural Network (DNN) is a type of artificial neural network that has many hidden layers
  • PyTorch is a popular framework for building DNNs due to its dynamic computational graph and ability to handle complex graph-based models
  • DNNs can be used for a wide range of applications including image classification, object detection, speech recognition, natural language processing, and more

Summary

In conclusion, a Deep Neural Network (DNN) is a powerful tool for solving complex tasks such as image recognition, speech recognition, and natural language processing. PyTorch is a flexible and powerful framework for building DNNs, and its dynamic computational graph and easy-to-use API make it an attractive option for many developers.

Published on: