pytorch
  1. pytorch-feed-forward-process

Feed Forward Process - ( Deep Neural Network with PyTorch )

Heading h2

Syntax

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

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        # define fully connected layers
        self.fc1 = nn.Linear(784, 256)
        self.fc2 = nn.Linear(256, 128)
        self.fc3 = nn.Linear(128, 10)

    def forward(self, x):
        # flatten input tensor
        x = x.view(-1, 784)
        
        # apply activation functions
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)

        return x

Example

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

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        # define fully connected layers
        self.fc1 = nn.Linear(784, 256)
        self.fc2 = nn.Linear(256, 128)
        self.fc3 = nn.Linear(128, 10)

    def forward(self, x):
        # flatten input tensor
        x = x.view(-1, 784)
        
        # apply activation functions
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)

        return x

net = Net()
input_tensor = torch.randn(1, 784)
output = net(input_tensor)
print(output)

Output

tensor([[ 0.1891, -0.1368, -0.1000, -0.1336, -0.1730, -0.0578,  0.1194,  0.1927,
         -0.1222, -0.1678]], grad_fn=<AddmmBackward>)

Explanation

The Feed Forward Process, also known as forward propagation, is the process of passing input data through a neural network in order to get output. In this example, we define a neural network with 3 fully connected layers using PyTorch.

We use the nn.Linear module to define the fully connected layers, and the F.relu activation function to apply ReLU activation to the output of the first two layers. We then apply a final linear layer to get the output.

In the example code, we initialize an instance of the Net class, generate a random input tensor of size 1x784, and pass it through the network. The resulting output tensor is printed.

Use

The Feed Forward Process is used in deep learning for a variety of applications, such as image and speech recognition, language translation, and more.

Important Points

  • The Feed Forward Process is the process of passing input data through a neural network to get output
  • It is also known as forward propagation
  • PyTorch is a popular deep learning library used for implementing neural networks
  • PyTorch has built-in modules such as nn.Linear for defining fully connected layers and F.relu for applying activation functions
  • The output of the final layer of a neural network can be used to make predictions

Summary

In summary, the Feed Forward Process is an essential part of implementing deep neural networks for a wide variety of applications. PyTorch is a popular deep learning library that provides built-in modules for implementing neural networks, including fully connected layers and activation functions. By passing input data through a neural network, we can produce output that can be used for making predictions.

Published on: