[Deep Learning] Pytorch Series Tutorials (14): PyTorch Data Structure: 6. Module: Forward Propagation

        

Table of contents

I. Introduction

2. Experimental environment

3. PyTorch data structure

0. Classification

1. Tensor

2. Tensor Operations

3. Variable

4. Dataset

5. Data Loader (DataLoader)

6. Module


I. Introduction

ChatGPT:

        PyTorch is an open source machine learning framework that is widely used in the field of deep learning. It provides a rich set of tools and libraries for building and training various types of neural network models. Here is some detailed introduction to PyTorch:

  1. Dynamic calculation graph: PyTorch uses a dynamic calculation graph for calculations, which means that the calculation graph can be dynamically defined, modified and adjusted at runtime, making model construction and debugging more flexible and intuitive.

  2. Powerful GPU acceleration support: PyTorch makes full use of the GPU for calculations, which can greatly improve the speed of training and inference. It provides optimized operations and memory management for GPU, making running models on GPU more efficient.

  3. Automatic derivation: PyTorch has a built-in automatic derivation function, which can automatically calculate the gradient according to the defined calculation graph. This simplifies the implementation of the backpropagation algorithm and makes training neural network models more convenient.

  4. A large number of pre-trained models and model libraries: There are many pre-trained models and model libraries available in the PyTorch ecosystem, such as TorchVision, TorchText, and TorchAudio, etc. These models can be easily loaded and used to speed up model development.

  5. High-level abstraction interface: PyTorch provides high-level abstraction interfaces, such as nn.Moduleand nn.functional, for quickly building neural network models. These interfaces encapsulate commonly used neural network layers and functions, simplifying the model definition and training process.

  6. Support distributed training: PyTorch supports distributed training on multiple GPUs and multiple machines, which can accelerate the training process and process large-scale data and models.

        Overall, PyTorch provides a flexible and powerful platform that makes deep learning research and development more convenient and efficient. Its simple API and rich functions allow users to quickly implement complex neural network models and achieve excellent performance in various tasks.

2. Experimental environment

        This series of experiments uses the following environment

conda create -n DL python=3.7 
conda activate DL
pip install torch==1.8.1+cu102 torchvision==0.9.1+cu102 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
conda install matplotlib

Regarding configuration environment issues, you can refer to the previous painful experience:

3. PyTorch data structure

0. Classification

  • Tensor : Tensor is the most basic data structure in PyTorch, similar to a multi-dimensional array. It can represent a scalar, vector, matrix, or array of arbitrary dimensions.
  • Tensor operations : PyTorch provides a wealth of operation functions for performing various operations on Tensor, such as mathematical operations, statistical calculations, tensor deformation, indexing and slicing, etc. These operation functions can efficiently utilize the GPU for parallel computing and accelerate the model training process.
  • Variable : Variable is an encapsulation of Tensor and is used for automatic derivation. In PyTorch, Variables automatically track and record operations performed on them, building computational graphs and supporting automatic derivation. In PyTorch 0.4.0 and later versions, Variable is abandoned, and Tensor can be used directly for automatic derivation.
  • Dataset : Dataset is an abstract class used to represent data sets. By inheriting the Dataset class, you can customize the data set and implement functions such as data loading, preprocessing, and sample acquisition. PyTorch also provides some built-in data set classes, such as MNIST, CIFAR-10, etc., to easily load commonly used data sets.
  • DataLoader : DataLoader is used to load the data in the Dataset in batches and provides multi-thread and multi-process data pre-reading functions. It can efficiently load large-scale data sets and supports operations such as random shuffling of data, parallel loading, and data enhancement.
  • Module : Module is the base class used to build models in PyTorch. By inheriting the Module class, you can define your own model and implement methods such as forward propagation and back propagation. Module provides functions such as parameter management, model saving and loading, etc. to facilitate model training and deployment.

1. Tensor _ _

        

PyTorch data structure: 1. Tensor (Tensor): dimensions (Dimensions), data types (Data Types)_QomolangmaH's blog-CSDN blog https://blog.csdn.net/m0_63834988/article/details/132909219​Edit https: //blog.csdn.net/m0_63834988/article/details/132909219 icon-default.png?t=N7T8https://blog.csdn.net/m0_63834988/article/details/132909219

2. Tensor Operations

3. Variable

PyTorch data structure: 3. Introduction to variables (Variable)_QomolangmaH's blog-CSDN blog icon-default.png?t=N7T8https://blog.csdn.net/m0_63834988/article/details/132923358?spm=1001.2014.3001.5501

4. Dataset

PyTorch data structure: 4. Dataset (Dataset)_QomolangmaH's blog-CSDN blog icon-default.png?t=N7T8https://blog.csdn.net/m0_63834988/article/details/132923697?spm=1001.2014.3001.5501

5. Data Loader (DataLoader)

PyTorch data structure: 5. Data Loader (DataLoader)_QomolangmaH's blog-CSDN blog icon-default.png?t=N7T8https://blog.csdn.net/m0_63834988/article/details/132924381?spm=1001.2014.3001.5502

6. Module

        In PyTorch, the general steps for building a model are to create a custom model class and subclass it as Module. In the initialization method of the model class (usually __init__), you can define the layer structure and parameters of the model. By overriding the forward propagation method ( forward), you can define how data flows through the model.

        The Module class provides many useful functions:

  •  Parameter management: The Module class automatically tracks and manages learnable parameters in the model. These parameters can parameters()be obtained through methods to facilitate optimization and parameter update operations.

  • Model saving and loading: torch.save()Methods can be used to save the entire model to a file for later reloading and use. When loading a model, you can use torch.load()methods to load saved model parameters.

  • Automatic derivation: Operations within the Module class support automatic derivation by default. During the forward propagation process, PyTorch will automatically build a calculation graph and record the gradient calculation method of each operation. In this way, the parameter gradients of the model can be automatically calculated and updated during the backpropagation process. 

        In addition to the above functions, the Module class also provides some other methods and properties, such as train()and eval()methods for switching the training and evaluation modes of the model, to()methods for moving the model to a specified device (such as CPU or GPU), etc.

import torch
import torch.nn as nn

# 自定义模型类
class CustomModel(nn.Module):
    def __init__(self, input_size, hidden_size, num_classes):
        super(CustomModel, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden_size)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(hidden_size, num_classes)
    
    def forward(self, x):
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        return out

# 创建一个自定义模型的实例
input_size = 10
hidden_size = 20
num_classes = 2

model = CustomModel(input_size, hidden_size, num_classes)

# 使用模型进行前向传播
input_data = torch.randn(5, input_size)
output = model(input_data)

print(output)

# 保存模型
torch.save(model.state_dict(), 'model.pth')

# 加载模型
loaded_model = CustomModel(input_size, hidden_size, num_classes)
loaded_model.load_state_dict(torch.load('model.pth'))
  • Defines a custom model class CustomModel, inherited from nn.Module.
  • The layer structure of the model is defined in the model's initialization method, and the data flow method is defined in the forward propagation method.
  • By creating a model instance model, we can use it for forward propagation.
  • Save model parameters to a file and load the parameters later to restore the model.

  

Supongo que te gusta

Origin blog.csdn.net/m0_63834988/article/details/132924603
Recomendado
Clasificación