Machine learning----PyTorch model training

PyTorch

  • I used pure manual linear regression before. The problem of linear regression is actually to find the value of w when the loss function is minimized.
  • Many functions in PyTorch are encapsulated, and we can use them directly.

loss function

Handwritten loss function
def loss(y, y_pred):
	"""损失函数"""
	# (真实值 - 预测值)^2 的平均值
    return ((y_pred - y)**2).mean()
Loss function encapsulated by PyTorch

损失函数就是计算 (预测值-真实值)^2的平均值
(均方误差)

torch.nn.MSELoss()方法参数:

  1. Predicted values ​​for all data
  2. all true values
  3. (The same goes for swapping positions, because the mean square error is calculated)
import torch
X = torch.tensor([1,2,3,4],dtype=torch.float32)
Y = torch.tensor([2,4,6,8],dtype=torch.float32)
w = torch.tensor(0.0,dtype=torch.float32,requires_grad=True)

def forward(x):
    return w * x

# 均方差计算预测值和真实值之间的距离
loss = torch.nn.MSELoss()
# 计算此时的损失
y_pre = forward(X)
l = loss(y_pre,Y)
print(f"此时的损失值:{
      
      l}")

Insert image description here


optimizer

The optimizer replaces the handwritten gradient descent algorithm. PyTorch will automatically calculate the gradient and update the parameters.

Define optimizer

optim模块内包含多个优化器,都是基于基本的梯度下降算法改进的算法,可以更快的求出最优的参数解,例如: SGD, Adam,Momentum,RMSProp,这里使用的是SGD梯度下降算法

  1. Parameters that need to be updated through backpropagation. There may be multiple parameters that need to be updated, so parameter 1 is a list.
  2. lrThe named parameter is learning rate
optimizer = torch.optim.SGD([W], lr=learning_rate)
Complete linear regression

l.backward()损失函数反向传播计算梯度
optimizer.step() 通过优化器 更新w参数 向梯度的方向走一步 (求解loss损失值关于w的偏导值)
optimizer.zero_grad() 清空梯度计算,防止梯度累加导致结果错误

# 创建x、y数据和自定义w参数
X = torch.tensor([1,2,3,4],dtype=torch.float32)
Y = torch.tensor([2,4,6,8],dtype=torch.float32)
w = torch.tensor(0.0,dtype=torch.float32,requires_grad=True)
# 定义学习率 和 训练模型迭代次数
learning_rate = 0.001
n_iters = 1000
# 创建损失函数
loss = torch.nn.MSELoss()
# 创建优化器 把w参数扔进去,要计算损失值关于w的关系(偏导数) 把学习率扔进去
optimizer = torch.optim.SGD([w],lr=learning_rate)

# 正向传播函数(模型)
def forward(x):
    """正向传播函数"""
    return w * x

# 训练模型
for epoch in range(n_iters):
    # 通过正向传播获取到预测值
    y_pred = forward(X)
    # 通过损失函数获取到损失值
    l = loss(y_pred,Y)
    # 反向传播计算梯度
    l.backward()
    # 通过优化器 更新w参数  向梯度的方向走一步
    optimizer.step()
    # 清空梯度计算,防止梯度累加导致结果错误
    optimizer.zero_grad()
    
    if epoch % 100 == 0:
        # 打印纪元 w参数的变化和损失值的变化
        print(f'epoch: {
      
      epoch},w: {
      
      w},loss: {
      
      l:.8f}')

Model building

Building the model eliminates the need for handwritten forward propagation functions.

torch.nn.Linear(input_size,output_size)Function representing a linear model

  • input_size: dimension of input data
  • output_size: dimension of output data

model.parameters()模型中的参数

# 创建x、y数据和自定义w参数
X = torch.tensor([[1],[2],[3],[4]],dtype=torch.float32)
Y = torch.tensor([[2],[4],[6],[8]],dtype=torch.float32)
# 测试集
X_test = torch.tensor([5],dtype=torch.float32)
# 定义模型参数
n_samples,n_features = X.shape
print(n_features)
# 因为当前定义的输入和输出维度一致  所以参数输入和输出都为n_features
model = torch.nn.Linear(n_features,n_features)
# 定义学习率 和 训练模型迭代次数
learning_rate = 0.01
n_iters = 1000
# 创建损失函数
loss = torch.nn.MSELoss()
# 创建优化器 把模型需要更新参数扔进去,要计算损失值关于w的关系(偏导数) 把学习率扔进去
optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate)


# 训练模型
for epoch in range(n_iters):
    # 通过正向传播获取到预测值
    y_pred = model(X)
    # 通过损失函数获取到损失值
    l = loss(y_pred,Y)
    # 反向传播计算梯度
    l.backward()
    # 通过优化器 更新w参数  向梯度的方向走一步
    optimizer.step()
    # 清空梯度计算,防止梯度累加导致结果错误
    optimizer.zero_grad()
    
    if epoch % 100 == 0:
        # 获取到模型中w参数的值和b的值
        w,b = model.parameters()
        # 打印纪元 w参数的变化和损失值的变化
        print(f'epoch: {
      
      epoch},w: {
      
      w[0,0].item()},loss: {
      
      l}')

Insert image description here

After training is completed, use the test set to test the w parameter:

test_model = model(X_test)

Insert image description here
y = 2x + b Data 5 is infinitely close to 10


Summarize:

PyTorch training model process:

  1. Get the training set, get the input dimensions and output dimensions
  2. Create an appropriate model based on input and output dimensions
  3. Create loss function
  4. Create optimizer
  5. Training model forward propagation ➡ back propagation ➡ gradient descent ➡ loop
  6. Get the test set to evaluate the model

Machine learning is not about finding the solution at once, but a process of slowly approaching the solution.

Guess you like

Origin blog.csdn.net/bjsyc123456/article/details/124849919