PyTorch 构建 simple model

usingPyTorch构建第一个机器Learningmodel, including线性回归 and 逻辑回归, understandingmodel构建 basic流程

PyTorch 构建 simple model

1. PyTorch model构建Basics

in PyTorchin, model通常inheritance自torch.nn.Moduleclass, 并implementationforwardmethod来定义 before 向传播逻辑. PyTorchproviding了丰富 预定义层, such as全连接层, 卷积层, 池化层etc., 我们可以直接using这些层来构建 complex model.

1.1 model构建 basic步骤

  1. 定义modelclass, inheritance自torch.nn.Module
  2. in __init__methodin初始化model 各个层
  3. implementationforwardmethod, 定义data before 向传播path
  4. creationmodelinstance
  5. 定义损失function and optimization器
  6. 训练model ( before 向传播, 计算损失, 反向传播, updateparameter)
  7. assessmentmodel

2. 线性回归model

线性回归 is 最 simple 机器Learningmodel之一, 它fake设输入 and 输出之间存 in 线性relationships. 我们可以usingPyTorcheasily构建线性回归model.

2.1 生成mockdata

首先, 我们生成一些mockdata用于训练线性回归model:

import torch
import numpy as np
import matplotlib.pyplot as plt

# 设置随机种子, 确保结果可重现
torch.manual_seed(42)

# 生成mockdata
X = torch.randn(100, 1) * 10  # 输入特征
y = X * 2 + 5 + torch.randn(100, 1) * 2  # 输出 (带 has 噪声) 

# visualizationdata
plt.scatter(X.numpy(), y.numpy())
plt.xlabel('X')
plt.ylabel('y')
plt.title('mockdata')
plt.show()

2.2 定义线性回归model

接 under 来, 我们定义线性回归model:

class LinearRegression(torch.nn.Module):
    def __init__(self, input_dim, output_dim):
        super(LinearRegression, self).__init__()
        # 定义全连接层, 输入维度 for input_dim, 输出维度 for output_dim
        self.linear = torch.nn.Linear(input_dim, output_dim)
    
    def forward(self, x):
        #  before 向传播, 直接返回线性变换 结果
        out = self.linear(x)
        return out

# creationmodelinstance
model = LinearRegression(input_dim=1, output_dim=1)

2.3 定义损失function and optimization器

线性回归通常using均方误差 (MSE) serving as损失function, 我们using随机梯度 under 降 (SGD) serving asoptimization器:

# 定义损失function
criterion = torch.nn.MSELoss()

# 定义optimization器, Learning率设置 for 0.01
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

2.4 训练model

现 in , 我们开始训练model:

# 训练轮数
epochs = 100

for epoch in range(epochs):
    #  before 向传播
    y_pred = model(X)
    
    # 计算损失
    loss = criterion(y_pred, y)
    
    # 清空梯度
    optimizer.zero_grad()
    
    # 反向传播, 计算梯度
    loss.backward()
    
    # updateparameter
    optimizer.step()
    
    # 每10轮打印一次损失
    if (epoch+1) % 10 == 0:
        print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}')

2.5 assessmentmodel

训练completion after , 我们可以assessmentmodel performance并visualization结果:

# assessmentmodel
model.eval()
with torch.no_grad():
    y_pred = model(X)
    test_loss = criterion(y_pred, y)
    print(f'Test Loss: {test_loss.item():.4f}')

# visualization结果
plt.scatter(X.numpy(), y.numpy(), label='真实data')
plt.plot(X.numpy(), y_pred.numpy(), color='red', label='预测直线')
plt.xlabel('X')
plt.ylabel('y')
plt.title('线性回归结果')
plt.legend()
plt.show()

# 打印modelparameter
print(f'modelparameter: 权重={model.linear.weight.item():.4f}, 偏置={model.linear.bias.item():.4f}')

3. 逻辑回归model

逻辑回归 is a用于二classificationissues model, 它throughsigmoidfunction将线性组合 结果map to [0, 1]区间, 表示样本属于正class 概率.

3.1 生成mockdata

我们生成一些二classification mockdata:

import torch
import numpy as np
import matplotlib.pyplot as plt

# 设置随机种子, 确保结果可重现
torch.manual_seed(42)

# 生成两classdata
class_0 = torch.randn(50, 2) * 0.5 + torch.tensor([1, 1])
class_1 = torch.randn(50, 2) * 0.5 + torch.tensor([-1, -1])

# mergedata
X = torch.cat([class_0, class_1], dim=0)
y = torch.cat([torch.zeros(50, 1), torch.ones(50, 1)], dim=0)

# visualizationdata
plt.scatter(class_0[:, 0].numpy(), class_0[:, 1].numpy(), label='class别 0')
plt.scatter(class_1[:, 0].numpy(), class_1[:, 1].numpy(), label='class别 1')
plt.xlabel('特征 1')
plt.ylabel('特征 2')
plt.title('二classificationmockdata')
plt.legend()
plt.show()

