1. Training Loop Step

  • A:Forward Pass:模型遍历所有数据一次,执行forward()计算(model(x_train));

  • B:Calculate Loss:模型的输出(预测)与ground truth比较,
    并评估,以了解错误程度:(loss = loss_fn(y_pred, y_train);

  • C:Zero Gradient:优化器梯度设置为零(默认累积accumulate),
    故可为特定的训练步骤重新计算 (optimizer.zero_grad())

  • D:Perform Backpropagation On Loss:计算每个要更新的模型参数
    (每个参数的requires_grad=True)的损失梯度,这称为反向传播,
    因此是"向后backwards",loss.backward()

  • E:Step Optimizer(Gradient Descent):将参数更新为
    requires_grad=True,以改善损耗梯度 (optimizer.step())

2. Mode To Label

  • go from raw model output to predict label;

  • logits → prediction probability → prediction label;

  • 在训练循环步骤之前,看看在前向传递过程中模型会产生啥结果,
    前向传递由forward()定义,为此想模型传递数据:

# view the frist 5 output of forward pass on test data
y_logit = model_0(X_test.to(device))[:5]
y_logit
tensor([[0.2625],
        [0.2737],
        [0.3577],
        [0.2350],
        [0.5554]], grad_fn=<SliceBackward0>)
  • 模型没经过训练,这些输出是随机的,它们是forward()的输出,
    已实现两层nn.Linear(),内部调用以下等式:

  • y = X * WeightT + bias

3. Build Train Test Loop