Loading MNIST dataset Using PyTorch
In this examples we will explore to load mnist dataset pytorch example. PyTorch offers a similar utility through torchvision.datasets, which is very convenient, especially when combined with torchvision.transforms to perform basic preprocessing like converting images to tensor format.
import matplotlib.pyplot as plt
import torch
from torchvision import datasets, transforms
# Define the transformation to convert images to PyTorch tensors
transform = transforms.Compose([transforms.ToTensor()])
# Load the MNIST dataset with the specified transformation
mnist_pytorch = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
# Create a DataLoader to load the dataset in batches
train_loader_pytorch = torch.utils.data.DataLoader(mnist_pytorch, batch_size=1, shuffle=False)
# Create a figure to display the images
plt.figure(figsize=(15, 3))
# Print the first few images in a row
for i, (image, label) in enumerate(train_loader_pytorch):
if i < 5: # Print the first 5 samples
plt.subplot(1, 5, i + 1)
plt.imshow(image[0].squeeze(), cmap='gray')
plt.title(f"Label: {label.item()}")
plt.axis('off')
else:
break # Exit the loop after printing 5 samples
plt.tight_layout()
plt.show()
Output:
MNIST Dataset : Practical Applications Using Keras and PyTorch
The MNIST dataset is a popular dataset used for training and testing in the field of machine learning for handwritten digit recognition. The article aims to explore the MNIST dataset, its characteristics and its significance in machine learning.
Table of Content
- What is MNIST Dataset?
- Structure of MNIST dataset
- Origin of the MNIST Dataset
- Methods to load MNIST dataset in Python
- Loading MNIST dataset using TensorFlow/Keras
- Loading MNIST dataset Using PyTorch
- Significance of MNIST in Machine Learning
- Applications of MNIST
Contact Us