Example of PyTorch Custom Neural Network Class

This file defines a custom neural network class using PyTorch's framework, including convolutional, max pooling, ReLU activation, and linear layers for deep learning purposes. This network could be used for tasks like image classification or feature extraction where the input is structured as a multi-dimensional tensor.

from torch.nn import Module
from torch.nn import Conv2d
from torch.nn import Linear
from torch.nn import MaxPool2d
from torch.nn import ReLU
from torch.nn import LogSoftmax
from torch import flatten
 
class HessClass(Module):
    def __init__(self):
        super(HessClass, self).__init__()
 
        self.conv1 = Conv2d(in_channels=1, out_channels=32, kernel_size=3)
        self.conv2 = Conv2d(in_channels=32, out_channels=32, kernel_size=3)
        self.relu1 = ReLU()
        self.maxpool1 = MaxPool2d(kernel_size=2, stride=2)
 
        self.conv3 = Conv2d(in_channels=32, out_channels=64, kernel_size=3)
        self.conv4 = Conv2d(in_channels=64, out_channels=64, kernel_size=3)
        self.relu2 = ReLU()
        self.maxpool2 = MaxPool2d(kernel_size=2, stride=2)
 
        self.conv5 = Conv2d(in_channels=64, out_channels=16, kernel_size=3)
        self.conv6 = Conv2d(in_channels=16, out_channels=16, kernel_size=3)
        self.relu3 = ReLU()
        self.maxpool3 = MaxPool2d(kernel_size=2, stride=2)
 
        self.conv7 = Conv2d(in_channels=16, out_channels=16, kernel_size=3)
        self.conv8 = Conv2d(in_channels=16, out_channels=16, kernel_size=3)
        self.relu4 = ReLU()
        self.maxpool4 = MaxPool2d(kernel_size=2, stride=2)
 
        self.fc1 = Linear(1024, 128)
        self.relu5 = ReLU()
        self.fc2 = Linear(128, 1)
 
    def forward(self, x):
        out = self.conv1(x)
        out = self.conv2(out)
        out = self.relu1(out)
        out = self.maxpool1(out)
 
        out = self.conv3(out)
        out = self.conv4(out)
        out = self.relu2(out)
        out = self.maxpool2(out)
 
        out = self.conv5(out)
        out = self.conv6(out)
        out = self.relu3(out)
        out = self.maxpool3(out)
 
        out = self.conv7(out)
        out = self.conv8(out)
        out = self.relu4(out)
        out = self.maxpool4(out)
 
        out = out.reshape(out.size(0), -1)
 
        out = self.fc1(out)
        out = self.relu5(out)
        out = self.fc2(out)
 
        return out