text: import numpy as np
  import random
  import scipy
  from tensorflow.examples.tutorials.mnist import input_data
  import matplotlib.pyplot as plt
  
  
  def load_data(mode='train'):
      """
      Function to (download and) load the MNIST data
      :param mode: train or test
      :return: images and the corresponding labels
      """
      mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
      if mode == 'train':
          x_train, y_train, x_valid, y_valid = mnist.train.images, mnist.train.labels, \
                                               mnist.validation.images, mnist.validation.labels
          x_train, _ = reformat(x_train, y_train)
          x_valid, _ = reformat(x_valid, y_valid)
          return x_train, y_train, x_valid, y_valid
      elif mode == 'test':
          x_test, y_test = mnist.test.images, mnist.test.labels
          x_test, _ = reformat(x_test, y_test)
      return x_test, y_test
  
  
  def randomize(x, y):
      """ Randomizes the order of data samples and their corresponding labels"""
      permutation = np.random.permutation(y.shape[0])
      shuffled_x = x[permutation, :, :, :]
      shuffled_y = y[permutation]
      return shuffled_x, shuffled_y
  
  
  def reformat(x, y):
      """
      Reformats the data to the format acceptable for convolutional layers
      :param x: input array
      :param y: corresponding labels
      :return: reshaped input and labels
      """
      img_size, num_ch, num_class = int(np.sqrt(x.shape[-1])), 1, len(np.unique(np.argmax(y, 1)))
      dataset = x.reshape((-1, img_size, img_size, num_ch)).astype(np.float32)
      labels = (np.arange(num_class) == y[:, None]).astype(np.float32)
      return dataset, labels
  
  
  def get_next_batch(x, y, start, end):
      x_batch = x[start:end]
      y_batch = y[start:end]
      return x_batch, y_batch
  
  def plot_images(images, cls_true, cls_pred=None, title=None):
      """
      Create figure with 3x3 sub-plots.
      :param images: array of images to be plotted, (9, img_h*img_w)
      :param cls_true: corresponding true labels (9,)
      :param cls_pred: corresponding true labels (9,)
      """
      fig, axes = plt.subplots(3, 3, figsize=(9, 9))
      fig.subplots_adjust(hspace=0.3, wspace=0.3)
      for i, ax in enumerate(axes.flat):
          # Plot image.
          ax.imshow(np.squeeze(images[i]), cmap='binary')
  
          # Show true and predicted classes.
          if cls_pred is None:
              ax_title = "True: {0}".format(cls_true[i])
          else:
              ax_title = "True: {0}, Pred: {1}".format(cls_true[i], cls_pred[i])
  
          ax.set_title(ax_title)
  
          # Remove ticks from the plot.
          ax.set_xticks([])
          ax.set_yticks([])
  
      if title:
          plt.suptitle(title, size=20)
      plt.show(block=False)
  
  
  def plot_example_errors(images, cls_true, cls_pred, title=None):
      """
      Function for plotting examples of images that have been mis-classified
      :param images: array of all images, (#imgs, img_h*img_w)
      :param cls_true: corresponding true labels, (#imgs,)
      :param cls_pred: corresponding predicted labels, (#imgs,)
      """
      # Negate the boolean array.
      incorrect = np.logical_not(np.equal(cls_pred, cls_true))
  
      # Get the images from the test-set that have been
      # incorrectly classified.
      incorrect_images = images[incorrect]
  
      # Get the true and predicted classes for those images.
      cls_pred = cls_pred[incorrect]
      cls_true = cls_true[incorrect]
  
      # Plot the first 9 images.
      plot_images(images=incorrect_images[0:9],
                  cls_true=cls_true[0:9],
                  cls_pred=cls_pred[0:9],
                  title=title)
  