Skip to content

Instantly share code, notes, and snippets.

Created January 13, 2019 15:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vihar/d5775e3c81d51c254dcc2ff1436b5321 to your computer and use it in GitHub Desktop.
# Load the libraries
import torch
import numpy as np
from torchvision import datasets
import torchvision.transforms as transforms
# Set the parameters
num_workers = 0
batch_size = 20
# Converting the Images to tensors using Transforms
transform = transforms.ToTensor()
train_data = datasets.MNIST(root='data', train=True,
download=True, transform=transform)
test_data = datasets.MNIST(root='data', train=False,
download=True, transform=transform)
# Loading the Data
train_loader =, batch_size=batch_size,
test_loader =, batch_size=batch_size,
import matplotlib.pyplot as plt
%matplotlib inline
dataiter = iter(train_loader)
images, labels =
images = images.numpy()
# Peeking into dataset
fig = plt.figure(figsize=(25, 4))
for image in np.arange(20):
ax = fig.add_subplot(2, 20/2, image+1, xticks=[], yticks=[])
ax.imshow(np.squeeze(images[image]), cmap='gray')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment