通常的 PyTorch Workflow 是这样的. But the workflow steps can be repeated and changed depending on the problem you’re working on.
- Get data ready (turn into tensors)
- Build or pick a pretrained model to suit your problem
2.1 Pick a loss function & optimizer
2.2 Build a training loop - Fit the model to the data and make a prediction
- Evaluate the model
- Improve through experimentation
- Save and reload your trained model
Topic | Contents |
---|---|
Getting data ready | Data can be almost anything but to get started we’re going to create a simple straight line |
Build a model | Create a model to learn patterns in the data, and choose a loss function, optimizer and build a training loop |
Fitting the model to data (training) | Got the data and a model, now let’s the model (try to) find patterns in the (training) data. |
Making predictions and evaluating a model (inference) | The model’s found patterns in the data, let’s compare its findings to the actual (testing) data. |
Saving and loading a model | You may want to use your model elsewhere, or come back to it later |
Putting it all together | Let’s take all of the above and combine it. |
或者也可以是这几个步骤:
- 数据准备:首先准备好数据集,包括训练集,验证集和测试集。PyTorch提供了一系列工具和类来加载,预处理和组织数据,例如:
torch.utils.data.Dataset
和torch.utils.data.DataLoader
- 模型定义:定义神经网络模型的结构,包括网络层的组织结构,激活函数等。可以使用PyTorch提供的
torch.nn.Module
类来创建模型。 - 损失函数定义:根据任务的性质选择合适的损失函数,用于衡量模型预测与真实标签之间的差异。PyTorch提供了各种损失函数,例如交叉熵损失函数,均方误差损失函数等。
- 优化器选择:选择合适的优化算法来更新模型参数,使得损失函数最小化。常见的优化算法包括随机梯度下降 (SGD),Adam, RMSprop等。PyTorch提供了
torch.optim
模块来实现各种优化算法。 - 模型训练:使用准备好的数据集,模型,损失函数和优化器来进行模型训练。训练过程通常包括多个周期 (epochs),每个周期包括数据集的多个批次 (batches)。在每个批次中,依次执行以下步骤:
- 前向传播 (Forward Pass): 将输入数据传递给模型,计算模型的输出。
- 计算损失值:使用损失函数计算模型输出与真实标签之间的损失之。
- 反向传播 (Backward Pass): 根据损失值计算模型参数的梯度。
- 参数更新:使用优化器根据参数的梯度更新模型参数。
- 模型评估:使用验证集或测试集评估训练好的模型的性能。通常会计算模型在验证集或测试集上的准确率,精确率,召回率等指标。
- 模型保存和部署:将训练好的模型保存为文件,并在需要时加载模型进行预测。PyTorch提供了·
torch.save()
和torch.load()
函数来保存和加载模型。模型也可以通过TorchScript进行序列化,以便于在其他平台上进行部署。
看到这了,给个赞呗~