paddle2.3 implements regression model

        Recently, I am researching AI, and I plan to do some intelligent analysis or automatic analysis functions. In recent years, deep learning has become extremely popular, and various major companies have entered the market one after another, developing frameworks, and publishing papers. It is very lively, but the application still needs various People in the industry will realize it slowly.

frame selection

        It is essential to make a deep learning framework, because some repetitive work frameworks have already been implemented for us. We only need to build our own neural network and prepare data for training. At present, the mainstream frameworks include pytorch and tensorflow. In China, Baidu has paddlepaddle, and Huawei has mindspore. As a framework that started relatively early in China, paddle has a very mature ecology and api. The documentation is very complete, combining the advantages of pytorch and tensorflow, combining dynamic and static graphs, and supporting distributed training at the same time, so I chose paddle.

example

        The following is an example of a regression I implemented with paddle

import paddle
from paddle import nn
import numpy as np
from paddle import to_tensor
import matplotlib.pyplot as plt
from paddle.nn.functional import square_error_cost as sqrc
from visualdl import LogWriter
# 创建 LogWriter 对象,指定 logdir 参数,如果指定路径不存在将会创建一个文件夹
logwriter = LogWriter(logdir='./paddlets_test/reg')



x = np.linspace(0,1,100).astype("float32")
y = np.sin(np.pi*x)+0.3

plt.scatter(x,y)
#增加数据维度,变成N*1的形状,深度学习一般至少是二维数据
x_data = x[:,None]
y_data = y[:,None]


print(x_data.dtype)
print(y_data.dtype)
mynet_Sequential = nn.Sequential(
    nn.Linear(1, 32),
    nn.Tanh(),
    nn.Linear(32, 32),
    nn.Tanh(),
    nn.Linear(32, 32),
    nn.Tanh(),
    nn.Linear(32, 10),
    nn.Tanh(),
    nn.Linear(10, 1),

)
#mynet_Sequential.train()
x_data_train = to_tensor(x_data)
y_data_train = to_tensor(y_data)
loss_func =  paddle.nn.loss.MSELoss() #损失函数为均方差
optimizer = paddle.optimizer.SGD(learning_rate=0.01,parameters=mynet_Sequential.parameters())


for pass_id in range(5000):
    out = mynet_Sequential(x_data_train)
    loss = loss_func(out,y_data_train)
    logwriter.add_scalar("train_avg_loss", value=loss.numpy(), step=pass_id)
    logwriter.add_histogram("fc_weight", values=mynet_Sequential.parameters()[1].numpy(), step=pass_id)
    loss.backward()
    optimizer.minimize(loss)
    optimizer.step()
    optimizer.clear_grad()
    if pass_id %1000 == 0:
        print("pass:%d, Cost:%.5f" % (pass_id,loss))

paddle.jit.save(mynet_Sequential,"./paddlets_test/model",[paddle.static.InputSpec([-1,1])])
logwriter.add_hparams(hparams_dict={'lr': 0.01, 'batch_size':100,'opt': 'sgd'},
                           metrics_list=['train_avg_loss','test_avg_loss','test_avg_acc'])

y_test = mynet_Sequential(x_data_train)
y_value = y_test.numpy()
#print(y_value)
y_1d = [item[0] for item in y_value]
plt.plot(x_data,y_1d,color='r')

                                      

 After 5000 times of training, it can be seen that the fitting effect is not bad.

Because logs are added during the training process, visual dl can also be used to visualize training indicators such as network structure, parameters, and loss.

loss

 

 network structure

parameter

In general, the paddle experience is not bad, the tools are perfect, and the API is easy to use, especially now that the technological boundaries (national boundaries) have become obvious, and localization is becoming more and more important. Interested friends can try it.

Guess you like

Origin blog.csdn.net/zy1620454507/article/details/128834252