PyTorch three mainstream model construction methods: nn.Sequential, nn.Module subclasses, nn.Module container development practice, taking real smoke recognition scene data as an example

Keras and PyTorch are two commonly used deep learning frameworks, both of which provide high-level APIs for building and training neural networks.

Keras:
Keras is a high-level neural network API that can run on multiple low-level deep learning frameworks, such as TensorFlow and CNTK. The following are the features and benefits of Keras:

advantage:

Easy to use: Keras has a concise API design, easy to get started and use, suitable for rapid prototyping and experimentation.
Flexibility: Keras provides a high-level API and a modular architecture that can flexibly build various types of neural network models.
Reusability: Keras models can be easily saved and loaded, and models can be easily shared, deployed and migrated.
Community support: Keras has a large community support and an active developer community, providing a large number of documents, tutorials and sample code.
shortcoming:

Functional limitations: Compared to underlying frameworks such as TensorFlow and PyTorch, Keras may have limitations in some advanced functions and customization.
Scalability: While Keras provides an easy-to-use API, it may be limited on complex models that require a lot of customization and scalability.
Flexibility: Keras is mainly designed for simple processes. When complex non-standard tasks need to be processed, Keras is less flexible.
Applicable scene:

Beginners: For newcomers, Keras is an ideal choice because of its simplicity and ease of use, with extensive documentation and examples to help get started quickly.
Rapid prototyping: Keras can quickly build and iterate models, suitable for rapid prototyping and rapid experimental verification.
General computer vision and natural language processing tasks: Keras provides a large number of pre-trained models and tools for computer vision and natural language processing, which are suitable for the development and application of general tasks.
PyTorch:
PyTorch is a dynamic graph deep learning framework that emphasizes ease of use and low-latency debugging. The following are the features and benefits of PyTorch:

advantage:

Dynamic graphs: PyTorch uses dynamic graphs to make model building and debugging more flexible and intuitive, allowing models to be viewed and debugged in real time.
Free control: Compared with the static graph framework, PyTorch can more freely control the complex logic of the model and explore new network architectures.
Algorithm development: PyTorch provides a rich mathematical operation library and automatic derivation function, which is suitable for algorithm research and customized model development.
Community support: PyTorch has an active community and a large number of open source projects, providing a wealth of resources and support.
shortcoming:

Deployment complexity: Compared with high-level API frameworks such as Keras, PyTorch requires developers to deal more with model deployment and production environments.
Static optimization: Compared with static graph frameworks, such as TensorFlow, PyTorch cannot perform static graph optimization, and may be slightly inferior in performance.
Barriers to entry: Compared to Keras, PyTorch can have some steep learning curves for beginners.
Applicable scene:

Research and custom models: PyTorch is suitable for research and experimentation, as well as custom model development that requires flexibility and a high degree of freedom.
Advanced computer vision and natural language processing tasks: PyTorch has extensive applications in the fields of computer vision and natural language processing, and is rich in various pre-trained models and resources.
In the previous two articles, the overall system summary recorded the three mainstream methods of developing and building models in the two mainstream frameworks Keras and PyTroch, and corresponding basic example implementations were given. If you are interested, you can read it by yourself. :

“Summary records three mainstream ways of Keras development and construction of neural network models: sequence model, function model, subclass model”

“Summary and record the three mainstream ways of building neural network models in PyTorch: nn.Sequential builds the model in layer order, inherits the nn.Module base class to build a custom model, inherits the nn.Module base class to build the model and assists in using the model container to encapsulate it”

The main purpose of this article is to develop and practice these three different types of model building methods based on real business data scenarios, and conduct comparative analysis of the results.

First, let’s take a look at the data set:

First, let’s look at the sequence model construction implementation:

def initModel():
    """
    nn.Sequential builds the model in layer order
    """
    model = nn. Sequential()
    model.add_module("conv1", nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3))
    model.add_module("pool1", nn.MaxPool2d(kernel_size=2, stride=2))
    model.add_module("conv2", nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5))
    model.add_module("dropout", nn.Dropout2d(p=0.1))
    model. add_module("pool2", nn. AdaptiveMaxPool2d((1, 1)))
    model.add_module("flattens", nn.Flatten())
    model. add_module("linear1", nn. Linear(64, 32))
    model.add_module("relu1", nn.ReLU())
    model. add_module("linear2", nn. Linear(32, 1))
    return model

The next step is to inherit the nn.Module base class to build a custom model, as follows:

class initModel(nn.Module):
    """
    Inherit the nn.Module base class to build a custom model
    """

    def __init__(self):
        super(initModel, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3)
        self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
        self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5)
        self.dropout = nn.Dropout2d(p=0.1)
        self.pool2 = nn.AdaptiveMaxPool2d((1, 1))
        self.flatten = nn.Flatten()
        self.linear1 = nn.Linear(64, 32)
        self.relu = nn.ReLU()
        self.linear2 = nn.Linear(32, 1)

    def forward(self, x):
        x = self.conv1(x)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self. dropout(x)
        x = self.pool2(x)
        x = self.flatten(x)
        x = self. linear1(x)
        x = self.relu(x)
        x = self. linear2(x)
        return x

Finally, it inherits the nn.Module base class and assists in applying model containers for encapsulation and construction. As mentioned in the previous article, there are three model containers available, namely:

nn.Sequential
nn.ModuleList
nn.ModuleDict

The code implementation is as follows:

class initModel(nn.Module):
    """
    Inherit the nn.Module base class and assist in encapsulating the application model container
    nn.Sequential as a model container
    """

    def __init__(self):
        super(initModel, self).__init__()
        self.model = nn.Sequential()
        self.model.add_module(
            "conv1", nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3)
        )
        self.model.add_module("pool1", nn.MaxPool2d(kernel_size=2, stride=2))
        self.model.add_module(
            "conv2", nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5)
        )
        self.model.add_module("dropout", nn.Dropout2d(p=0.1))
        self.model.add_module("pool2", nn.AdaptiveMaxPool2d((1, 1)))
        self.model.add_module("flatten", nn.Flatten())
        self.model.add_module("linear1", nn.Linear(64, 32))
        self.model.add_module("relu", nn.ReLU())
        self.model.add_module("linear2", nn.Linear(32, 1))

    def forward(self, x):
        y = self. model(x)
        return y


class initModel(nn.Module):
    """
    Inherit the nn.Module base class and assist in encapsulating the application model container
    nn.ModuleList as model container
    """

    def __init__(self):
        super(initModel, self).__init__()
        self.layers = nn.ModuleList(
            [
                nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3),
                nn.MaxPool2d(kernel_size=2, stride=2),
                nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5),
                nn.Dropout2d(p=0.1),
                nn.AdaptiveMaxPool2d((1, 1)),
                nn. Flatten(),
                nn. Linear(64, 32),
                nn.ReLU(),
                nn. Linear(32, 1),
            ]
        )

    def forward(self, x):
        for layer in self.layers:
            x = layer(x)
        return x


class initModel(nn.Module):
    """
    Inherit the nn.Module base class and assist in encapsulating the application model container
    nn.ModuleDict as model container
    """

    def __init__(self):
        super(initModel, self).__init__()
        self.layers_dict = nn.ModuleDict(
            {
                "conv1": nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3),
                "pool1": nn.MaxPool2d(kernel_size=2, stride=2),
                "conv2": nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5),
                "dropout": nn.Dropout2d(p=0.1),
                "pool2": nn.AdaptiveMaxPool2d((1, 1)),
                "flatten": nn. Flatten(),
                "linear1": nn.Linear(64, 32),
                "relu": nn.ReLU(),
                "linear2": nn.Linear(32, 1),
            }
        )

    def forward(self, x):
        layers = [
            "conv1",
            "pool1",
            "conv2",
            "dropout",
            "pool2",
            "flatten",
            "linear1",
            "relu",
            "linear2",
        ]
        for layer in layers:
            x = self.layers_dict[layer](x)
        return x

Like the keras framework, the iterative calculation of 100 epochs is set by default. Here is a direct look at the result graph:

If you are interested, you can practice it yourself. Many contents or methods are essentially analogous.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeHomepageOverview 335116 people are learning the system