How to evaluate and analyze the performance of a model in PyTorch?

In PyTorch, the test data set can be loaded using and the model’s eval() method can be called to enter evaluation mode. Then, the model can be used to make inferences on the test data set and calculate performance metrics such as accuracy and loss value.

import torch
import torch.nn as nn
import torch.optim as optim
import as data

# 加载测试数据集
test_dataset = MyDataset(data_path='test_data.pth')
test_loader = data.DataLoader(test_dataset, batch_size=64, shuffle=False)

# 加载模型
model = MyModel()

criterion = nn.CrossEntropyLoss()

# 在测试数据集上进行推理
correct = 0
total = 0
total_loss = 0
with torch.no_grad():
    for inputs, labels in test_loader:
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        total_loss += loss.item()
        _, predicted = torch.max(outputs, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

accuracy = correct / total
average_loss = total_loss / len(test_loader)
print(f'Accuracy: {accuracy}, Average Loss: {average_loss}')

In addition to calculating accuracy and loss values, tools like torch.utils.tensorboard can also be used for analyzing model performance, such as visualizing the changes in loss values and accuracy during training, as well as the distribution of model parameters.

Leave a Reply 0

Your email address will not be published. Required fields are marked *