mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-06 20:43:11 +02:00
feat: add plotting
This commit is contained in:
parent
fa10997a1d
commit
fe23eadb1a
@ -2,6 +2,7 @@ import torch
|
|||||||
import torch.nn as nn
|
import torch.nn as nn
|
||||||
import torch.optim as optim
|
import torch.optim as optim
|
||||||
from torchvision import datasets, transforms
|
from torchvision import datasets, transforms
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
# Set random seed for reproducibility
|
# Set random seed for reproducibility
|
||||||
torch.manual_seed(42)
|
torch.manual_seed(42)
|
||||||
@ -43,6 +44,11 @@ criterion = nn.CrossEntropyLoss()
|
|||||||
# Optimizer
|
# Optimizer
|
||||||
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
|
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
|
||||||
|
|
||||||
|
# Lists to store loss and accuracy values
|
||||||
|
loss_values = []
|
||||||
|
train_acc_values = []
|
||||||
|
val_acc_values = []
|
||||||
|
|
||||||
# Training loop
|
# Training loop
|
||||||
for epoch in range(num_epochs):
|
for epoch in range(num_epochs):
|
||||||
for batch_idx, (data, targets) in enumerate(train_loader):
|
for batch_idx, (data, targets) in enumerate(train_loader):
|
||||||
@ -58,9 +64,8 @@ for epoch in range(num_epochs):
|
|||||||
loss.backward()
|
loss.backward()
|
||||||
optimizer.step()
|
optimizer.step()
|
||||||
|
|
||||||
# Print loss value for every learning step
|
# Append loss value for every learning step
|
||||||
if (batch_idx+1) % 100 == 0:
|
loss_values.append(loss.item())
|
||||||
print(f'Epoch [{epoch+1}/{num_epochs}], Step [{batch_idx+1}/{len(train_loader)}], Loss: {loss.item():.4f}')
|
|
||||||
|
|
||||||
# Calculate accuracy on train set after each epoch
|
# Calculate accuracy on train set after each epoch
|
||||||
correct = 0
|
correct = 0
|
||||||
@ -73,7 +78,7 @@ for epoch in range(num_epochs):
|
|||||||
correct += (predicted == targets).sum().item()
|
correct += (predicted == targets).sum().item()
|
||||||
|
|
||||||
train_accuracy = 100 * correct / total
|
train_accuracy = 100 * correct / total
|
||||||
print(f'Accuracy on Train Set after Epoch {epoch+1}: {train_accuracy:.2f}%')
|
train_acc_values.append(train_accuracy)
|
||||||
|
|
||||||
# Calculate accuracy on validation set after each epoch
|
# Calculate accuracy on validation set after each epoch
|
||||||
correct = 0
|
correct = 0
|
||||||
@ -86,7 +91,28 @@ for epoch in range(num_epochs):
|
|||||||
correct += (predicted == targets).sum().item()
|
correct += (predicted == targets).sum().item()
|
||||||
|
|
||||||
validation_accuracy = 100 * correct / total
|
validation_accuracy = 100 * correct / total
|
||||||
print(f'Accuracy on Validation Set after Epoch {epoch+1}: {validation_accuracy:.2f}%')
|
val_acc_values.append(validation_accuracy)
|
||||||
print('---')
|
|
||||||
|
|
||||||
# Conclusions and observations can be included in the report
|
# Print loss value for every learning step
|
||||||
|
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss_values[-1]:.4f}, Train Accuracy: {train_accuracy:.2f}%, Validation Accuracy: {validation_accuracy:.2f}%')
|
||||||
|
|
||||||
|
# Plot the loss value for every learning step
|
||||||
|
plt.plot(loss_values)
|
||||||
|
plt.xlabel('Learning Step')
|
||||||
|
plt.ylabel('Loss')
|
||||||
|
plt.title('Loss Value')
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
# Plot the accuracy on train set after each epoch
|
||||||
|
plt.plot(train_acc_values)
|
||||||
|
plt.xlabel('Epoch')
|
||||||
|
plt.ylabel('Train Accuracy')
|
||||||
|
plt.title('Accuracy on Train Set')
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
# Plot the accuracy on validation set after each epoch
|
||||||
|
plt.plot(val_acc_values)
|
||||||
|
plt.xlabel('Epoch')
|
||||||
|
plt.ylabel('Validation Accuracy')
|
||||||
|
plt.title('Accuracy on Validation Set')
|
||||||
|
plt.show()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user