Testing of DNN model - ( Deep Neural Network with PyTorch )
Heading h2
Syntax
PyTorch
import torch
def test(model, test_loader, criterion):
model.eval()
correct = 0
total_loss = 0.0
with torch.no_grad():
for data, target in test_loader:
output = model(data)
total_loss += criterion(output, target).item() * data.size(0)
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss = total_loss / len(test_loader.dataset)
test_acc = 100. * correct / len(test_loader.dataset)
print('Test Accuracy: {:.2f}%\n'.format(test_acc))
Example
import torch
import torchvision
import torchvision.transforms as transforms
import torch.nn as nn
import torch.optim as optim
# Define the neural network architecture
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool(torch.relu(self.conv1(x)))
x = self.pool(torch.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
# Define the test dataset and data loader
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=transform)
test_loader = torch.utils.data.DataLoader(testset, batch_size=4,
shuffle=False, num_workers=2)
# Load the pre-trained model
net = torch.load('model.pt')
# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
# Test the model on the test data set
test(net, test_loader, criterion)
Output
Test Accuracy: 70.00%
Explanation
When it comes to testing a Deep Neural Network model, the main goal is to evaluate the performance of our model on the unseen data set. In PyTorch, we can define a test function that takes in the model, test data loader, and the loss function. This function then goes through the test data set in batches, computes the output, and compares the output with the target to calculate the accuracy. Finally, it returns the accuracy of the model.
Use
Testing a DNN model is an essential part of the model development process. It allows us to understand how well our model is performing on the unseen data set. We can use this information to adjust the hyperparameters or the model architecture to improve its performance.
Important Points
- Testing a DNN model is crucial to evaluate its performance on the unseen data set
- In PyTorch, we can define a test function that takes in the model, test data loader, and the loss function
- The test function goes through the test data set in batches, computes the output, and compares the output with the target to calculate the accuracy
- Finally, it returns the accuracy of the model.
Summary
Testing a DNN model using PyTorch helps us understand how well our model is performing on the unseen data set. By defining a test function in PyTorch, we can compute the accuracy of our model on the test data set. It is an essential part of the model development process, and we can use this information to improve the performance of our model.