Machine learning PaddlePaddle training code template

Two, import packages and set up random seeds

Paddle.seed (seed)[source code]

Random seed to set the global default generator.

Parameters:

Seed (int) – The random seed to set. A large integer is recommended.

Returns:

Generator: The global default Generator object.

Code examples:

import paddle
paddle.seed(102)
Copy the code
import numpy as np
import paddle
import paddle.nn as nn
import numpy as np
import pandas as pd
from paddle.io import DataLoader, Dataset
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import warnings
# ignore warnings
warnings.filterwarnings("ignore")

import random
seed = 42
# set random number
paddle.seed(seed)
np.random.seed(seed)
random.seed(seed)
Copy the code

Define hyperparameters as classes

class argparse() :
    pass
# check version
print(paddle.utils.run_check())
Check the CPU and GPU
print(paddle.device.get_device())
# overparameter Settings
args = argparse()
args.epochs, args.learning_rate, args.patience = [30.0.001.4]
args.hidden_size, args.input_size= [40.30]
# Equipment setting, paddlePaddle automatic setting, no manual setting
args.device, = [paddle.device.set_device(paddle.device.get_device() ),]
Copy the code
Running verify PaddlePaddle program ... 
PaddlePaddle works well on 1 CPU.
PaddlePaddle works well on 2 CPUs.
PaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now.
None
cpu
Copy the code

Define the model

# Custom model
class Your_model(nn.Layer) :
    def __init__(self) :
        super(Your_model, self).__init__()
        pass

    def forward(self,x) :
        pass
        return x
Copy the code

5. Define early stop classes (this step can be omitted)


class EarlyStopping() :
    def __init__(self,patience=7,verbose=False,delta=0) :
        self.patience = patience
        self.verbose = verbose
        self.counter = 0
        self.best_score = None
        self.early_stop = False
        self.val_loss_min = np.Inf
        self.delta = delta
    def __call__(self,val_loss,model,path) :
        print("val_loss={}".format(val_loss))
        score = -val_loss
        if self.best_score is None:
            self.best_score = score
            self.save_checkpoint(val_loss,model,path)
        elif score < self.best_score+self.delta:
            self.counter+=1
            print(f'EarlyStopping counter: {self.counter} out of {self.patience}')
            if self.counter>=self.patience:
                self.early_stop = True
        else:
            self.best_score = score
            self.save_checkpoint(val_loss,model,path)
            self.counter = 0
    def save_checkpoint(self,val_loss,model,path) :
        if self.verbose:
            print(
                f'Validation loss decreased ({self.val_loss_min:6.f} --> {val_loss:6.f}). Saving model ... ')
        paddle.save(model.state_dict(), path+'/'+'model_checkpoint.pdparams')
        self.val_loss_min = val_loss
Copy the code

6, define your own Dataset,DataLoader

class Dataset_name(Dataset) :
    def __init__(self, flag='train') :
        assert flag in ['train'.'test'.'valid']
        self.flag = flag
        self.__load_data__()

    def __getitem__(self, index) :
        pass
    def __len__(self) :
        pass

    def __load_data__(self, csv_paths: list) :
        pass
        print(
            "train_X.shape:{}\ntrain_Y.shape:{}\nvalid_X.shape:{}\nvalid_Y.shape:{}\n"
            .format(self.train_X.shape, self.train_Y.shape, self.valid_X.shape, self.valid_Y.shape))

train_dataset = Dataset_name(flag='train')
train_dataloader = DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)
valid_dataset = Dataset_name(flag='valid')
valid_dataloader = DataLoader(dataset=valid_dataset, batch_size=64, shuffle=True)
Copy the code

7. Instantiate the model, set loss and optimizer, etc

device = paddle.set_device('cpu') # or 'gpu'
model = paddle.Model(Your_model(), input. label) criterion = paddle.nn.MSELoss() optimizer = paddle.optimizer.Adam(Your_model.parameters(),lr=args.learning_rate) train_loss = [] valid_loss = [] train_epochs_loss = [] valid_epochs_loss = [] early_stopping = EarlyStopping(patience=args.patience,verbose=True)
Copy the code

8. Start training and adjust LR

for epoch in range(args.epochs):
    Your_model.train()
    train_epoch_loss = []
    for idx,(data_x,data_y) in enumerate(train_dataloader,0) : data_x = data_x.to(torch.float32).to(args.device) data_y = data_y.to(torch.float32).to(args.device) outputs = Your_model(data_x) optimizer.zero_grad() loss = criterion(data_y,outputs) loss.backward() optimizer.step() train_epoch_loss.append(loss.item()) train_loss.append(loss.item())if idx%(len(train_dataloader)//2) = =0:
            print("epoch={}/{},{}/{}of train, loss={}".format(
                epoch, args.epochs, idx, len(train_dataloader),loss.item()))
    train_epochs_loss.append(np.average(train_epoch_loss))

    #=====================valid============================
    Your_model.eval()
    valid_epoch_loss = []
    for idx,(data_x,data_y) in enumerate(valid_dataloader,0):
        data_x = paddle.to_tensor(data_x, dtype='float32', place=paddle.CUDAPlace(0), stop_gradient=False)
        data_y = paddle.to_tensor(data_y, dtype='float32', place=paddle.CUDAPlace(0), stop_gradient=False)
        outputs = Your_model(data_x)
        loss = criterion(outputs,data_y)
        valid_epoch_loss.append(loss.item())
        valid_loss.append(loss.item())
    valid_epochs_loss.append(np.average(valid_epoch_loss))
    #==================early stopping======================
    early_stopping(valid_epochs_loss[-1],model=Your_model,path=r'c:\\your_model_to_save')
    if early_stopping.early_stop:
        print("Early stopping")
        break
    #====================adjust lr========================
    lr_adjust = {
            2: 5e-5.4: 1e-5.6: 5e-6.8: 1e-6.10: 5e-7.15: 1e-7.20: 5e-8
        }
    if epoch in lr_adjust.keys():
        lr = lr_adjust[epoch]
        for param_group in optimizer.param_groups:
            param_group['lr'] = lr
        print('Updating learning rate to {}'.format(lr))
Copy the code

Nine, drawing

You can draw by hand or with the help of VisualDL. In this case, you can draw with the following code


plt.figure(figsize=(12.4))
plt.subplot(121)
plt.plot(train_loss[:])
plt.title("train_loss")
plt.subplot(122)
plt.plot(train_epochs_loss[1:].'-o',label="train_loss")
plt.plot(valid_epochs_loss[1:].'-o',label="valid_loss")
plt.title("epochs_loss")
plt.legend()
plt.show()
Copy the code

Ten, forecasting

Here you can define a Dataloader for the prediction set. 0 Could also 0 directly add batch_size=1 to your predicted data 0
Your_model.eval()
predict = Your_model(data)
Copy the code

Xi. Conclusion

From parameter definition, to network model definition, and then to training steps, verification steps, testing steps, summed up a set of more intuitive template. The directory is as follows:

    1. Import packages and set random seeds
    1. Define hyperparameters as a class
    1. Define your own model
    1. Define early stop classes (this step can be omitted)
    1. Define your own Dataset,DataLoader
    1. Instantiate the model, set up loss, optimizer, etc
    1. Start training and adjust LR
    1. drawing
    1. To predict