359 views|4 replies

163

Posts

3

Resources
The OP
 

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.

02-simple-way.ipynb.zip (2.51 KB, downloads: 3)

Latest reply

print(f'epoch {epoch + 1}, loss {l:f}') If you don't understand, please ask, does anyone know what {l:f} means? l is a variable, what is the role of :f here?   Details Published on 2024-10-30 23:12
 
 

278

Posts

0

Resources
2
 
Linear regression? Not linear regression?

Comments

Edited, thanks for the correction  Details Published on 2024-10-30 22:45
 
 
 

163

Posts

3

Resources
3
 
zhoupxa posted on 2024-10-30 22:39 Linear regression? Not linear regression?

Edited, thanks for the correction

 
 
 

149

Posts

2

Resources
4
 

print(f'epoch {epoch + 1}, loss {l:f}')

If you don't understand, please ask, does anyone know what {l:f} means? l is a variable, what is the role of :f here?

Comments

Formats floating point numbers for output, similar to Java's stringformat or C's %d %f %h  Details Published on 2024-10-30 23:27
 
 
 

163

Posts

3

Resources
5
 
ljg2np posted on 2024-10-30 23:12 print(f'epoch {epoch + 1}, loss {l:f}') If you don't understand, just ask. Does anyone know what {l:f} means...

Formats floating point numbers for output, similar to Java's stringformat or C's %d %f %h

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list