4. Convolutional Neural Networks
Activity 4.01: Building a CNN for an Image Classification Problem
Solution
- Import the required libraries:
import numpy as np import torch from torch import nn, optim import torch.nn.functional as F from torchvision import datasets import torchvision.transforms as transforms from torch.utils.data.sampler import SubsetRandomSampler from sklearn.metrics import accuracy_score import matplotlib.pyplot as plt
- Set the transformations to be performed on the data, which will be converting the data into tensors and normalizing the pixel values:
transform = \ Â Â Â Â transforms.Compose([transforms.ToTensor(), \ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â transforms.Normalize((0.5, 0.5, 0.5),\ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ...