Python - PyTorch - MNIST Home : www.sharetechnote.com |
PyTorch - MNIST
Downloading the image
import torch import torchvision from torchvision import datasets, transforms from torch import nn, optim from time import time import matplotlib.pyplot as plt import numpy as np
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)), ]); dataset = datasets.MNIST(r'..\input\MNIST', download=True, train=True, transform=transform);
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to ..\input\MNIST\MNIST\raw\train-images-idx3-ubyte.gz 0.0%0.1%0.2%0.2%0.3%0.4%0.5%0.6%0.7%0.7%0.8%0.9%1.0%1.1%1.2%1.2%1.3%1.4%1.5%1.6%1.7%1.7%1.8% 1.9%2.0%2.1%2.1%2.2%2.3%2.4%2.5%2.6%2.6%2.7%2.8%2.9%3.0%3.1%3.1%3.2%3.3%3.4%3.5%3.6%3.6%3 ..... Extracting ..\input\MNIST\MNIST\raw\train-images-idx3-ubyte.gz to ..\input\MNIST\MNIST\raw Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to ..\input\MNIST\MNIST\raw\train-labels- idx1-ubyte.gz 0.0%28.4%56.7%85.1%113.5%Extracting ..\input\MNIST\MNIST\raw\train-labels-idx1-ubyte.gz to ..\input\MNIST\ MNIST\raw Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to ..\input\MNIST\MNIST\raw\t10k- images-idx3-ubyte.gz 93.4%93.9%94.4%94.9%95.4%95.9%96.4%96.9%97.4%97.9%98.4%98.9%99.4%99.9%100.4%Extracting ..\input\ MNIST\MNIST\raw\t10k-images-idx3-ubyte.gz to ..\input\MNIST\MNIST\raw Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to ..\input\MNIST\MNIST\raw\t10k-labels- idx1-ubyte.gz 0.0%180.4%Extracting ..\input\MNIST\MNIST\raw\t10k-labels-idx1-ubyte.gz to ..\input\MNIST\MNIST\raw Processing... Done!
Loading the Data
dataloader = torch.utils.data.DataLoader(dataset, batch_size=10, shuffle=False);
Extracting the Individual Data
dataiter = iter(dataloader) images, labels = dataiter.next()
print(images.shape) print(labels.shape) ==> torch.Size([10, 1, 28, 28]) torch.Size([10])
plt.imshow(images[0].numpy().squeeze(), cmap='gray_r');
print(images[0]) tensor([[[-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000], [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000], ....... [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000], [-1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000, -1.0000]]])
print(images[0].numpy().squeeze()) [[-1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. ] [-1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. ] ...... [-1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. ] [-1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1. ]]
Referene :
[1] Building Your First PyTorch Solution
|