Pytorch - 工作流
总览

具体步骤:

1. Data (preparing and loading)
通过大模型(神经网络)将不同类型的输入(语音,文本,视频,图片等),转换成数据表示形式。

tips:
机器学习:
- 将数据转换成数字
- 训练一个模型去学习这些数据的规律
实践
使用线性回归来创建具有已知参数(模型可以学习的东西)的数据,然后使用 PyTorch 来看是否可以构建模型来估计这些参数(使用梯度下降方法)。
- 创建已知参数和数据
# Create *known* parameters
weight = 0.7
bias = 0.3
# Create data
start = 0
end = 1
step = 0.02
X = torch.arange(start, end, step).unsqueeze(dim=1)
y = weight * X + bias
- 拆分数据为训练集和测试集
# Create train/test split
train_split = int(0.8 * len(X)) # 80% of data used for training set, 20% for testing
X_train, y_train = X[:train_split], y[:train_split]
X_test, y_test = X[train_split:], y[train_split:]

- 绘制预测曲线
def plot_predictions(train_data=X_train,
train_labels=y_train,
test_data=X_test,
test_labels=y_test,
predictions=None):
"""
Plots training data, test data and compares predictions.
"""
plt.figure(figsize=(10, 7))
# Plot training data in blue
plt.scatter(train_data, train_labels, c="b", s=4, label="Training data")
# Plot test data in green
plt.scatter(test_data, test_labels, c="g", s=4, label="Testing data")
if predictions is not None:
# Plot the predictions in red (predictions were made on the test data)
plt.scatter(test_data, predictions, c="r", s=4, label="Predictions")
# Show the legend
plt.legend(prop={"size": 14});
效果图:

2. Build model
创建线性回归模型:

具体实现过程:
# 创建线性回归模型
class LinearRegressionModel(nn.Module):
def __init__(self):
super().__init__()
self.weights = nn.Parameter(torch.randn(1,
requires_grad=True,
dtype=torch.float
))
self.bias = nn.Parameter(torch.randn(1,
requires_grad=True,
dtype=torch.float
))
# Forward defines the computation in the model
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.weights * x + self.bias
# 设置手动种子
torch.manual_seed(42)
# 创建模型实例
model_0 = LinearRegressionModel()
# 查看具体参数
print(model_0.state_dict())
# 使用模型进行预测
with torch.inference_mode():
y_preds = model_0(X_test)
# 校验预测值
print(f"Number of testing samples: {len(X_test)}")
print(f"Number of predictions made: {len(y_preds)}")
print(f"Predicted values:\n{y_preds}")
plot_predictions(predictions=y_preds)
plt.show()
效果图:

3. Train model
通过第一步和第二步,发现构建的模型进行预测的值与实际测试数据偏差较大,进一步进行训练
Creating a loss function and optimizer in PyTorch

通过损失函数和优化器来更新模型参数。
- MAE:Mean Absolute Error(平均绝对误差)
- 创建损失函数和优化器
# 创建损失函数
loss_fn = nn.L1Loss() # MAE loss is same as L1Loss
# 创建优化器
optimizer = torch.optim.SGD(params=model_0.parameters(),lr=0.01)
- PyTorch 创建优化循环
有了损失函数和优化器,是时候创建训练循环和测试循环了。
- 训练循环是指模型遍历训练数据,学习特征和标签之间的关系。
- 测试循环是指遍历测试数据,评估模型在训练数据上学习到的模式的优劣。
训练循环步骤:

loss.backward()
- 这个函数用于反向传播(Backpropagation),它会自动计算损失(loss)相对于模型中所有可学习参数的梯度,并将这些梯度存储在参数的 .grad 属性中。
- 每次调用 backward() 都会在已有的 .grad 上累加新的梯度。因此,在每次迭代开始前通常需要调用 optimizer.zero_grad() 来清空之前的梯度。
optimizer.step()
- 这个函数用于根据当前计算的梯度来更新模型参数。具体的更新方式取决于你选择的优化器类型,比如 SGD、Adam 等。
训练循环demo:

测试循环步骤:

测试循环demo:

- 绘制损失曲线
# 绘制损失曲线
plt.plot(epoch_count, train_loss_values, label="Train loss")
plt.plot(epoch_count, test_loss_values, label="Test loss")
plt.title("Training and test loss curves")
plt.ylabel("Loss")
plt.xlabel("Epochs")
plt.legend()
plt.show()
结果:

- 模型学习的参数值
# 打印模型学习的参数值
print("The model learned the following values for weights and bias:")
print(model_0.state_dict())
print("\nAnd the original values for weights and bias are:")
print(f"weights: {weight}, bias: {bias}")
# 最开始的随机值为
# OrderedDict([('weights', tensor([0.3367])), ('bias', tensor([0.1288]))])
The model learned the following values for weights and bias:
OrderedDict([('weights', tensor([0.5784])), ('bias', tensor([0.3513]))])
And the original values for weights and bias are:
weights: 0.7, bias: 0.3
4. Making predictions with a trained PyTorch model (inference)
使用训练的模型进行预测:
# 使用训练的模型进行预测
model_0.eval()
with torch.inference_mode():
y_preds = model_0(X_test)
plot_predictions(predictions=y_preds)
plt.show()

5. Saving and loading a PyTorch model
保存模型:
# 保存模型
# 1. Create models directory
MODEL_PATH = Path("models")
MODEL_PATH.mkdir(parents=True, exist_ok=True)
# 2. Create model save path
MODEL_NAME = "01_pytorch_workflow_model_0.pth"
MODEL_SAVE_PATH = MODEL_PATH / MODEL_NAME
# 3. Save the model state dict
print(f"Saving model to: {MODEL_SAVE_PATH}")
torch.save(obj=model_0.state_dict(),f=MODEL_SAVE_PATH)
加载模型:
# 加载模型
# Instantiate a new instance of our model (this will be instantiated with random weights)
loaded_model_0 = LinearRegressionModel()
# Load the state_dict of our saved model (this will update the new instance of our model with trained weights)
loaded_model_0.load_state_dict(torch.load(f=MODEL_SAVE_PATH))
# 1. Put the loaded model into evaluation mode
loaded_model_0.eval()
# 2. Use the inference mode context manager to make predictions
with torch.inference_mode():
loaded_model_preds = loaded_model_0(X_test)
y_preds == loaded_model_preds
