text: import matplotlib.pyplot as plt
  import numpy as np
  from torch import nn, optim
  from torch.autograd import Variable
  import torch
  
  from torch import nn, optim
  import torch.nn.functional as F
  
  
  
  def test_network(net, trainloader):
  
      criterion = nn.MSELoss()
      optimizer = optim.Adam(net.parameters(), lr=0.001)
  
      dataiter = iter(trainloader)
      images, labels = dataiter.next()
  
      # Create Variables for the inputs and targets
      inputs = Variable(images)
      targets = Variable(images)
  
      # Clear the gradients from all Variables
      optimizer.zero_grad()
  
      # Forward pass, then backward pass, then update weights
      output = net.forward(inputs)
      loss = criterion(output, targets)
      loss.backward()
      optimizer.step()
  
      return True
  
  
  def imshow(image, ax=None, title=None, normalize=True):
      """Imshow for Tensor."""
      if ax is None:
          fig, ax = plt.subplots()
      image = image.numpy().transpose((1, 2, 0))
  
      if normalize:
          mean = np.array([0.485, 0.456, 0.406])
          std = np.array([0.229, 0.224, 0.225])
          image = std * image + mean
          image = np.clip(image, 0, 1)
  
      ax.imshow(image)
      ax.spines['top'].set_visible(False)
      ax.spines['right'].set_visible(False)
      ax.spines['left'].set_visible(False)
      ax.spines['bottom'].set_visible(False)
      ax.tick_params(axis='both', length=0)
      ax.set_xticklabels('')
      ax.set_yticklabels('')
  
      return ax
  
  
  def view_recon(img, recon):
      ''' Function for displaying an image (as a PyTorch Tensor) and its
          reconstruction also a PyTorch Tensor
      '''
  
      fig, axes = plt.subplots(ncols=2, sharex=True, sharey=True)
      axes[0].imshow(img.numpy().squeeze())
      axes[1].imshow(recon.data.numpy().squeeze())
      for ax in axes:
          ax.axis('off')
          ax.set_adjustable('box-forced')
  
  def view_classify(img, ps, version="MNIST"):
      ''' Function for viewing an image and it's predicted classes.
      '''
      ps = ps.data.numpy().squeeze()
  
      fig, (ax1, ax2) = plt.subplots(figsize=(6,9), ncols=2)
      ax1.imshow(img.resize_(1, 28, 28).numpy().squeeze())
      ax1.axis('off')
      ax2.barh(np.arange(10), ps)
      ax2.set_aspect(0.1)
      ax2.set_yticks(np.arange(10))
      if version == "MNIST":
          ax2.set_yticklabels(np.arange(10))
      elif version == "Fashion":
          ax2.set_yticklabels(['T-shirt/top',
                              'Trouser',
                              'Pullover',
                              'Dress',
                              'Coat',
                              'Sandal',
                              'Shirt',
                              'Sneaker',
                              'Bag',
                              'Ankle Boot'], size='small');
      ax2.set_title('Class Probability')
      ax2.set_xlim(0, 1.1)
  
      plt.tight_layout()
  
  
  def matplotlib_imshow(img, one_channel=False):
      if one_channel:
          img = img.mean(dim=0)
      img = img / 2 + 0.5     # unnormalize
      npimg = img.cpu().numpy()
      if one_channel:
          plt.imshow(npimg, cmap="Greys")
      else:
          plt.imshow(np.transpose(npimg, (1, 2, 0)))
  
  
  def images_to_probs(net, images):
      '''
      Generates predictions and corresponding probabilities from a trained
      network and a list of images
      '''
      output = net(images)
      # convert output probabilities to predicted class
      _, preds_tensor = torch.max(output, 1)
      preds = np.squeeze(preds_tensor.cpu().numpy())
      return preds, [F.softmax(el, dim=0)[i].item() for i, el in zip(preds, output.cpu())]
  
  def plot_classes_preds(net, images, labels):
      '''
      Generates matplotlib Figure using a trained network, along with images
      and labels from a batch, that shows the network's top prediction along
      with its probability, alongside the actual label, coloring this
      information based on whether the prediction was correct or not.
      Uses the "images_to_probs" function.
      '''
      preds, probs = images_to_probs(net, images)
  
      classes = ('Dog', 'Cat')
  
      # plot the images in the batch, along with predicted and true labels
      fig = plt.figure(figsize=(12, 48))
      for idx in np.arange(4):
          ax = fig.add_subplot(1, 4, idx+1, xticks=[], yticks=[])
          matplotlib_imshow(images[idx], one_channel=True)
          ax.set_title("{0}, {1:.1f}%\n(label: {2})".format(
              classes[preds[idx]],
              probs[idx] * 100.0,
              classes[labels[idx]]),
                      color=("green" if preds[idx]==labels[idx].item() else "red"))
      return fig