<jupyter_start><jupyter_text>MLP for image classification using PyTorchIn this section, we follow Chap. 7 of the [Deep Learning With PyTorch](https://pytorch.org/assets/deep-learning/Deep-Learning-with-PyTorch.pdf) book, and illustrate how to fit an MLP to a two-class version of CIFAR. (We modify the code from [here](https://github.com/deep-learning-with-pytorch/dlwpt-code/tree/master/p1ch7).)<jupyter_code>import sklearn
import scipy
import scipy.optimize
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import itertools
import time
from functools import partial

import os

import numpy as np
from scipy.special import logsumexp
np.set_printoptions(precision=3)





import torch
import torch.nn as nn
import torchvision
print("torch version {}".format(torch.__version__))
if torch.cuda.is_available():
  print(torch.cuda.get_device_name(0))
  print("current device {}".format(torch.cuda.current_device()))
else:
  print("Torch cannot find GPU")

def set_seed(seed):
  np.random.seed(seed)
  torch.manual_seed(seed)
  torch.cuda.manual_seed_all(seed)

use_cuda = torch.cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")
#torch.backends.cudnn.benchmark = True<jupyter_output>torch version 1.8.0+cu101
Tesla V100-SXM2-16GB
current device 0<jupyter_text>Get the CIFAR dataset<jupyter_code>from torchvision import datasets

folder = 'data'
cifar10 = datasets.CIFAR10(folder, train=True, download=True)
cifar10_val = datasets.CIFAR10(folder, train=False, download=True)
print(type(cifar10))
print(type(cifar10).__mro__) # module resolution order shows class hierarchy

print(len(cifar10))
img, label = cifar10[99]
print(type(img))
print(img)
plt.imshow(img)
plt.show()

class_names = ['airplane','automobile','bird','cat','deer',
               'dog','frog','horse','ship','truck']

fig = plt.figure(figsize=(8,3))
num_classes = 10
for i in range(num_classes):
    ax = fig.add_subplot(2, 5, 1 + i, xticks=[], yticks=[])
    ax.set_title(class_names[i])
    img = next(img for img, label in cifar10 if label == i)
    plt.imshow(img)
plt.show()<jupyter_output><empty_output><jupyter_text>Convert to tensors<jupyter_code># Now we want to convert this to a tensor
from torchvision import transforms

to_tensor = transforms.ToTensor()

img, label = cifar10[99]
img_t = to_tensor(img)
print(type(img))
#print(img.shape)
print(type(img_t))
print(img_t.shape) # channels * height * width, here channels=3 (RGB)


# transform the whole dataset to tensors
cifar10 = datasets.CIFAR10(folder, train=True, download=False,
                          transform=transforms.ToTensor())
img, label = cifar10[99]
print(type(img))
print(img.min(), img.max()) # pixel values are rescaled to 0..1

plt.imshow(img.permute(1, 2, 0)) # matplotlib expects H*W*C
plt.show()<jupyter_output><class 'torch.Tensor'>
tensor(0.) tensor(1.)<jupyter_text>Standardize the inputsWe standardize the features by  computing the mean and std of each channel, averaging across all pixels and all images. This will help optimization.<jupyter_code># we load the whole training set as a batch, of size 3*H*W*N

imgs = torch.stack([img for img, _ in cifar10], dim=3)
print(imgs.shape)

imgs_flat =  imgs.view(3, -1) # reshape by keeping first 3 channels, but flatten all others 
print(imgs_flat.shape)
mu = imgs_flat.mean(dim=1) # average over second dimension (not channel)
sigma = imgs_flat.std(dim=1)
print(mu)
print(sigma)
cifar10 = datasets.CIFAR10(folder, train=True, download=False,
                          transform=transforms.Compose([
                              transforms.ToTensor(),
                              transforms.Normalize(mu, sigma)
                          ]))

cifar10_val = datasets.CIFAR10(folder, train=False, download=False,
                          transform=transforms.Compose([
                              transforms.ToTensor(),
                              transforms.Normalize(mu, sigma),
                          ]))
# rescaled data is harder to visualize
img, _ = cifar10[99]

plt.imshow(img.permute(1, 2, 0))
plt.show()<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).<jupyter_text>Create two-class version of datasetWe extract data which correspond to airplane or bird.The result object is a list of pairs.This "acts like" an object of type torch.utilts.data.dataset.Dataset, since it implements the len() and item index methods.<jupyter_code>class_names = ['airplane','automobile','bird','cat','deer',
               'dog','frog','horse','ship','truck']

label_map = {0: 0, 2: 1} # 0(airplane->0, 2(bird)->1
cifar2 = [(img, label_map[label]) for img, label in cifar10 if label in [0, 2]]
cifar2_val = [(img, label_map[label]) for img, label in cifar10_val if label in [0, 2]]
print(len(cifar2))
print(len(cifar2_val))<jupyter_output>10000
2000
{'fraction_non_alphanumeric': 0.12012856568903174, 'fraction_numerical': 0.02571313780634793, 'mean_word_length': 4.296808510638298, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '556323', 'license': 'MIT', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1688, 'n_words': 444, 'path': 'book1/supplements/mlp_cifar_pytorch.ipynb', 'repo_name': 'Garvit9000c/pyprobmlm', 'size': '4978'}