# How to train and evaluate models in PyTorch?

In PyTorch, you can train and evaluate models using the following steps:

- To define a model: First, you need to define a neural network model. You can use various neural network modules provided by PyTorch to build the model, or customize the model structure.
- Define the loss function: Choose an appropriate loss function based on the characteristics of the task to measure the difference between the model’s output and the actual labels.
- Definition of optimizer: Choose the appropriate optimizer to update the model’s parameters, common optimizers include SGD, Adam, etc.
- Model Training: Input training data into the model iteratively, calculate loss and update model parameters through backpropagation, until the model converges or reaches the specified number of training epochs.
- Model evaluation: use a test data set to assess the performance of the trained model, metrics such as accuracy, precision, recall, etc. can be calculated to evaluate the model’s performance.

Here is a simple example code demonstrating how to train and evaluate a model in PyTorch.

```
import torch
import torch.nn as nn
import torch.optim as optim
# 定义模型
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.fc = nn.Linear(10, 1)
def forward(self, x):
return self.fc(x)
model = SimpleModel()
# 定义损失函数和优化器
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
# 训练模型
for epoch in range(num_epochs):
for inputs, labels in train_loader:
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# 评估模型
total_correct = 0
total_samples = 0
with torch.no_grad():
for inputs, labels in test_loader:
outputs = model(inputs)
_, predicted = torch.max(outputs, 1)
total_correct += (predicted == labels).sum().item()
total_samples += labels.size(0)
accuracy = total_correct / total_samples
print('Accuracy: {:.2f}%'.format(accuracy * 100))
```

In this example, we defined a simple model, SimpleModel, and trained it using an SGD optimizer and mean squared error loss function. We also calculated the model’s accuracy on the test dataset. In practical applications, the choice of model structure, loss function, and optimizer can be customized based on the specific requirements of the task, and training can be fine-tuned accordingly.