I’m currently working on training a MobileNet V3 Small model for object detection using PyTorch. I’ve successfully implemented the training loop, but I’m unsure how to write the validation loop correctly. I need help with writing a validation loop. Below is my current training loop:
# Set the loss function
criterion = nn.CrossEntropyLoss()
# Set the optimizer
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
# Set the number of epochs
num_epochs = 10
# Set the batch size
batch_size = 32
# Set the number of iterations per epoch
num_iterations = len(dataset) // batch_size
# Training loop
for epoch in range(num_epochs):
running_loss = 0.0
running_accuracy = 0.0
for i, (inputs, labels) in enumerate(dataloader):
# Move inputs and labels to the device
inputs = inputs.to(device)
labels = labels.to(device)
# Zero the gradients
optimizer.zero_grad()
# Forward pass
outputs = model(inputs)
# Compute the loss
loss = criterion(outputs, labels)
# Backward pass
loss.backward()
# Update the weights
optimizer.step()
# Compute the accuracy
predictions = torch.argmax(outputs, dim=1)
accuracy = torchmetrics.Accuracy()(predictions, labels)
# Update the running loss and accuracy
running_loss += loss.item()
running_accuracy += accuracy.item()
# Print the progress every 100 iterations
if (i + 1) % 100 == 0:
print(f"Epoch [{epoch+1}/{num_epochs}], Iteration [{i+1}/{num_iterations}], Loss: {running_loss/100:.4f}, Accuracy: {running_accuracy/100:.4f}")
running_loss = 0.0
running_accuracy = 0.0
New contributor
V_G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.