class_docstr: DIPVAEExplainer can be used to visualize the changes in the latent space of Disentangled Inferred Prior-VAE
  or DIPVAE [#1]_. This model is a Variational Autoencoder [#2]_ variant that leads to a
  disentangled latent space. This is achieved by matching the covariance of the prior distributions with the
  inferred prior.
  
  References:
      .. [#1] `Variational Inference of Disentangled Latent Concepts from Unlabeled Observations (DIP-VAE), ICLR 2018.
       Kumar, Sattigeri, Balakrishnan. <https://arxiv.org/abs/1711.00848>`_
      .. [#2] `Diederik P Kingma and Max Welling. Auto-encoding variational Bayes. ICLR, 2014. <https://arxiv.org/pdf/1312.6114.pdf>`_
class_docstr_tok: ["DIPVAEExplainer","can","be","used","to","visualize","the","changes","in","the","latent","space","of","Disentangled","Inferred","Prior-VAE","or","DIPVAE","[","#","1","]","_",".","This","model","is","a","Variational","Autoencoder","[","#","2","]","_","variant","that","leads","to","a","disentangled","latent","space",".","This","is","achieved","by","matching","the","covariance","of","the","prior","distributions","with","the","inferred","prior",".","References",":","..","[","#","1","]","`","Variational","Inference","of","Disentangled","Latent","Concepts","from","Unlabeled","Observations","(","DIP-VAE",")",",","ICLR","2018",".","Kumar",",","Sattigeri",",","Balakrishnan",".","<","https",":","\/\/arxiv.org\/abs\/1711.00848",">","`","_","..","[","#","2","]","`","Diederik","P","Kingma","and","Max","Welling",".","Auto-encoding","variational","Bayes",".","ICLR",",","2014",".","<","https",":","\/\/arxiv.org\/pdf\/1312.6114.pdf",">","`","_"]
docstr: Train the underlying generative model.
  
  Args:
      visualize: Plot reconstructions during fit.
      save_dir: directory where plots and model will be saved.
  Returns:
      elbo
docstr_tok: ["Train","the","underlying","generative","model",".","Args",":","visualize",":","Plot","reconstructions","during","fit",".","save_dir",":","directory","where","plots","and","model","will","be","saved",".","Returns",":","elbo"]
code: def fit(self, visualize=False, save_dir="results"):
      """
      Train the underlying generative model.
  
      Args:
          visualize: Plot reconstructions during fit.
          save_dir: directory where plots and model will be saved.
      Returns:
          elbo
      """
  
      torch.manual_seed(self.model_args.seed)
  
      optimizer = Adam(
          self.net.parameters(), lr=self.model_args.step_size
      )
  
      loss_epoch_list = []
  
      for epoch in np.arange(self.model_args.num_epochs):
          loss_epoch = 0.0
          batch_id = 0
  
          for x, y in self.dataset.next_batch():
              # x, y = torch.tensor(x), torch.tensor(y)
              if self.cuda_available:
                  x = x.cuda()
                  y = y.cuda()
  
              # forward
              if "mnist" in self.dataset.name:
                  loss = self.net.neg_elbo(
                      x.squeeze().view(
                          -1, np.prod(self.dataset.data_dims)
                      )
                  )
              else:
                  loss = self.net.neg_elbo(x)
              # backward
              optimizer.zero_grad()
              loss.backward()
              optimizer.step()
              loss_epoch += loss
  
              batch_id += 1
  
              if visualize and batch_id % 10 == 0:
                  if not os.path.isdir(save_dir):
                      os.makedirs(save_dir)
  
                  plot_reconstructions(
                      self.dataset,
                      self.net,
                      x,
                      image_id_to_plot=2,
                      epoch=epoch,
                      batch_id=batch_id,
                      save_dir=save_dir,
                  )
                  if batch_id % 100 == 0:
                      plot_latent_traversal(
                          self,
                          x,
                          self.model_args,
                          self.dataset,
                          image_id_to_plot=2,
                          epoch=epoch,
                          batch_id=batch_id,
                          save_dir=save_dir,
                      )
                      torch.save(
                          self.net, os.path.join(save_dir, "net.p")
                      )
  
          loss_epoch_list.append(
              -loss_epoch / self.dataset.num_training_instances
          )
          print(
              "Epoch {0} | ELBO {1}".format(
                  epoch,
                  -loss_epoch / self.dataset.num_training_instances,
              )
          )
  
      return loss_epoch_list
  
file_path: AIX360/aix360/algorithms/dipvae/dipvae.py
imports: from __future__ import print_function
  import torch
  from torch import __init__
  from torch.optim import Adam
  from .dipvae_utils import VAE, DIPVAE
  from .dipvae_utils import plot_reconstructions, plot_latent_traversal
  from aix360.algorithms.die import DIExplainer
  import os
  import sys
  import random
  import time
  import numpy
label_desc: Use image node_id 4 for calling the DIPVAEExplainer obj's underlying member method code with example usage: obj.fit(visualize, save_dir) and returns: loss_epoch_list