]>
vgcfreebox.myrthtech.pt Git - ue-rnap-aerossol.git/blob - torch-randomdata-example-MNIST.py
1 # ----------------------------------------------
2 # 💡 Cell 1: Setup and Imports
3 # ----------------------------------------------
5 # 1. Import Core Libraries
8 import torch
.optim
as optim
9 from torch
.utils
.data
import DataLoader
10 from torchvision
import datasets
, transforms
11 import matplotlib
.pyplot
as plt
14 # 2. Device Configuration (The most crucial check!)
15 # This automatically detects and selects the GPU if available.
16 device
= torch
.device("cuda" if torch
.cuda
.is_available() else "cpu")
17 print(f
"✅ Successfully initialized. Using device: {device}")
19 # Optional: Check GPU details (Good for debugging)
20 if device
.type == 'cuda':
21 print(f
"GPU Name: {torch.cuda.get_device_name(0)}")
23 # ----------------------------------------------
24 # 💡 Cell 2: Data Loading and Transformations
25 # ----------------------------------------------
27 # Define the preprocessing steps
28 transform
= transforms
.Compose([
29 transforms
.ToTensor(), # Converts the image to a Tensor
30 transforms
.Normalize((0.5,), (0.5,)) # Normalizes pixel values (0 to 1)
33 # Download and Load the Dataset (Train set)
34 train_dataset
= datasets
.MNIST(root
='./data', train
=True, download
=True, transform
=transform
)
36 # Create the DataLoader (handles batching and shuffling)
38 train_loader
= DataLoader(dataset
=train_dataset
, batch_size
=BATCH_SIZE
, shuffle
=True)
40 # Repeat for the Test/Validation set
41 test_dataset
= datasets
.MNIST(root
='./data', train
=False, download
=True, transform
=transform
)
42 test_loader
= DataLoader(dataset
=test_dataset
, batch_size
=BATCH_SIZE
, shuffle
=False)
44 print("✅ Data loaded and prepared successfully!")
46 # ----------------------------------------------
47 # 💡 Cell 3: Model Definition and GPU Transfer
48 # ----------------------------------------------
50 # Define the CNN Model Architecture
51 class SimpleCNN(nn
.Module
):
53 super(SimpleCNN
, self
).__init
__()
54 # Convolutional layer: 1 channel in, 16 channels out, 3x3 kernel
55 self
.conv1
= nn
.Sequential(
56 nn
.Conv2d(1, 16, kernel_size
=3, stride
=1, padding
=1),
58 nn
.MaxPool2d(kernel_size
=2) # Halves the spatial dimensions
60 # Second convolutional layer
61 self
.conv2
= nn
.Sequential(
62 nn
.Conv2d(16, 32, kernel_size
=3, stride
=1, padding
=1),
64 nn
.MaxPool2d(kernel_size
=2)
66 # Fully connected layer (Calculate input size: 32 channels * 7 * 7)
67 self
.fc
= nn
.Linear(32 * 7 * 7, 10)
72 # Flatten the tensor for the linear layer
73 x
= x
.view(x
.size(0), -1)
77 # Initialize the model
80 # CRITICAL STEP: Move the entire model's parameters to the GPU
83 print("✅ Model defined and weights transferred to the GPU!")
86 # ----------------------------------------------
87 # 💡 Cell 4: The Training Loop
88 # ----------------------------------------------
90 # Setup hyperparameters
94 # Loss function and Optimizer
95 criterion
= nn
.CrossEntropyLoss()
96 optimizer
= optim
.Adam(model
.parameters(), lr
=LEARNING_RATE
)
98 # Store history for plotting
102 print("🚀 Starting Training...")
104 for epoch
in range(NUM_EPOCHS
):
105 # Set model to training mode
109 for batch_idx
, data
in enumerate(train_loader
):
111 # CRITICAL: Move data (inputs and labels) to the GPU!
112 images
= data
[0].to(device
)
113 labels
= data
[1].to(device
)
116 optimizer
.zero_grad()
119 outputs
= model(images
)
122 loss
= criterion(outputs
, labels
)
124 # 4. Backward Pass (The CUDA magic happens here)
125 # PyTorch automatically handles the graph computation on the GPU.
128 # 5. Optimize (Updates the weights)
131 total_loss
+= loss
.item()
133 # Calculate average loss for the epoch
134 avg_loss
= total_loss
/ len(train_loader
)
135 loss_history
.append(avg_loss
)
136 print(f
"Epoch {epoch+1}/{NUM_EPOCHS} | Loss: {avg_loss:.4f}")
138 print("🎉 Training Complete!")
141 # ----------------------------------------------
142 # 💡 Cell 5: Testing and Visualization
143 # ----------------------------------------------
145 # Set model to evaluation mode (disables dropout, etc.)
150 with torch
.no_grad(): # Context manager that disables gradient tracking (saves memory)
151 for data
in test_loader
:
152 # CRITICAL: Move data to the GPU
153 images
= data
[0].to(device
)
154 labels
= data
[1].to(device
)
156 outputs
= model(images
)
158 # Get the index of the highest score (the predicted class)
159 _
, predicted
= torch
.max(outputs
.data
, 1)
161 total
+= labels
.size(0)
162 correct
+= (predicted
.eq(labels
.view_as(predicted
))).sum().item()
164 accuracy
= 100 * correct
/ total
165 print(f
"\n🌟 Final Test Accuracy: {accuracy:.2f}%")
168 # Visualization (Highly recommended in a Jupyter environment)
169 plt
.figure(figsize
=(12, 5))
173 plt
.plot(loss_history
, marker
='o')
174 plt
.title("Training Loss Over Epochs")
178 # Plot 2: Conceptual Improvement (You would track accuracy here)
180 plt
.plot([0] * len(loss_history
), label
="Dummy Acc.") # Placeholder for accuracy plot
181 plt
.title("Model Performance")
183 plt
.ylabel("Accuracy (%)")
188 # Optional: Save the best model weights
189 torch
.save(model
.state_dict(), 'mnist_cnn_model.pth')
190 print("\nModel weights saved to 'mnist_cnn_model.pth'")