3.2 定义逻辑回归model

定义逻辑回归model:

class LogisticRegression(torch.nn.Module):
    def __init__(self, input_dim, output_dim):
        super(LogisticRegression, self).__init__()
        # 定义全连接层, 输入维度 for input_dim, 输出维度 for output_dim
        self.linear = torch.nn.Linear(input_dim, output_dim)
    
    def forward(self, x):
        #  before 向传播, 先for线性变换, 然 after throughsigmoidfunction
        out = self.linear(x)
        out = torch.nn.functional.sigmoid(out)
        return out

# creationmodelinstance
model = LogisticRegression(input_dim=2, output_dim=1)

3.3 定义损失function and optimization器

逻辑回归通常using二元交叉熵损失function:

# 定义损失function
criterion = torch.nn.BCELoss()

# 定义optimization器, Learning率设置 for 0.1
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)

3.4 训练model

训练逻辑回归model:

# 训练轮数
epochs = 100

for epoch in range(epochs):
    #  before 向传播
    y_pred = model(X)
    
    # 计算损失
    loss = criterion(y_pred, y)
    
    # 清空梯度
    optimizer.zero_grad()
    
    # 反向传播, 计算梯度
    loss.backward()
    
    # updateparameter
    optimizer.step()
    
    # 每10轮打印一次损失
    if (epoch+1) % 10 == 0:
        print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}')

3.5 assessmentmodel

assessmentmodel performance:

# assessmentmodel
model.eval()
with torch.no_grad():
    y_pred = model(X)
    # 将概率转换 for class别 ( big 于0.5 for class别1, 否则 for class别0) 
    y_pred_class = (y_pred > 0.5).float()
    
    # 计算准确率
    accuracy = (y_pred_class == y).sum().item() / y.size(0)
    print(f'Accuracy: {accuracy:.4f}')

# visualization决策edge界
x1 = np.linspace(-3, 3, 100)
x2 = -(model.linear.weight[0][0].item() * x1 + model.linear.bias.item()) / model.linear.weight[0][1].item()

plt.scatter(class_0[:, 0].numpy(), class_0[:, 1].numpy(), label='class别 0')
plt.scatter(class_1[:, 0].numpy(), class_1[:, 1].numpy(), label='class别 1')
plt.plot(x1, x2, color='red', label='决策edge界')
plt.xlabel('特征 1')
plt.ylabel('特征 2')
plt.title('逻辑回归决策edge界')
plt.legend()
plt.show()

提示

in PyTorchin, model eval()method用于将model切换 to assessment模式, 这会关闭dropout and batch normalizationetc. in 训练 and assessment阶段behavior不同 层. in assessmentmodel时, 我们通常会usingtorch.no_grad() on under 文management器来禁用梯度计算, 以improving计算efficiency并reducingmemory消耗.

4. usingSequential构建model

除了inheritancetorch.nn.Moduleclass来构建model out , PyTorch还providing了torch.nn.Sequentialclass, 可以更简洁地构建model. Sequentialclass允许我们按顺序添加层, 自动processing before 向传播.

4.1 用Sequential构建线性回归model

# usingSequential构建线性回归model
model = torch.nn.Sequential(
    torch.nn.Linear(input_dim=1, output_dim=1)
)

4.2 用Sequential构建逻辑回归model

# usingSequential构建逻辑回归model
model = torch.nn.Sequential(
    torch.nn.Linear(input_dim=2, output_dim=1),
    torch.nn.Sigmoid()
)

注意

Sequentialclass适用于 simple 线性model, 即每一层 输出直接serving as under 一层 输入. for 于 complex modelstructure, such as带 has branch or 跳跃连接 model, 仍然需要inheritancetorch.nn.Moduleclass并手动implementationforwardmethod.

实践练习

练习1: 线性回归model

生成 new mockdata, 其iny and X relationships for y = 3*X + 7 + 噪声, 然 after usingPyTorch构建线性回归model并训练, 观察model is 否able to准确Learning to 真实 权重 and 偏置.

练习2: many 特征线性回归

生成package含3个特征 mockdata, 其iny = w1*X1 + w2*X2 + w3*X3 + b + 噪声, 然 after usingPyTorch构建 many 特征线性回归model并训练.

练习3: 逻辑回归model

usingsklearn make_classificationfunction生成更 complex 二classificationdata, 然 after usingPyTorch构建逻辑回归model并训练, assessmentmodel 准确率.

5. summarized

本tutorial介绍了such as何usingPyTorch构建 simple 机器Learningmodel, including:

  • PyTorchmodel构建 basic步骤
  • 线性回归model 构建, 训练 and assessment
  • 逻辑回归model 构建, 训练 and assessment
  • usingSequentialclass简洁地构建model

这些Basicsmodel is 深度Learning 基石, Master它们 构建 and 训练method for 于Learning更 complex 深度Learningmodel至关 important .