Another example where it is useful is shown here:

  
      github.com
  
  
    pkdn/pytorch-smoothgrad/blob/master/lib/gradients.py#L79 31

    one_hot = np.zeros((1, output.size()[-1]), dtype=np.float32)
    one_hot[0][index] = 1
    if self.cuda:
        one_hot = Variable(torch.from_numpy(one_hot).cuda(), requires_grad=True)
    else:
        one_hot = Variable(torch.from_numpy(one_hot), requires_grad=True)
    one_hot = torch.sum(one_hot * output)


    if x_plus_noise.grad is not None:
        x_plus_noise.grad.data.zero_()
    one_hot.backward(retain_variables=True)


    grad = x_plus_noise.grad.data.cpu().numpy()


    if self.magnitutde:
        total_gradients += (grad * grad)
    else:
        total_gradients += grad
    #if self.visdom:


avg_gradients = total_gradients[0, :, :, :] / self.n_samples



  
  
    
    
  
  


Here the input image is treated as variables, and a gradient is calculated to see which parts in the image will have most influence on the global classification decision.
This is useful when you trained a classifier without any localization and you still want to see some localization information.