text: # Download Fashion MNIST Dataset
  import gzip
  import os
  from urllib.request import urlretrieve
  import numpy as np
  import matplotlib.pyplot as plt
  
  def fashion_mnist():
      """
      Download compressed Fashion MNIST data to local directory, and 
      unpack data into numpy arrays. 
      
      Return (train_images, train_labels, test_images, test_labels).
  
      Args:
          None
  
      Returns:
          Tuple of (train_images, train_labels, test_images, test_labels), each
              a matrix. Rows are examples. Columns of images are pixel values.
              Columns of labels are a onehot encoding of the correct class.
      """
      url = 'http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/'
      files = ['train-images-idx3-ubyte.gz',
               'train-labels-idx1-ubyte.gz',
               't10k-images-idx3-ubyte.gz',
               't10k-labels-idx1-ubyte.gz']
      path = './' # Download data to current directory.
      os.makedirs(path, exist_ok=True) # Create path if it doesn't exist.
  
      # Download any missing files
      for file in files:
          if file not in os.listdir(path):
              urlretrieve(url + file, os.path.join(path, file))
              print("Downloaded %s to %s" % (file, path))
      
      def _images(path):
          """Return images loaded locally."""
          with gzip.open(path) as f:
              # First 16 bytes are magic_number, n_imgs, n_rows, n_cols
              pixels = np.frombuffer(f.read(), 'B', offset=16)
          return pixels.reshape(-1, 784).astype('float32') / 255
  
      def _labels(path):
          """Return labels loaded locally."""
          with gzip.open(path) as f:
              # First 8 bytes are magic_number, n_labels
              integer_labels = np.frombuffer(f.read(), 'B', offset=8)
  
          def _onehot(integer_labels):
              """Return matrix whose rows are onehot encodings of integers."""
              n_rows = len(integer_labels)
              n_cols = integer_labels.max() + 1
              onehot = np.zeros((n_rows, n_cols), dtype='uint8')
              onehot[np.arange(n_rows), integer_labels] = 1
              return onehot
  
          return _onehot(integer_labels)
  
      train_images = _images(os.path.join(path, files[0]))
      train_labels = _labels(os.path.join(path, files[1]))
      test_images = _images(os.path.join(path, files[2]))
      test_labels = _labels(os.path.join(path, files[3]))
      
      return train_images, train_labels, test_images, test_labels
  
  train_images, train_labels, test_images, test_labels = fashion_mnist()
  
  # Plot examples from dataset.
  plt.figure(1, figsize=(14,6))
  for i in range(10):
      plt.subplot(1,10, i+1)
      plt.imshow(train_images[i,:].reshape(28,28), cmap='bone')
      plt.title(f'Label: {train_labels[i].argmax()}')
      plt.xticks([])
      plt.yticks([])
  