Learn practical skills, build real-world projects, and advance your career
import torch
import numpy as np
import torchvision
from torchvision.datasets import MNIST
from torchvision.transforms import ToTensor
from torch.utils.data.sampler import SubsetRandomSampler
from torch.utils.data.dataloader import DataLoader
dataset = MNIST(root='data/',
               download=True,
               transform=ToTensor())
img, label =dataset[0]
img.shape, label
(torch.Size([1, 28, 28]), 5)
import matplotlib.pyplot as plt
plt.imshow(img[0])
<matplotlib.image.AxesImage at 0x7fc40dcf3790>
Notebook Image
img[0, 10:15, 10:15]
tensor([[0.0039, 0.6039, 0.9922, 0.3529, 0.0000],
        [0.0000, 0.5451, 0.9922, 0.7451, 0.0078],
        [0.0000, 0.0431, 0.7451, 0.9922, 0.2745],
        [0.0000, 0.0000, 0.1373, 0.9451, 0.8824],
        [0.0000, 0.0000, 0.0000, 0.3176, 0.9412]])