import matplotlib pyplot as plt from torch utils data import dataset dataloader from pil import image import torch nn functional as ###############################################################data loader class ###############################################################class loaddata dataset )def __init__ (self fname transform =none)data pd read_csv fname self np array (data iloc [: :dtype =np uint reshape (- self np array (data iloc [: ]def __len__ (self)return len(self xdef __getitem__ (self idx)img self [idxlbl self [idxreturn (img lblload the image data train_ds loaddata ('fashionmnist /fashion mnist_train csv 'test_ds loaddata ('fashionmnist /fashion mnist_test csv 'set labels dictionary labels { 'tshirt ' 'trouser ' 'pullover ' 'dress ' 'coat ' 'sandal ' 'shirt ' 'sneaker ' 'bag ' 'ankle boot 'since an image input data is generally memory intensiveit is important to partition the data set into (mini-)batches the code below defines batch size of images and initializes the pytorch data loader objects these objects will be used for efficient iteration over the data set load the data in batches batch_size train_loader torch utils data dataloader dataset =train_ds batch_size batch_size shuffle =truetest_loader torch utils data dataloader dataset =test_ds batch_size batch_size shuffle =truenextto define the network architecture in pytorch all we need to do is define an instance of the torch nn module class choosing network architecture with good generalization properties can be difficult task herewe use network with two convolution layers (defined in the cnn_layer block) kerneland three hidden layers (defined in the flat_layer blocksince there are ten possible output labelsthe output layer has ten nodes more specificallythe first and the second convolution layers have and output channels combining this with the definition of the kernelwe conclude that the size