Several major steps for pytorch to build a training model2021-10-24

Several steps for pytorch to build a training model

1. Data preparation
2. Model construction
3. Definition of cost function and optimizer
4. Iterative training
5. Model preservation

1. Data preparation
The data to be used for training is processed in the early stage, and the feature representations that need to be learned are extracted, including data features and corresponding data labels.

#data_processing() 为自己设计的数据前期处理函数,对数据进行特征的提取
x_data, y_data=date_processing()    #x_data一般为向量列表,y_data一般为分类标签信息

Then convert the data into Tensor for easy model use

from torch.utils.data import TensorDataset

dataset = TensorDataset(torch.tensor(x_data, dtype=torch.float), torch.tensor(y_data))

To build a data batch, the model needs to iteratively learn in batches

Call the torch.utils.data.DataLoader module

from torch.utils.data import DataLoader

dataloader = DataLoader(dataset, batch_size=64, shuffle=True)
#batch_size定义块大小 shuffle决定是否打乱块顺序

In some cases, Dataset needs to be rewritten

from torch.utils.data import Dataset
class Iterator(Dataset):
    def __init__(self, data_dict):
        self.data_dict = data_dict
        self.keys = list(data_dict.keys())

    def __getitem__(self, index):
    	##获得数据标签index
        return {
    
    k: self.data_dict[k][index] for k in self.keys}

    def __len__(self):
    	##获取数据量
    	data_dataset=len()或者 data_dict.shap[0]
        return len

2. Model construction

Inherit torch.nn.Module and implement the following methods

class TrainModel(nn.Module):
    def __init__(self):
        super(TrainModel, self).__init__()
        #定义模型参数

    def forward(self, x):
    #前向传播
       #定义模型结构
        return out
model = TrainModel()

3. Define the cost function and optimizer

import torch.nn as nn
import torch.optim as optim

criterion = nn.CrossEntropyLoss()   #定义自己的需要的损失函数,此处为交叉熵损失函数
optimizer = optim.Adam(model.parameters(),lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, amsgrad=False)  #定义迭代优化器

4. Iterative training

num_epochs=100 #定义迭代的次数
total_step = len(dataloader)  #统计训练数据共有多少Bitch

for epoch in range(num_epochs): 
        train_loss = 0
        for step, (x,y_true ) in enumerate(dataloader):
           
            y_pred= model(x)   # 前向传播
            loss = criterion(y_pred, y_true)  # 计算损失

            optimizer.zero_grad() ##清零梯度
            loss.backward()		##反向传播
            train_loss += loss.item()   #叠加一个epoch内所有bitch的损失和
            optimizer.step() ##更新梯度参数
         print('Epoch [{}/{}], train_loss: {:.4f}'.format(epoch + 1, num_epochs, train_loss / total_step))
 

5. Save the model

torch.save(model.state_dict(),save_path)

Guess you like

Origin blog.csdn.net/weixin_42213421/article/details/120938474