代写data编程、代做Python程序语言
Lab 2: Neural Networks for Image
Classification
Duration: 2 hours
Tools:
• Jupyter Notebook
• IDE: PyCharm==2024.2.3 (or any IDE of your choice)
• Python: 3.12
• Libraries:
o PyTorch==2.4.0
o TorchVision==0.19.0
o Matplotlib==3.9.2
Learning Objectives:
• Understand the basic architecture of a neural network.
• Load and explore the CIFAR-10 dataset.
• Implement and train a neural network, individualized by your QMUL ID.
• Verify machine learning concepts such as accuracy, loss, and evaluation metrics
by running predefined code.
Lab Outline:
In this lab, you will implement a simple neural network model to classify images from
the CIFAR-10 dataset. The task will be individualized based on your QMUL ID to ensure
unique configurations for each student.
1. Task 1: Understanding the CIFAR-10 Dataset
• The CIFAR-10 dataset consists of 60,000 32x32 color images categorized into 10
classes (airplanes, cars, birds, cats, deer, dogs, frogs, horses, ships, and trucks).
• The dataset is divided into 50,000 training images and 10,000 testing images.
• You will load the CIFAR-10 dataset using PyTorch’s built-in torchvision library.
Step-by-step Instructions:
1. Open the provided Jupyter Notebook.
2. Load and explore the CIFAR-10 dataset using the following code:
import torchvision.transforms as transforms
import torchvision.datasets as datasets
# Basic transformations for the CIFAR-10 dataset
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])
# Load the CIFAR-10 dataset
dataset = datasets.CIFAR10(root='./data', train=True,
download=True, transform=transform)
2. Task 2: Individualized Neural Network Implementation, Training, and Test
You will implement a neural network model to classify images from the CIFAR-10
dataset. However, certain parts of the task will be individualized based on your QMUL
ID. Follow the instructions carefully to ensure your model’s configuration is unique.
Step 1: Dataset Split Based on Your QMUL ID
You will use the last digit of your QMUL ID to define the training-validation split:
• If your ID ends in 0-4: use a 70-30 split (70% training, 30% validation).
• If your ID ends in 5-9: use an 80-20 split (80% training, 20% validation).
Code:
from torch.utils.data import random_split
# Set the student's last digit of the ID (replace with
your own last digit)
last_digit_of_id = 7 # Example: Replace this with the
last digit of your QMUL ID
# Define the split ratio based on QMUL ID
split_ratio = 0.7 if last_digit_of_id <= 4 else 0.8
# Split the dataset
train_size = int(split_ratio * len(dataset))
val_size = len(dataset) - train_size
train_dataset, val_dataset = random_split(dataset,
[train_size, val_size])
# DataLoaders
from torch.utils.data import DataLoader
batch_size = 32 + last_digit_of_id # Batch size is 32 +
last digit of your QMUL ID
train_loader = DataLoader(train_dataset,
batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_dataset,
batch_size=batch_size, shuffle=False)
print(f"Training on {train_size} images, Validating on
{val_size} images.")
Step 2: Predefined Neural Network Model
You will use a predefined neural network architecture provided in the lab. The model’s
hyperparameters will be customized based on your QMUL ID.
1. Learning Rate: Set the learning rate to 0.001 + (last digit of your QMUL ID *
0.0001).
2. Number of Epochs: Train your model for 10 + (last digit of your QMUL ID)
epochs.
Code:
import torch
import torch.optim as optim
# Define the model
model = torch.nn.Sequential(
torch.nn.Flatten(),
torch.nn.Linear(32*32*3, 512),
torch.nn.ReLU(),
torch.nn.Linear(512, 10) # 10 output classes for
CIFAR-10
)
# Loss function and optimizer
criterion = torch.nn.CrossEntropyLoss()
# Learning rate based on QMUL ID
learning_rate = 0.001 + (last_digit_of_id * 0.0001)
optimizer = optim.Adam(model.parameters(),
lr=learning_rate)
# Number of epochs based on QMUL ID
num_epochs = 100 + last_digit_of_id
print(f"Training for {num_epochs} epochs with learning
rate {learning_rate}.")
Step 3: Model Training and Evaluation
Use the provided training loop to train your model and evaluate it on the validation set.
Track the loss and accuracy during the training process.
Expected Output: For training with around 100 epochs, it may take 0.5~1 hour to finish.
You may see a lower accuracy, especially for the validation accuracy, due to the lower
number of epochs or the used simple neural network model, etc. If you are interested,
you can find more advanced open-sourced codes to test and improve the performance.
In this case, it may require a long training time on the CPU-based device.
Code:
# Training loop
train_losses = []
train_accuracies = []
val_accuracies = []
for epoch in range(num_epochs):
model.train()
running_loss = 0.0
correct = 0
total = 0
for inputs, labels in train_loader:
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()

running_loss += loss.item()
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
train_accuracy = 100 * correct / total
print(f"Epoch {epoch+1}/{num_epochs}, Loss:
{running_loss:.4f}, Training Accuracy:
{train_accuracy:.2f}%")

# Validation step
model.eval()
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in val_loader:
outputs = model(inputs)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()

val_accuracy = 100 * correct / total
print(f"Validation Accuracy after Epoch {epoch + 1}:
{val_accuracy:.2f}%")
train_losses.append(running_loss)
train_accuracies.append(train_accuracy)
val_accuracies.append(val_accuracy)
Task 3: Visualizing and Analyzing the Results
Visualize the results of the training and validation process. Generate the following plots
using Matplotlib:
• Training Loss vs. Epochs.
• Training and Validation Accuracy vs. Epochs.
Code for Visualization:
import matplotlib.pyplot as plt
# Plot Loss
plt.figure()
plt.plot(range(1, num_epochs + 1), train_losses,
label="Training Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.title("Training Loss")
plt.legend()
plt.show()
# Plot Accuracy
plt.figure()
plt.plot(range(1, num_epochs + 1), train_accuracies,
label="Training Accuracy")
plt.plot(range(1, num_epochs + 1), val_accuracies,
label="Validation Accuracy")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.title("Training and Validation Accuracy")
plt.legend()
plt.show()
Lab Report Submission and Marking Criteria
After completing the lab, you need to submit a report that includes:
1. Individualized Setup (20/100):
o Clearly state the unique configurations used based on your QMUL ID,
including dataset split, number of epochs, learning rate, and batch size.
2. Neural Network Architecture and Training (30/100):
o Provide an explanation of the model architecture (i.e., the number of input
layer, hidden layer, and output layer, activation function) and training
procedure (i.e., the used optimizer).
o Include the plots of training loss, training and validation accuracy.
3. Results Analysis (30/100):
o Provide analysis of the training and validation performance.
o Reflect on whether the model is overfitting or underfitting based on the
provided results.
4. Concept Verification (20/100):
o Answer the provided questions below regarding machine learning
concepts.
(1) What is overfitting issue? List TWO methods for addressing the overfitting
issue.
(2) What is the role of loss function? List TWO representative loss functions.

热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图