Hands-on Deep Learning (PyTorch Edition) - [Reading Activity-Sharing Experience] Using Pytorch to Implement Linear Regression
[Copy link]
This post was last edited by Misaka10032 on 2024-10-30 22:42
Introduction
In the previous section, we manually implemented the training and definition of linear regression. In this section, we will use PYtorch's API to implement related operations.
Today, I have a new understanding of yesterday's code. I have to sigh that the writer's level is quite high.
This is mainly reflected in the above code. When we randomly generate training data, the X (tensor input) of this data set can be multi-dimensional. The training data set will create multiple x as input according to the weight value of true_w passed in. For example, in the above code
The length of true_w is 3, so after calling create_synthetic_data, the features created are a list of tensors with a length of 3.
Then use the data loader provided by torch to load the training data into torch. And define a small batch of data sets.
Line 109 This code imports the neural network package from torch.
Line 110 creates a neural network with an input of 3 (i.e. the length of the X vector) and an output of 1 (i.e. Y).
Line 111 initializes the weights and biases of the first layer of the neural network.
After 112, the calculation method of the loss function in the model is defined, that is, using nn.BCELoss()
Line 113 defines the optimization algorithm and learning rate of the model. The optimization algorithm used here is Stochastic Gradient Descent.
Then the model is trained
num_epochs = 10
for epoch in range(num_epochs):
for X, y in data_iter:
l = loss(net(X), y)
# 清空梯度
trainer.zero_grad()
# 反向传播
l.backward()
# 更新模型参数
trainer.step()
l = loss(net(features), labels)
print(f'epoch {epoch + 1}, loss {l:f}')
A total of ten rounds of training were conducted.
Finally, we save the complete model. We can create another file to load this model for prediction.
import numpy as np
import torch
from torch import nn
from torch.utils import data
from d2l import torch as d2l
# 加载模型参数
model = torch.load('model_complete.pth') # 直接加载整个模型
model.eval()
# 使用已加载的模型进行预测
# 示例输入特征
X_new = torch.tensor([[-0.2552, -1.1491, -2.3036]])
y_pred = loaded_net(X_new)
print(f'预测结果: {y_pred.item()}')
You can actually compare the predicted value with the original data. As shown below
The error is still very small.
The model trained with one thousand data is also very small, only 2 KB. Next, we will try to get this model into micro python.
|